-
-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathindex.tsx
188 lines (171 loc) · 5.4 KB
/
index.tsx
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
"use client"
import React, { useEffect, useMemo, useActionState } from "react"
import Input from "@modules/common/components/input"
import NativeSelect from "@modules/common/components/native-select"
import AccountInfo from "../account-info"
import { HttpTypes } from "@medusajs/types"
import { addCustomerAddress, updateCustomerAddress } from "@lib/data/customer"
type MyInformationProps = {
customer: HttpTypes.StoreCustomer
regions: HttpTypes.StoreRegion[]
}
const ProfileBillingAddress: React.FC<MyInformationProps> = ({
customer,
regions,
}) => {
const regionOptions = useMemo(() => {
return (
regions
?.map((region) => {
return region.countries?.map((country) => ({
value: country.iso_2,
label: country.display_name,
}))
})
.flat() || []
)
}, [regions])
const [successState, setSuccessState] = React.useState(false)
const billingAddress = customer.addresses?.find(
(addr) => addr.is_default_billing
)
const initialState: Record<string, any> = {
isDefaultBilling: true,
isDefaultShipping: false,
error: false,
success: false,
}
if (billingAddress) {
initialState.addressId = billingAddress.id
}
const [state, formAction] = useActionState(
billingAddress ? updateCustomerAddress : addCustomerAddress,
initialState
)
const clearState = () => {
setSuccessState(false)
}
useEffect(() => {
setSuccessState(state.success)
}, [state])
const currentInfo = useMemo(() => {
if (!billingAddress) {
return "No billing address"
}
const country =
regionOptions?.find(
(country) => country?.value === billingAddress.country_code
)?.label || billingAddress.country_code?.toUpperCase()
return (
<div className="flex flex-col font-semibold" data-testid="current-info">
<span>
{billingAddress.first_name} {billingAddress.last_name}
</span>
<span>{billingAddress.company}</span>
<span>
{billingAddress.address_1}
{billingAddress.address_2 ? `, ${billingAddress.address_2}` : ""}
</span>
<span>
{billingAddress.postal_code}, {billingAddress.city}
</span>
<span>{country}</span>
</div>
)
}, [billingAddress, regionOptions])
return (
<form action={formAction} onReset={() => clearState()} className="w-full">
<input type="hidden" name="addressId" value={billingAddress?.id} />
<AccountInfo
label="Billing address"
currentInfo={currentInfo}
isSuccess={successState}
isError={!!state.error}
clearState={clearState}
data-testid="account-billing-address-editor"
>
<div className="grid grid-cols-1 gap-y-2">
<div className="grid grid-cols-2 gap-x-2">
<Input
label="First name"
name="first_name"
defaultValue={billingAddress?.first_name || undefined}
required
data-testid="billing-first-name-input"
/>
<Input
label="Last name"
name="last_name"
defaultValue={billingAddress?.last_name || undefined}
required
data-testid="billing-last-name-input"
/>
</div>
<Input
label="Phone"
name="phone"
defaultValue={billingAddress?.phone || undefined}
data-testid="billing-phone-input"
/>
<Input
label="Company"
name="company"
defaultValue={billingAddress?.company || undefined}
data-testid="billing-company-input"
/>
<Input
label="Address"
name="address_1"
defaultValue={billingAddress?.address_1 || undefined}
required
data-testid="billing-address-1-input"
/>
<Input
label="Apartment, suite, etc."
name="address_2"
defaultValue={billingAddress?.address_2 || undefined}
data-testid="billing-address-2-input"
/>
<div className="grid grid-cols-[144px_1fr] gap-x-2">
<Input
label="Postal code"
name="postal_code"
defaultValue={billingAddress?.postal_code || undefined}
required
data-testid="billing-postcal-code-input"
/>
<Input
label="City"
name="city"
defaultValue={billingAddress?.city || undefined}
required
data-testid="billing-city-input"
/>
</div>
<Input
label="Province"
name="province"
defaultValue={billingAddress?.province || undefined}
data-testid="billing-province-input"
/>
<NativeSelect
name="country_code"
defaultValue={billingAddress?.country_code || undefined}
required
data-testid="billing-country-code-select"
>
<option value="">-</option>
{regionOptions.map((option, i) => {
return (
<option key={i} value={option?.value}>
{option?.label}
</option>
)
})}
</NativeSelect>
</div>
</AccountInfo>
</form>
)
}
export default ProfileBillingAddress