-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathone-click-pickup-address.jsx
More file actions
184 lines (176 loc) · 6.85 KB
/
one-click-pickup-address.jsx
File metadata and controls
184 lines (176 loc) · 6.85 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (c) 2025, 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 {FormattedMessage, useIntl} from 'react-intl'
// Components
import {Box, Button, Container, Text} from '@salesforce/retail-react-app/app/components/shared/ui'
import {
ToggleCard,
ToggleCardSummary
} from '@salesforce/retail-react-app/app/components/toggle-card'
import StoreDisplay from '@salesforce/retail-react-app/app/components/store-display'
// Hooks
import {useCheckout} from '@salesforce/retail-react-app/app/pages/checkout-one-click/util/checkout-context'
import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket'
import {
useShopperBasketsV2Mutation as useShopperBasketsMutation,
useStores
} from '@salesforce/commerce-sdk-react'
import {isPickupShipment} from '@salesforce/retail-react-app/app/utils/shipment-utils'
const PickupAddress = () => {
const {formatMessage} = useIntl()
const [isLoading, setIsLoading] = useState()
const updateShippingAddressForShipment = useShopperBasketsMutation(
'updateShippingAddressForShipment'
)
const {step, STEPS, goToStep} = useCheckout()
const {data: basket} = useCurrentBasket()
// Find the pickup shipment that actually has items assigned
const shipments = basket?.shipments || []
const items = basket?.productItems || []
const shipmentsWithItems = shipments.filter((s) =>
items.some((i) => i.shipmentId === s.shipmentId)
)
const pickupShipment = shipmentsWithItems.find((s) => isPickupShipment(s))
// Check if basket is a pickup order
const isPickupOrder = !!pickupShipment
const storeId = pickupShipment?.c_fromStoreId
const {data: storeData, isLoading: isStoreLoading} = useStores(
{
parameters: {
ids: storeId
}
},
{
enabled: !!storeId && isPickupOrder
}
)
const store = storeData?.data?.[0]
// Build address object for shipping API update (required for order processing)
const pickupAddress = {
address1: store?.address1,
city: store?.city,
countryCode: store?.countryCode,
postalCode: store?.postalCode,
stateCode: store?.stateCode,
firstName: store?.name,
lastName: 'Pickup',
phone: store?.phone
}
const submitAndContinue = async (address) => {
setIsLoading(true)
const {address1, city, countryCode, firstName, lastName, phone, postalCode, stateCode} =
address
await updateShippingAddressForShipment.mutateAsync({
parameters: {
basketId: basket.basketId,
shipmentId: pickupShipment?.shipmentId || 'me',
useAsBilling: false
},
body: {
address1,
city,
countryCode,
firstName,
lastName,
phone,
postalCode,
stateCode
}
})
setIsLoading(false)
goToStep(STEPS.PAYMENT)
}
return (
<ToggleCard
id="step-1"
title={formatMessage({
defaultMessage: 'Pickup Address & Information',
id: 'pickup_address.title.pickup_address'
})}
editing={step === STEPS.PICKUP_ADDRESS}
disabled={step === STEPS.CONTACT_INFO}
isLoading={isLoading}
>
{step === STEPS.PICKUP_ADDRESS && (
<>
<Text fontWeight="bold" fontSize="md" mb={2}>
<FormattedMessage
defaultMessage="Store Information"
id="pickup_address.title.store_information"
/>
</Text>
{store ? (
<Box mb={4}>
<StoreDisplay
store={store}
showDistance={true}
showStoreHours={true}
showPhone={true}
showEmail={true}
nameStyle={{
fontSize: 'sm',
fontWeight: 'normal'
}}
textSize="sm"
/>
</Box>
) : isStoreLoading ? (
<Text>
<FormattedMessage
defaultMessage="Loading store information…"
id="pickup_address.message.loading_store_info"
/>
</Text>
) : null}
<Box pt={3}>
<Container variant="form">
<Button w="full" onClick={() => submitAndContinue(pickupAddress)}>
<FormattedMessage
defaultMessage="Continue to Payment"
id="pickup_address.button.continue_to_payment"
/>
</Button>
</Container>
</Box>
</>
)}
{isPickupOrder && (store || isStoreLoading) && (
<ToggleCardSummary>
<Text fontWeight="bold" fontSize="md" mb={2}>
<FormattedMessage
defaultMessage="Store Information"
id="pickup_address.title.store_information"
/>
</Text>
{store ? (
<StoreDisplay
store={store}
showDistance={true}
showStoreHours={true}
showPhone={true}
showEmail={true}
nameStyle={{
fontSize: 'sm',
fontWeight: 'normal'
}}
textSize="sm"
/>
) : (
<Text>
<FormattedMessage
defaultMessage="Loading store information…"
id="pickup_address.message.loading_store_info"
/>
</Text>
)}
</ToggleCardSummary>
)}
</ToggleCard>
)
}
export default PickupAddress