Skip to content

Commit

Permalink
Merge pull request #158 from Josmar-jr/fix/save-edit-address-data
Browse files Browse the repository at this point in the history
fix: save edit, remove and add address data at organization
  • Loading branch information
ataideverton authored May 28, 2024
2 parents f137175 + 1afbb40 commit c17bb9e
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 91 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed

- Save new address within the Cost Center in the store

## [1.31.4] - 2024-05-23

Expand Down
202 changes: 147 additions & 55 deletions react/components/CostCenterDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FunctionComponent } from 'react'
import React, { useEffect, useState, useContext } from 'react'
import React, { useEffect, useState, useContext, useCallback } from 'react'
import { useQuery, useMutation, useLazyQuery } from 'react-apollo'
import {
Layout,
Expand Down Expand Up @@ -137,9 +137,10 @@ const CostCenterDetails: FunctionComponent<RouterProps> = ({

const { data: permissionsData } = useQuery(GET_PERMISSIONS, { ssr: false })

const [updateCostCenter] = useMutation(UPDATE_COST_CENTER)
const [deleteCostCenter] = useMutation(DELETE_COST_CENTER)

const [updateCostCenter] = useMutation(UPDATE_COST_CENTER)

const handleSetAddresses = (_addresses: Address[]) => {
setAddresses(
_addresses.map((item, index) => {
Expand Down Expand Up @@ -231,9 +232,10 @@ const CostCenterDetails: FunctionComponent<RouterProps> = ({
input: {
name: costCenterName,
addresses: _addresses.map(item => {
delete item.checked

return item
return {
...item,
checked: undefined,
}
}),
paymentTerms,
phoneNumber,
Expand Down Expand Up @@ -303,57 +305,88 @@ const CostCenterDetails: FunctionComponent<RouterProps> = ({
setDeleteCostCenterModalState({ isOpen: true })
}

const handleAddNewAddress = (address: AddressFormFields) => {
const uid = setGUID(address)
const handleAddNewAddress = useCallback(
(address: AddressFormFields) => {
const uid = setGUID(address)

const duplicated = data?.getCostCenterByIdStorefront?.addresses?.find(
(item: any) => item.addressId === uid
)
const duplicated = data?.getCostCenterByIdStorefront?.addresses?.find(
({ addressId }: { addressId: string }) => addressId === uid
)

let isDuplicatedError = false
let isDuplicatedError = false

if (duplicated !== undefined) {
isDuplicatedError = duplicated.postalCode === address.postalCode.value
}
if (duplicated !== undefined) {
isDuplicatedError = duplicated.postalCode === address.postalCode.value
}

if (!isDuplicatedError) {
const newAddress = {
addressId: uid,
addressType: address.addressType.value,
city: address.city.value,
complement: address.complement.value,
country: address.country.value,
receiverName: address.receiverName.value,
geoCoordinates: address.geoCoordinates.value,
neighborhood: address.neighborhood.value,
number: address.number.value,
postalCode: address.postalCode.value,
reference: address.reference.value,
state: address.state.value,
street: address.street.value,
addressQuery: address.addressQuery.value,
} as Address

const newAddresses = [...addresses, newAddress]

setAddresses(
newAddresses.map(item => {
if (address.checked) {
item.checked = item === newAddress
}
if (!isDuplicatedError) {
const newAddress = {
addressId: uid,
addressType: address.addressType.value,
city: address.city.value,
complement: address.complement.value,
country: address.country.value,
receiverName: address.receiverName.value,
geoCoordinates: address.geoCoordinates.value,
neighborhood: address.neighborhood.value,
number: address.number.value,
postalCode: address.postalCode.value,
reference: address.reference.value,
state: address.state.value,
street: address.street.value,
addressQuery: address.addressQuery.value,
} as Address

const newAddresses = [...addresses, newAddress]

const variables = {
id: params.id,
input: {
addresses: newAddresses.map(item => {
return {
...item,
checked: undefined,
}
}),
},
}

setLoadingState(true)

return item
updateCostCenter({
variables,
})
)
.then(() => {
toastMessage(messages.toastUpdateSuccess)
refetch()

handleCloseModals()
} else {
showToast({
variant: 'critical',
message: formatMessage(messages.duplicateAddress),
})
}
}
setLoadingState(false)
handleCloseModals()

setAddresses(
newAddresses.map(item => {
if (address.checked) {
item.checked = item === newAddress
}

return item
})
)
})
.catch(error => {
console.error(error)
toastMessage(messages.toastUpdateFailure)
setLoadingState(false)
})
} else {
showToast({
variant: 'critical',
message: formatMessage(messages.duplicateAddress),
})
}
},
[addresses]
)

const handleCheckDefault = (address: Address) => {
setAddresses(
Expand Down Expand Up @@ -390,11 +423,40 @@ const CostCenterDetails: FunctionComponent<RouterProps> = ({
addressQuery: modifiedAddress.addressQuery.value,
} as Address

setAddresses(addressArray)
handleCloseModals()
const variables = {
id: params.id,
input: {
addresses: addressArray.map(item => {
return {
...item,
checked: undefined,
}
}),
},
}

setLoadingState(true)

updateCostCenter({
variables,
})
.then(() => {
toastMessage(messages.toastUpdateSuccess)
refetch()

setLoadingState(false)

setAddresses(addressArray)
handleCloseModals()
})
.catch(error => {
console.error(error)
toastMessage(messages.toastUpdateFailure)
setLoadingState(false)
})
}

const handleDeleteAddress = () => {
const handleDeleteAddress = useCallback(() => {
const { addressId } = deleteAddressModalState
const addressArray = addresses

Expand All @@ -403,9 +465,39 @@ const CostCenterDetails: FunctionComponent<RouterProps> = ({
)

addresses.splice(addressIndex, 1)
setAddresses(addressArray)
handleCloseModals()
}

const variables = {
id: params.id,
input: {
addresses: addressArray.map(item => {
return {
...item,
checked: undefined,
}
}),
},
}

setLoadingState(true)

updateCostCenter({
variables,
})
.then(() => {
toastMessage(messages.toastUpdateSuccess)
refetch()

setLoadingState(false)

setAddresses(addressArray)
handleCloseModals()
})
.catch(error => {
console.error(error)
toastMessage(messages.toastUpdateFailure)
setLoadingState(false)
})
}, [addresses])

const handleTogglePaymentTerm = (id: string) => {
let newTerms = paymentTerms
Expand Down
18 changes: 9 additions & 9 deletions react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
"@vtex/tsconfig": "^0.6.0",
"apollo-cache-inmemory": "^1.6.5",
"graphql": "^14.6.0",
"vtex.address-form": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.address-form@4.22.4/public/@types/vtex.address-form",
"vtex.address-form": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.address-form@4.23.3/public/@types/vtex.address-form",
"vtex.admin-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.admin-graphql@2.40.0/public/@types/vtex.admin-graphql",
"vtex.b2b-organizations": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations@1.27.3/public/@types/vtex.b2b-organizations",
"vtex.b2b-organizations-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations-graphql@0.41.0/public/@types/vtex.b2b-organizations-graphql",
"vtex.catalog-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.catalog-graphql@1.102.3/public/@types/vtex.catalog-graphql",
"vtex.b2b-organizations": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations@1.31.3/public/@types/vtex.b2b-organizations",
"vtex.b2b-organizations-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations-graphql@0.49.4/public/@types/vtex.b2b-organizations-graphql",
"vtex.catalog-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.catalog-graphql@1.103.1/public/@types/vtex.catalog-graphql",
"vtex.country-codes": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.country-codes@2.0.1/public/_types/react",
"vtex.css-handles": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.css-handles@0.4.4/public/@types/vtex.css-handles",
"vtex.my-account": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.my-account@1.27.0/public/@types/vtex.my-account",
"vtex.my-account": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.my-account@1.27.1/public/@types/vtex.my-account",
"vtex.my-account-commons": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.my-account-commons@1.7.1/public/@types/vtex.my-account-commons",
"vtex.product-context": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-context@0.10.0/public/@types/vtex.product-context",
"vtex.product-context": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-context@0.10.1/public/@types/vtex.product-context",
"vtex.render-runtime": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.render-runtime@8.134.2/public/@types/vtex.render-runtime",
"vtex.store": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store@2.132.0/public/@types/vtex.store",
"vtex.store-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-graphql@2.170.0/public/@types/vtex.store-graphql",
"vtex.storefront-permissions": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.storefront-permissions@1.36.0/public/@types/vtex.storefront-permissions",
"vtex.store": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store@2.135.0/public/@types/vtex.store",
"vtex.store-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-graphql@2.170.2/public/@types/vtex.store-graphql",
"vtex.storefront-permissions": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.storefront-permissions@1.40.4/public/@types/vtex.storefront-permissions",
"vtex.styleguide": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.styleguide@9.146.9/public/@types/vtex.styleguide"
},
"dependencies": {
Expand Down
54 changes: 27 additions & 27 deletions react/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6066,25 +6066,25 @@ verror@1.10.0:
core-util-is "1.0.2"
extsprintf "^1.2.0"

"vtex.address-form@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.address-form@4.22.4/public/@types/vtex.address-form":
version "4.22.4"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.address-form@4.22.4/public/@types/vtex.address-form#aa2423ec31d295c1861a6f08b7216f995f747a98"
"vtex.address-form@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.address-form@4.23.3/public/@types/vtex.address-form":
version "4.23.3"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.address-form@4.23.3/public/@types/vtex.address-form#983d8b8254e65c2d2de6a003b2d086391c7b78b8"

"vtex.admin-graphql@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.admin-graphql@2.40.0/public/@types/vtex.admin-graphql":
version "2.40.0"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.admin-graphql@2.40.0/public/@types/vtex.admin-graphql#6abd9321f95ac522b200112ee8c556eb3c6936d5"

"vtex.b2b-organizations-graphql@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations-graphql@0.41.0/public/@types/vtex.b2b-organizations-graphql":
version "0.41.0"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations-graphql@0.41.0/public/@types/vtex.b2b-organizations-graphql#dbc4b19d561230f991eea3d2502f95fbdcedf6ee"
"vtex.b2b-organizations-graphql@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations-graphql@0.49.4/public/@types/vtex.b2b-organizations-graphql":
version "0.49.4"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations-graphql@0.49.4/public/@types/vtex.b2b-organizations-graphql#8705542c4908c69c32492a2faa4515cf048a4334"

"vtex.b2b-organizations@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations@1.27.3/public/@types/vtex.b2b-organizations":
version "1.27.3"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations@1.27.3/public/@types/vtex.b2b-organizations#db21fa79490615bc5428c60037e71db22e19edf1"
"vtex.b2b-organizations@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations@1.31.3/public/@types/vtex.b2b-organizations":
version "1.31.3"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.b2b-organizations@1.31.3/public/@types/vtex.b2b-organizations#0c579cd04477e56236467ff625fc2c717f79dfff"

"vtex.catalog-graphql@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.catalog-graphql@1.102.3/public/@types/vtex.catalog-graphql":
version "1.102.3"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.catalog-graphql@1.102.3/public/@types/vtex.catalog-graphql#ce99343170d314d3231612357ef62e3356b50d81"
"vtex.catalog-graphql@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.catalog-graphql@1.103.1/public/@types/vtex.catalog-graphql":
version "1.103.1"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.catalog-graphql@1.103.1/public/@types/vtex.catalog-graphql#c1b1caa413a7fc65b029814e67c944541daec337"

"vtex.country-codes@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.country-codes@2.0.1/public/_types/react":
version "0.0.0"
Expand All @@ -6098,29 +6098,29 @@ verror@1.10.0:
version "1.7.1"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.my-account-commons@1.7.1/public/@types/vtex.my-account-commons#2ef5346cfcb342a528ac03b7e583d87d1f6b4745"

"vtex.my-account@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.my-account@1.27.0/public/@types/vtex.my-account":
version "1.27.0"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.my-account@1.27.0/public/@types/vtex.my-account#a0d6dc12364422b9e649aa1e4da4b2756453fdfe"
"vtex.my-account@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.my-account@1.27.1/public/@types/vtex.my-account":
version "1.27.1"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.my-account@1.27.1/public/@types/vtex.my-account#bb94dc0514950137df740439a599d3f413624d75"

"vtex.product-context@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-context@0.10.0/public/@types/vtex.product-context":
version "0.10.0"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-context@0.10.0/public/@types/vtex.product-context#c5e2a97b404004681ee12f4fff7e6b62157786cc"
"vtex.product-context@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-context@0.10.1/public/@types/vtex.product-context":
version "0.10.1"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-context@0.10.1/public/@types/vtex.product-context#86ceba68085420edcf54749f07e51a257d2e5d94"

"vtex.render-runtime@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.render-runtime@8.134.2/public/@types/vtex.render-runtime":
version "8.134.2"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.render-runtime@8.134.2/public/@types/vtex.render-runtime#ae69e2b2a471291c6c6b155e17510150fbfc2d0e"

"vtex.store-graphql@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-graphql@2.170.0/public/@types/vtex.store-graphql":
version "2.170.0"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-graphql@2.170.0/public/@types/vtex.store-graphql#9827a5a84fc90c238e16a1feab451caaf6ba59b2"
"vtex.store-graphql@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-graphql@2.170.2/public/@types/vtex.store-graphql":
version "2.170.2"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-graphql@2.170.2/public/@types/vtex.store-graphql#18cb23b99445e8027822218812143a6ffed8541e"

"vtex.store@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store@2.132.0/public/@types/vtex.store":
version "2.132.0"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store@2.132.0/public/@types/vtex.store#f230c2340528267fbbed36c365f898184d05c872"
"vtex.store@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store@2.135.0/public/@types/vtex.store":
version "2.135.0"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store@2.135.0/public/@types/vtex.store#00f447a8320298247d3e8e4dd43e40b841f38145"

"vtex.storefront-permissions@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.storefront-permissions@1.36.0/public/@types/vtex.storefront-permissions":
version "1.36.0"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.storefront-permissions@1.36.0/public/@types/vtex.storefront-permissions#9b6eca8b176bd2b4a349932a598b6b8691bd6fe9"
"vtex.storefront-permissions@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.storefront-permissions@1.40.4/public/@types/vtex.storefront-permissions":
version "1.40.4"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.storefront-permissions@1.40.4/public/@types/vtex.storefront-permissions#9e6f5ad5186ec29a0a024dae750ca701eefd09e2"

"vtex.styleguide@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.styleguide@9.146.9/public/@types/vtex.styleguide":
version "9.146.9"
Expand Down

0 comments on commit c17bb9e

Please sign in to comment.