-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathshipping-address.jsx
More file actions
137 lines (130 loc) · 4.67 KB
/
shipping-address.jsx
File metadata and controls
137 lines (130 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {useState} from 'react'
import {nanoid} from 'nanoid'
import {defineMessage, useIntl} from 'react-intl'
import {useCheckout} from '../../../pages/checkout/util/checkout-context'
import {ToggleCard, ToggleCardEdit, ToggleCardSummary} from '../../../components/toggle-card'
import ShippingAddressSelection from '../../../pages/checkout/partials/shipping-address-selection'
import AddressDisplay from '../../../components/address-display'
import {
useShopperCustomersMutation,
useShopperBasketsMutation
} from '@salesforce/commerce-sdk-react'
import {useCurrentCustomer} from '../../../hooks/use-current-customer'
import {useCurrentBasket} from '../../../hooks/use-current-basket'
const submitButtonMessage = defineMessage({
defaultMessage: 'Continue to Shipping Method',
id: 'shipping_address.button.continue_to_shipping'
})
const shippingAddressAriaLabel = defineMessage({
defaultMessage: 'Shipping Address Form',
id: 'shipping_address.label.shipping_address_form'
})
export default function ShippingAddress() {
const {formatMessage} = useIntl()
const [isLoading, setIsLoading] = useState()
const {data: customer} = useCurrentCustomer()
const {data: basket} = useCurrentBasket()
const selectedShippingAddress = basket?.shipments && basket?.shipments[0]?.shippingAddress
const {step, STEPS, goToStep, goToNextStep} = useCheckout()
const createCustomerAddress = useShopperCustomersMutation('createCustomerAddress')
const updateCustomerAddress = useShopperCustomersMutation('updateCustomerAddress')
const updateShippingAddressForShipment = useShopperBasketsMutation(
'updateShippingAddressForShipment'
)
const submitAndContinue = async (address) => {
setIsLoading(true)
const {
addressId,
address1,
city,
countryCode,
firstName,
lastName,
phone,
postalCode,
stateCode
} = address
await updateShippingAddressForShipment.mutateAsync({
parameters: {
basketId: basket.basketId,
shipmentId: 'me',
useAsBilling: false
},
body: {
address1,
city,
countryCode,
firstName,
lastName,
phone,
postalCode,
stateCode
}
})
if (customer.isRegistered && !addressId) {
const body = {
address1,
city,
countryCode,
firstName,
lastName,
phone,
postalCode,
stateCode,
addressId: nanoid()
}
await createCustomerAddress.mutateAsync({
body,
parameters: {customerId: customer.customerId}
})
}
if (customer.isRegistered && addressId) {
await updateCustomerAddress.mutateAsync({
body: address,
parameters: {
customerId: customer.customerId,
addressName: addressId
}
})
}
goToNextStep()
setIsLoading(false)
}
return (
<ToggleCard
id="step-1"
title={formatMessage({
defaultMessage: 'Shipping Address',
id: 'shipping_address.title.shipping_address'
})}
editing={step === STEPS.SHIPPING_ADDRESS}
isLoading={isLoading}
disabled={step === STEPS.CONTACT_INFO && !selectedShippingAddress}
onEdit={() => goToStep(STEPS.SHIPPING_ADDRESS)}
editLabel={formatMessage({
defaultMessage: 'Edit Shipping Address',
id: 'toggle_card.action.editShippingAddress'
})}
>
<ToggleCardEdit>
<ShippingAddressSelection
selectedAddress={selectedShippingAddress}
submitButtonLabel={submitButtonMessage}
onSubmit={submitAndContinue}
formTitleAriaLabel={shippingAddressAriaLabel}
/>
</ToggleCardEdit>
{selectedShippingAddress && (
<ToggleCardSummary>
<AddressDisplay address={selectedShippingAddress} />
</ToggleCardSummary>
)}
</ToggleCard>
)
}