Skip to content

Commit

Permalink
Fix fill/edit billing address form using invertAddress prop
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed Oct 2, 2024
1 parent 0b4c761 commit 0514e06
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import addressReducer, {
type SetAddressParams,
setCloneAddress,
saveAddresses,
ICustomerAddress
type ICustomerAddress
} from '#reducers/AddressReducer'
import { type BaseError } from '#typings/errors'
import OrderContext from '#context/OrderContext'
Expand Down Expand Up @@ -94,7 +94,7 @@ export function AddressesContainer(props: Props): JSX.Element {
defaultAddressContext.setAddress({ ...params, dispatch })
},
saveAddresses: async (params: {
customerEmail?: string,
customerEmail?: string
customerAddress?: ICustomerAddress
}): ReturnType<typeof saveAddresses> =>
await saveAddresses({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function BillingAddressForm(props: Props): JSX.Element {
setErrorForm({
name: field,
code: 'VALIDATION_ERROR',
message: message
message
})
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export function SaveAddressesButton(props: Props): JSX.Element {
lineItems: order?.line_items
})
// NOTE: This is a temporary fix to avoid the button to be disabled when the user is editing an address
const invertAddressesDisable = invertAddresses && shippingAddressId ? false : shippingDisable
const invertAddressesDisable =
invertAddresses && shippingAddressId ? false : shippingDisable
const disable =
disabled ||
customerEmail ||
Expand All @@ -120,29 +121,40 @@ export function SaveAddressesButton(props: Props): JSX.Element {
success: false
}
setForceDisable(true)
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (true) {
case order != null && addressId != null && createCustomerAddress != null && saveAddresses != null:
case order != null &&
addressId != null &&
createCustomerAddress != null &&
saveAddresses != null: {
response = await saveAddresses({
customerEmail: email,
customerAddress: {
resource: invertAddresses ? 'shipping_address' : 'billing_address',
resource: invertAddresses
? 'shipping_address'
: 'billing_address',
id: addressId
}
})
break;
case order != null && saveAddresses != null:
break
}
case order != null && saveAddresses != null: {
response = await saveAddresses({
customerEmail: email,
customerEmail: email
})
break;
case createCustomerAddress != null:
const address = invertAddresses ? { ...shippingAddress } : { ...billingAddress }
break
}
case createCustomerAddress != null: {
const address = invertAddresses
? { ...shippingAddress }
: { ...billingAddress }
if (addressId) address.id = addressId
void createCustomerAddress(address as TCustomerAddress)
response = {
success: true
}
break;
break
}
}
setForceDisable(false)
if (onClick && response.success) onClick(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ export function BraintreePayment({
const validStatus =
response?.liabilityShiftPossible === true &&
response?.liabilityShifted === true
console.log('validStatus', validStatus)
if (validStatus && paymentSource != null) {
await setPaymentSource({
paymentSourceId: paymentSource.id,
Expand Down
41 changes: 29 additions & 12 deletions packages/react-components/src/reducers/AddressReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { type updateOrder } from './OrderReducer'
import camelCase from 'lodash/camelCase'
import { type TCustomerAddress } from './CustomerReducer'
import { type TResourceError } from '#components/errors/Errors'
import { invertedAddressesHandler, sanitizeMetadataFields } from '#utils/addressesManager'
import {
invertedAddressesHandler,
sanitizeMetadataFields
} from '#utils/addressesManager'
import { formCleaner } from '#utils/formCleaner'
import { type AddressValuesKeys } from '#context/BillingAddressFormContext'
import { type AddressInputName } from '#typings/index'
Expand All @@ -25,12 +28,15 @@ export type CustomFieldMessageError = (props: {
message?: string | undefined
value: string
values?: Record<string, any>
}) => string | null | {
field: Extract<AddressValuesKeys, AddressInputName> | string
value: string
isValid: boolean
message?: string
}[]
}) =>
| string
| null
| Array<{
field: Extract<AddressValuesKeys, AddressInputName> | string
value: string
isValid: boolean
message?: string
}>

export type AddressActionType =
| 'setErrors'
Expand Down Expand Up @@ -195,7 +201,7 @@ export async function saveAddresses({
}: TSaveAddressesParams): Promise<{
success: boolean
order?: Order
error?: unknown,
error?: unknown
}> {
const {
shipToDifferentAddress,
Expand All @@ -209,8 +215,14 @@ export async function saveAddresses({
const sdk = getSdk(config)
if (order) {
let orderAttributes: OrderUpdate | null = null
const billingAddressCloneId = customerAddress?.resource === 'billing_address' ? customerAddress?.id : billingAddressId
const shippingAddressCloneId = customerAddress?.resource === 'shipping_address' ? customerAddress?.id : shippingAddressId
const billingAddressCloneId =
customerAddress?.resource === 'billing_address'
? customerAddress?.id
: billingAddressId
const shippingAddressCloneId =
customerAddress?.resource === 'shipping_address'
? customerAddress?.id
: shippingAddressId
if (invertAddresses) {
orderAttributes = await invertedAddressesHandler({
billingAddress,
Expand Down Expand Up @@ -239,7 +251,11 @@ export async function saveAddresses({
orderAttributes._shipping_address_clone_id =
order?.shipping_address?.id
}
if (billingAddress != null && Object.keys(billingAddress).length > 0 && !billingAddressCloneId) {
if (
billingAddress != null &&
Object.keys(billingAddress).length > 0 &&
!billingAddressCloneId
) {
delete orderAttributes._billing_address_clone_id
delete orderAttributes._shipping_address_clone_id
if (!doNotShipItems) {
Expand All @@ -260,7 +276,8 @@ export async function saveAddresses({
Object.keys(shippingAddress).length > 0
) {
delete orderAttributes._shipping_address_clone_id
const shippingAddressWithMeta = sanitizeMetadataFields(shippingAddress)
const shippingAddressWithMeta =
sanitizeMetadataFields(shippingAddress)
const address = await sdk.addresses.create(shippingAddressWithMeta)
orderAttributes.shipping_address = sdk.addresses.relationship(
address.id
Expand Down
8 changes: 6 additions & 2 deletions packages/react-components/src/utils/addressesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ export async function invertedAddressesHandler({
orderAttributes._billing_address_clone_id = order?.billing_address?.id
orderAttributes._shipping_address_clone_id = order?.shipping_address?.id
}
if (shippingAddress != null && Object.keys(shippingAddress).length > 0 && !shippingAddressId) {
if (
shippingAddress != null &&
Object.keys(shippingAddress).length > 0 &&
!shippingAddressId
) {
delete orderAttributes._billing_address_clone_id
delete orderAttributes._shipping_address_clone_id
orderAttributes._billing_address_same_as_shipping = true
Expand Down Expand Up @@ -359,4 +363,4 @@ export function sanitizeMetadataFields(address: AddressCreate): AddressCreate {
})
}
return address
}
}
5 changes: 5 additions & 0 deletions packages/react-components/src/utils/formCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { type AddressSchema } from '#reducers/AddressReducer'

export function formCleaner(address: AddressSchema): AddressSchema {
Object.keys(address).forEach((key) => {
const keyCleaned = key
.replace('shipping_address_', '')
.replace('billing_address_', '')
const isNotCleaned =
key.startsWith('shipping_address_') || key.startsWith('billing_address_')
if (isNotCleaned) {
// @ts-expect-error type error
address[keyCleaned] = address[key]
// @ts-expect-error type error
delete address[key]
}
Expand Down

0 comments on commit 0514e06

Please sign in to comment.