Skip to content

Commit

Permalink
feat: Expose address as param of onSelect function
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed Jan 28, 2022
1 parent 513b438 commit 29ee53e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
24 changes: 16 additions & 8 deletions src/components/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import OrderContext from '#context/OrderContext'
import AddressCardsTemplate, {
AddressCardsTemplateChildren,
CustomerAddress,
HandleSelect,
} from './utils/AddressCardsTemplate'

const propTypes = components.Address.propTypes
Expand All @@ -26,7 +27,7 @@ type Props = {
children: ReactNode | AddressCardsTemplateChildren
selectedClassName?: string
disabledClassName?: string
onSelect?: () => void
onSelect?: (address: AddressType) => void
addresses?: AddressType[]
deselect?: boolean
} & JSX.IntrinsicElements['div']
Expand Down Expand Up @@ -97,19 +98,20 @@ const Address: FunctionComponent<Props> = (props) => {
addressesContext,
shipToDifferentAddress,
])
const handleSelect = async (
k: number,
addressId: string,
customerAddressId: string,
disabled: boolean
const handleSelect: HandleSelect = async (
k,
addressId,
customerAddressId,
disabled,
address
) => {
!disabled && setSelected(k)
setBillingAddress &&
(await setBillingAddress(addressId, { customerAddressId }))
!disabled &&
setShippingAddress &&
(await setShippingAddress(addressId, { customerAddressId }))
onSelect && onSelect()
onSelect && onSelect(address)
}
const countryLock = order?.shipping_country_code_lock
const components =
Expand Down Expand Up @@ -147,7 +149,13 @@ const Address: FunctionComponent<Props> = (props) => {
<div
className={finalClassName}
onClick={() =>
handleSelect(k, address.id, customerAddressId, disabled)
handleSelect(
k,
address.id,
customerAddressId,
disabled,
address
)
}
data-disabled={disabled}
{...p}
Expand Down
7 changes: 4 additions & 3 deletions src/components/utils/AddressCardsTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ export type AddressCardsTemplateChildren = FunctionChildren<ChildrenProps>

export type AddressCardsType = ChildrenProps

type HandleSelect = (
export type HandleSelect = (
k: number,
addressId: string,
customerAddressId: string,
disabled: boolean
disabled: boolean,
address: Address
) => void

type Props = {
Expand Down Expand Up @@ -64,7 +65,7 @@ export default function AddressCardsTemplate({
: addressSelectedClass
const customerAddressId: string = address?.reference || ''
const onClick = () =>
handleSelect(k, address.id, customerAddressId, disabled)
handleSelect(k, address.id, customerAddressId, disabled, address)
return {
...attributes,
className: finalClassName,
Expand Down

0 comments on commit 29ee53e

Please sign in to comment.