-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathshipping-options.jsx
More file actions
270 lines (257 loc) · 13.3 KB
/
shipping-options.jsx
File metadata and controls
270 lines (257 loc) · 13.3 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* 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, {useEffect} from 'react'
import {FormattedMessage, FormattedNumber, useIntl} from 'react-intl'
import {Box, Button, Container, Flex, HStack, RadioGroup, Stack, Text} from '@chakra-ui/react'
import {useForm, Controller} from 'react-hook-form'
import {useCheckout} from '../../../pages/checkout/util/checkout-context'
import {ChevronDownIcon} from '../../../components/icons'
import {ToggleCard, ToggleCardEdit, ToggleCardSummary} from '../../../components/toggle-card'
import {
useShippingMethodsForShipment,
useShopperBasketsMutation
} from '@salesforce/commerce-sdk-react'
import {useCurrentBasket} from '../../../hooks/use-current-basket'
import {useCurrency} from '../../../hooks'
export default function ShippingOptions() {
const {formatMessage} = useIntl()
const {step, STEPS, goToStep, goToNextStep} = useCheckout()
const {data: basket} = useCurrentBasket()
const {currency} = useCurrency()
const updateShippingMethod = useShopperBasketsMutation('updateShippingMethodForShipment')
const {data: shippingMethods} = useShippingMethodsForShipment(
{
parameters: {
basketId: basket?.basketId,
shipmentId: 'me'
}
},
{
enabled: Boolean(basket?.basketId) && step === STEPS.SHIPPING_OPTIONS
}
)
const selectedShippingMethod = basket?.shipments?.[0]?.shippingMethod
const selectedShippingAddress = basket?.shipments?.[0]?.shippingAddress
const form = useForm({
shouldUnregister: false,
defaultValues: {
shippingMethodId: selectedShippingMethod?.id || shippingMethods?.defaultShippingMethodId
}
})
useEffect(() => {
const defaultMethodId = shippingMethods?.defaultShippingMethodId
const methodId = form.getValues().shippingMethodId
if (!selectedShippingMethod && !methodId && defaultMethodId) {
form.reset({shippingMethodId: defaultMethodId})
}
if (selectedShippingMethod && methodId !== selectedShippingMethod.id) {
form.reset({shippingMethodId: selectedShippingMethod.id})
}
}, [selectedShippingMethod, shippingMethods])
const submitForm = async ({shippingMethodId}) => {
await updateShippingMethod.mutateAsync({
parameters: {
basketId: basket.basketId,
shipmentId: 'me'
},
body: {
id: shippingMethodId
}
})
goToNextStep()
}
const shippingItem = basket?.shippingItems?.[0]
const selectedMethodDisplayPrice = Math.min(
shippingItem?.price || 0,
shippingItem?.priceAfterItemDiscount || 0
)
const freeLabel = formatMessage({
defaultMessage: 'Free',
id: 'checkout_confirmation.label.free'
})
let shippingPriceLabel = selectedMethodDisplayPrice
if (selectedMethodDisplayPrice !== shippingItem?.price) {
const currentPrice =
selectedMethodDisplayPrice === 0 ? freeLabel : selectedMethodDisplayPrice
shippingPriceLabel = formatMessage(
{
defaultMessage: 'Originally {originalPrice}, now {newPrice}',
id: 'checkout_confirmation.label.shipping.strikethrough.price'
},
{
originalPrice: shippingItem?.price,
newPrice: currentPrice
}
)
}
// Note that this card is disabled when there is no shipping address as well as no shipping method.
// We do this because we apply the default shipping method to the basket before checkout - so when
// landing on checkout the first time will put you at the first step (contact info), but the shipping
// method step would appear filled out already. This fix attempts to avoid any confusion in the UI.
return (
<ToggleCard
id="step-2"
title={formatMessage({
defaultMessage: 'Shipping & Gift Options',
id: 'shipping_options.title.shipping_gift_options'
})}
editing={step === STEPS.SHIPPING_OPTIONS}
isLoading={form.formState.isSubmitting}
disabled={selectedShippingMethod == null || !selectedShippingAddress}
onEdit={() => goToStep(STEPS.SHIPPING_OPTIONS)}
editLabel={formatMessage({
defaultMessage: 'Edit Shipping Options',
id: 'toggle_card.action.editShippingOptions'
})}
>
<ToggleCardEdit>
<form
onSubmit={form.handleSubmit(submitForm)}
data-testid="sf-checkout-shipping-options-form"
>
<Stack gap={6}>
{shippingMethods?.applicableShippingMethods && (
<Controller
name="shippingMethodId"
control={form.control}
defaultValue=""
render={({field: {value, onChange}}) => (
<RadioGroup.Root
name="shipping-options-radiogroup"
value={value}
onValueChange={(selected) => {
onChange(selected.value) // Chakra v3 radio returns the selected id in an object with a value property
}}
>
<Stack gap={5}>
{shippingMethods.applicableShippingMethods.map(
(opt) => (
<RadioGroup.Item value={opt.id} key={opt.id}>
<RadioGroup.ItemHiddenInput />
<RadioGroup.ItemIndicator />
<Flex justify="space-between" w="full">
<HStack>
<Box>
<RadioGroup.ItemText>
{opt.name}
</RadioGroup.ItemText>
<Text
fontSize="sm"
color="gray.600"
>
{opt.description}
</Text>
{opt.shippingPromotions?.map(
(promo) => {
return (
<Text
key={
promo.promotionId
}
fontSize="sm"
color="green.600"
>
{
promo.calloutMsg
}
</Text>
)
}
)}
</Box>
</HStack>
<Text fontWeight="bold">
<FormattedNumber
value={opt.price}
style="currency"
currency={currency}
/>
</Text>
</Flex>
</RadioGroup.Item>
)
)}
</Stack>
</RadioGroup.Root>
)}
/>
)}
<Box>
{/* TODO: Get the rightIcon to display */}
<Button variant="link-blue" size="sm" rightIcon={<ChevronDownIcon />}>
<FormattedMessage
defaultMessage="Do you want to send this as a gift?"
id="shipping_options.action.send_as_a_gift"
/>
</Button>
</Box>
<Box>
<Container variant="form">
<Button w="full" type="submit">
<FormattedMessage
defaultMessage="Continue to Payment"
id="shipping_options.button.continue_to_payment"
/>
</Button>
</Container>
</Box>
</Stack>
</form>
</ToggleCardEdit>
{selectedShippingMethod && selectedShippingAddress && (
<ToggleCardSummary>
<Flex justify="space-between" w="full">
<Text>{selectedShippingMethod.name}</Text>
<Flex alignItems="center" aria-label={shippingPriceLabel} role="group">
<Text fontWeight="bold" aria-hidden="true" role="presentation">
{selectedMethodDisplayPrice === 0 ? (
freeLabel
) : (
<FormattedNumber
value={selectedMethodDisplayPrice}
style="currency"
currency={currency}
/>
)}
</Text>
{selectedMethodDisplayPrice !== shippingItem?.price && (
<Text
fontWeight="normal"
textDecoration="line-through"
color="gray.600"
marginLeft={1}
aria-hidden="true"
role="presentation"
>
<FormattedNumber
style="currency"
currency={currency}
value={shippingItem?.price}
/>
</Text>
)}
</Flex>
</Flex>
<Text fontSize="sm" color="gray.700">
{selectedShippingMethod.description}
</Text>
{shippingItem?.priceAdjustments?.map((adjustment) => {
return (
<Text
key={adjustment.priceAdjustmentId}
fontSize="sm"
color="green.600"
>
{adjustment.itemText}
</Text>
)
})}
</ToggleCardSummary>
)}
</ToggleCard>
)
}