-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathpickup-address.jsx
More file actions
130 lines (123 loc) · 4.72 KB
/
pickup-address.jsx
File metadata and controls
130 lines (123 loc) · 4.72 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
/*
* 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 AddressDisplay from '@salesforce/retail-react-app/app/components/address-display'
// Hooks
import {useCheckout} from '@salesforce/retail-react-app/app/pages/checkout/util/checkout-context'
import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket'
import {useShopperBasketsMutation, useStores} from '@salesforce/commerce-sdk-react'
export default function PickupAddress() {
const {formatMessage} = useIntl()
const [isLoading, setIsLoading] = useState()
const updateShippingAddressForShipment = useShopperBasketsMutation(
'updateShippingAddressForShipment'
)
const {step, STEPS, goToStep} = useCheckout()
const {data: basket} = useCurrentBasket()
const selectedShippingAddress = basket?.shipments && basket?.shipments[0]?.shippingAddress
const isAddressFilled = selectedShippingAddress?.address1 && selectedShippingAddress?.city
// Check if basket is a pickup order
const isPickupOrder = basket?.shipments?.[0]?.shippingMethod?.c_storePickupEnabled === true
const storeId = basket?.shipments?.[0]?.c_fromStoreId
const {data: storeData} = useStores(
{
parameters: {
ids: storeId
}
},
{
enabled: !!storeId && isPickupOrder
}
)
const store = storeData?.data?.[0]
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: '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>
{pickupAddress && <AddressDisplay address={pickupAddress} />}
<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>
</>
)}
{isAddressFilled && (
<ToggleCardSummary>
<Text fontWeight="bold" fontSize="md" mb={2}>
<FormattedMessage
defaultMessage="Store Information"
id="pickup_address.title.store_information"
/>
</Text>
<AddressDisplay address={selectedShippingAddress} />
</ToggleCardSummary>
)}
</ToggleCard>
)
}