@pradip1995/segment-login-template 0.5.25 → 0.5.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/account-addresses.tsx +287 -287
- package/src/account-guest-orders.tsx +98 -98
- package/src/account-orders.tsx +260 -260
- package/src/account-overview.tsx +133 -133
- package/src/account-payment-methods.tsx +328 -328
- package/src/account-profile-logic.ts +282 -282
- package/src/account-profile.tsx +111 -111
- package/src/account-theme.ts +91 -91
- package/src/account-utils.ts +20 -20
- package/src/auth-server.ts +442 -442
- package/src/commerce-auth-otp-modal.css +125 -125
- package/src/commerce-auth-otp-modal.tsx +121 -121
- package/src/forgot-password-form.tsx +109 -109
- package/src/google-auth-section.tsx +22 -22
- package/src/google-sign-in-section.tsx +67 -67
- package/src/guest-order-modal.tsx +215 -215
- package/src/index.ts +7 -7
- package/src/is-next-redirect.ts +60 -60
- package/src/login-brand-panel.tsx +6 -5
- package/src/login-form.tsx +469 -469
- package/src/manifest.ts +11 -11
- package/src/order-history-status.test.ts +224 -224
- package/src/order-history-status.ts +244 -244
- package/src/otp-component.ts +14 -14
- package/src/otp-input.tsx +46 -46
- package/src/otp-unified-form.tsx +333 -333
- package/src/otp-verification-modal.tsx +112 -112
- package/src/payment-methods-actions.ts +243 -243
- package/src/profile-email-field.tsx +116 -116
- package/src/profile-email.test.ts +182 -182
- package/src/profile-email.ts +54 -54
- package/src/profile-phone-field.tsx +119 -119
- package/src/register-form.tsx +339 -339
- package/src/reorder-checkout-address.ts +101 -101
- package/src/reset-password-form.tsx +103 -103
- package/src/reset-password-page.tsx +12 -12
- package/src/segment.tsx +272 -272
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pradip1995/segment-login-template",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.27",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@pradip1995/commerce-auth": "^4.0.0",
|
|
39
39
|
"@pradip1995/commerce-core": "^4.0.0",
|
|
40
|
-
"@pradip1995/segment-primitives": "^0.4.
|
|
40
|
+
"@pradip1995/segment-primitives": "^0.4.15",
|
|
41
41
|
"@pradip1995/segment-tokens": "^0.3.19"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
@@ -1,287 +1,287 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import { useActionState, useState } from "react"
|
|
4
|
-
import type { HttpTypes } from "@medusajs/types"
|
|
5
|
-
import {
|
|
6
|
-
addCustomerAddress,
|
|
7
|
-
deleteCustomerAddress,
|
|
8
|
-
setDefaultAddress,
|
|
9
|
-
updateCustomerAddress,
|
|
10
|
-
} from "@pradip1995/commerce-core/data/customer"
|
|
11
|
-
import { acct } from "./account-theme"
|
|
12
|
-
|
|
13
|
-
export default function AccountAddresses({
|
|
14
|
-
customer,
|
|
15
|
-
countryCode,
|
|
16
|
-
}: {
|
|
17
|
-
customer: HttpTypes.StoreCustomer
|
|
18
|
-
countryCode: string
|
|
19
|
-
}) {
|
|
20
|
-
const addresses = customer.addresses ?? []
|
|
21
|
-
const [editingId, setEditingId] = useState<string | null>(null)
|
|
22
|
-
const [showAdd, setShowAdd] = useState(false)
|
|
23
|
-
const hasDefault = addresses.some((a) => a.is_default_shipping || a.is_default_billing)
|
|
24
|
-
|
|
25
|
-
return (
|
|
26
|
-
<div className="space-y-8">
|
|
27
|
-
<div className="flex flex-wrap items-end justify-between gap-4">
|
|
28
|
-
<div>
|
|
29
|
-
<h2 className={acct.title}>Saved addresses</h2>
|
|
30
|
-
<p className={`text-sm ${acct.muted}`}>Manage shipping and billing addresses.</p>
|
|
31
|
-
</div>
|
|
32
|
-
<button type="button" onClick={() => setShowAdd(true)} className={acct.btnPrimary}>
|
|
33
|
-
Add address
|
|
34
|
-
</button>
|
|
35
|
-
</div>
|
|
36
|
-
|
|
37
|
-
{addresses.length === 0 && !showAdd && (
|
|
38
|
-
<div className={`${acct.cardMuted} p-8 text-center text-sm ${acct.muted}`}>
|
|
39
|
-
No saved addresses yet.
|
|
40
|
-
</div>
|
|
41
|
-
)}
|
|
42
|
-
|
|
43
|
-
<ul className="space-y-4">
|
|
44
|
-
{addresses.map((address) => (
|
|
45
|
-
<li key={address.id} className={`${acct.card} p-5`}>
|
|
46
|
-
{editingId === address.id ? (
|
|
47
|
-
<AddressForm
|
|
48
|
-
countryCode={countryCode}
|
|
49
|
-
address={address}
|
|
50
|
-
addressId={address.id}
|
|
51
|
-
defaultChecked={!!(address.is_default_shipping || address.is_default_billing)}
|
|
52
|
-
onDone={() => setEditingId(null)}
|
|
53
|
-
onCancel={() => setEditingId(null)}
|
|
54
|
-
/>
|
|
55
|
-
) : (
|
|
56
|
-
<div className="flex flex-wrap justify-between gap-4">
|
|
57
|
-
<div className="text-sm text-body space-y-1">
|
|
58
|
-
<p className="font-semibold text-heading">
|
|
59
|
-
{[address.first_name, address.last_name].filter(Boolean).join(" ")}
|
|
60
|
-
</p>
|
|
61
|
-
<p>{address.address_1}</p>
|
|
62
|
-
{address.address_2 && <p>{address.address_2}</p>}
|
|
63
|
-
<p>
|
|
64
|
-
{[address.city, address.province, address.postal_code].filter(Boolean).join(", ")}
|
|
65
|
-
</p>
|
|
66
|
-
<p className="uppercase text-xs text-muted">{address.country_code}</p>
|
|
67
|
-
{address.phone && <p>{address.phone}</p>}
|
|
68
|
-
{(address.is_default_shipping || address.is_default_billing) && (
|
|
69
|
-
<p className="text-xs text-brand-accent font-medium pt-1">
|
|
70
|
-
{address.is_default_shipping && address.is_default_billing
|
|
71
|
-
? "Default shipping & billing"
|
|
72
|
-
: address.is_default_shipping
|
|
73
|
-
? "Default shipping"
|
|
74
|
-
: "Default billing"}
|
|
75
|
-
</p>
|
|
76
|
-
)}
|
|
77
|
-
</div>
|
|
78
|
-
<div className="flex flex-col sm:flex-row gap-2 shrink-0 items-end sm:items-center">
|
|
79
|
-
{!address.is_default_shipping && (
|
|
80
|
-
<MakeDefaultButton addressId={address.id!} />
|
|
81
|
-
)}
|
|
82
|
-
<button
|
|
83
|
-
type="button"
|
|
84
|
-
onClick={() => setEditingId(address.id!)}
|
|
85
|
-
className={`${acct.btnOutline} !py-2 !px-3`}
|
|
86
|
-
>
|
|
87
|
-
Edit
|
|
88
|
-
</button>
|
|
89
|
-
<DeleteAddressButton addressId={address.id!} />
|
|
90
|
-
</div>
|
|
91
|
-
</div>
|
|
92
|
-
)}
|
|
93
|
-
</li>
|
|
94
|
-
))}
|
|
95
|
-
</ul>
|
|
96
|
-
|
|
97
|
-
{showAdd && (
|
|
98
|
-
<div className={`${acct.card} p-5`}>
|
|
99
|
-
<h3 className="text-sm font-semibold text-heading mb-4">New address</h3>
|
|
100
|
-
<AddressForm
|
|
101
|
-
countryCode={countryCode}
|
|
102
|
-
defaultChecked={addresses.length === 0 || !hasDefault}
|
|
103
|
-
onDone={() => setShowAdd(false)}
|
|
104
|
-
onCancel={() => setShowAdd(false)}
|
|
105
|
-
/>
|
|
106
|
-
</div>
|
|
107
|
-
)}
|
|
108
|
-
</div>
|
|
109
|
-
)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function AddressForm({
|
|
113
|
-
countryCode,
|
|
114
|
-
address,
|
|
115
|
-
addressId,
|
|
116
|
-
defaultChecked = false,
|
|
117
|
-
onDone,
|
|
118
|
-
onCancel,
|
|
119
|
-
}: {
|
|
120
|
-
countryCode: string
|
|
121
|
-
address?: HttpTypes.StoreCustomerAddress
|
|
122
|
-
addressId?: string
|
|
123
|
-
defaultChecked?: boolean
|
|
124
|
-
onDone: () => void
|
|
125
|
-
onCancel: () => void
|
|
126
|
-
}) {
|
|
127
|
-
const action = addressId ? updateCustomerAddress : addCustomerAddress
|
|
128
|
-
const [state, formAction, pending] = useActionState(
|
|
129
|
-
async (prev: Record<string, unknown>, formData: FormData) => {
|
|
130
|
-
const makeDefault = formData.get("is_default") === "on"
|
|
131
|
-
const result = await action(
|
|
132
|
-
addressId
|
|
133
|
-
? { ...prev, addressId }
|
|
134
|
-
: {
|
|
135
|
-
...prev,
|
|
136
|
-
isDefaultShipping: makeDefault,
|
|
137
|
-
isDefaultBilling: makeDefault,
|
|
138
|
-
},
|
|
139
|
-
formData
|
|
140
|
-
)
|
|
141
|
-
if (result?.success) {
|
|
142
|
-
if (addressId && makeDefault && !address?.is_default_shipping) {
|
|
143
|
-
const defaultResult = await setDefaultAddress(addressId)
|
|
144
|
-
if (!defaultResult?.success) {
|
|
145
|
-
return {
|
|
146
|
-
success: false,
|
|
147
|
-
error: defaultResult?.error || "Failed to set default address",
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
onDone()
|
|
152
|
-
window.location.reload()
|
|
153
|
-
}
|
|
154
|
-
return result
|
|
155
|
-
},
|
|
156
|
-
{ success: false, error: null as string | null }
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
return (
|
|
160
|
-
<form action={formAction} className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
161
|
-
<Field label="First name" name="first_name" defaultValue={address?.first_name || ""} />
|
|
162
|
-
<Field label="Last name" name="last_name" defaultValue={address?.last_name || ""} />
|
|
163
|
-
<Field label="Address line 1" name="address_1" className="sm:col-span-2" defaultValue={address?.address_1 || ""} />
|
|
164
|
-
<Field label="Address line 2" name="address_2" className="sm:col-span-2" defaultValue={address?.address_2 || ""} />
|
|
165
|
-
<Field label="City" name="city" defaultValue={address?.city || ""} />
|
|
166
|
-
<Field label="State / Province" name="province" defaultValue={address?.province || ""} />
|
|
167
|
-
<Field label="Postal code" name="postal_code" defaultValue={address?.postal_code || ""} />
|
|
168
|
-
<Field
|
|
169
|
-
label="Country code"
|
|
170
|
-
name="country_code"
|
|
171
|
-
defaultValue={address?.country_code || countryCode}
|
|
172
|
-
/>
|
|
173
|
-
<Field label="Phone" name="phone" type="tel" defaultValue={address?.phone || ""} />
|
|
174
|
-
<label className="sm:col-span-2 flex items-center gap-2 text-sm text-body cursor-pointer">
|
|
175
|
-
<input
|
|
176
|
-
type="checkbox"
|
|
177
|
-
name="is_default"
|
|
178
|
-
defaultChecked={defaultChecked}
|
|
179
|
-
className="size-4 accent-[var(--color-brand-accent,#7c3aed)]"
|
|
180
|
-
/>
|
|
181
|
-
<span>Set as default shipping address</span>
|
|
182
|
-
</label>
|
|
183
|
-
{state?.error && (
|
|
184
|
-
<p className="sm:col-span-2 text-sm text-red-600">{String(state.error)}</p>
|
|
185
|
-
)}
|
|
186
|
-
<div className="sm:col-span-2 flex gap-3">
|
|
187
|
-
<button type="submit" disabled={pending} className={acct.btnPrimary}>
|
|
188
|
-
{pending ? "Saving…" : "Save address"}
|
|
189
|
-
</button>
|
|
190
|
-
<button type="button" onClick={onCancel} className={acct.btnOutline}>
|
|
191
|
-
Cancel
|
|
192
|
-
</button>
|
|
193
|
-
</div>
|
|
194
|
-
</form>
|
|
195
|
-
)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function MakeDefaultButton({ addressId }: { addressId: string }) {
|
|
199
|
-
const [pending, setPending] = useState(false)
|
|
200
|
-
const [error, setError] = useState<string | null>(null)
|
|
201
|
-
|
|
202
|
-
async function handleDefault() {
|
|
203
|
-
setPending(true)
|
|
204
|
-
setError(null)
|
|
205
|
-
try {
|
|
206
|
-
const result = await setDefaultAddress(addressId)
|
|
207
|
-
if (!result?.success) {
|
|
208
|
-
setError(result?.error || "Failed to set default address")
|
|
209
|
-
setPending(false)
|
|
210
|
-
return
|
|
211
|
-
}
|
|
212
|
-
window.location.reload()
|
|
213
|
-
} catch (err) {
|
|
214
|
-
setError(err instanceof Error ? err.message : "Failed to set default address")
|
|
215
|
-
setPending(false)
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return (
|
|
220
|
-
<div className="flex flex-col items-end gap-1">
|
|
221
|
-
<button
|
|
222
|
-
type="button"
|
|
223
|
-
onClick={handleDefault}
|
|
224
|
-
disabled={pending}
|
|
225
|
-
className="text-xs text-brand-accent hover:underline disabled:opacity-60"
|
|
226
|
-
>
|
|
227
|
-
{pending ? "Setting…" : "Make default"}
|
|
228
|
-
</button>
|
|
229
|
-
{error && <p className="text-xs text-red-600 max-w-[12rem] text-right">{error}</p>}
|
|
230
|
-
</div>
|
|
231
|
-
)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function DeleteAddressButton({ addressId }: { addressId: string }) {
|
|
235
|
-
const [pending, setPending] = useState(false)
|
|
236
|
-
|
|
237
|
-
async function handleDelete() {
|
|
238
|
-
if (!confirm("Delete this address?")) return
|
|
239
|
-
setPending(true)
|
|
240
|
-
try {
|
|
241
|
-
await deleteCustomerAddress(addressId)
|
|
242
|
-
window.location.reload()
|
|
243
|
-
} finally {
|
|
244
|
-
setPending(false)
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return (
|
|
249
|
-
<button
|
|
250
|
-
type="button"
|
|
251
|
-
onClick={handleDelete}
|
|
252
|
-
disabled={pending}
|
|
253
|
-
className="text-xs text-red-600 hover:underline disabled:opacity-60"
|
|
254
|
-
>
|
|
255
|
-
{pending ? "Deleting…" : "Delete"}
|
|
256
|
-
</button>
|
|
257
|
-
)
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function Field({
|
|
261
|
-
label,
|
|
262
|
-
name,
|
|
263
|
-
type = "text",
|
|
264
|
-
defaultValue,
|
|
265
|
-
className = "",
|
|
266
|
-
}: {
|
|
267
|
-
label: string
|
|
268
|
-
name: string
|
|
269
|
-
type?: string
|
|
270
|
-
defaultValue?: string
|
|
271
|
-
className?: string
|
|
272
|
-
}) {
|
|
273
|
-
return (
|
|
274
|
-
<label className={`block ${className}`}>
|
|
275
|
-
<span className="text-xs font-semibold uppercase tracking-wider text-muted mb-1.5 block">
|
|
276
|
-
{label}
|
|
277
|
-
</span>
|
|
278
|
-
<input
|
|
279
|
-
name={name}
|
|
280
|
-
type={type}
|
|
281
|
-
defaultValue={defaultValue}
|
|
282
|
-
required={name !== "address_2"}
|
|
283
|
-
className="w-full border border-cart-border rounded-none px-3 py-2.5 text-sm bg-surface text-heading focus:outline-none focus:border-brand-accent"
|
|
284
|
-
/>
|
|
285
|
-
</label>
|
|
286
|
-
)
|
|
287
|
-
}
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useActionState, useState } from "react"
|
|
4
|
+
import type { HttpTypes } from "@medusajs/types"
|
|
5
|
+
import {
|
|
6
|
+
addCustomerAddress,
|
|
7
|
+
deleteCustomerAddress,
|
|
8
|
+
setDefaultAddress,
|
|
9
|
+
updateCustomerAddress,
|
|
10
|
+
} from "@pradip1995/commerce-core/data/customer"
|
|
11
|
+
import { acct } from "./account-theme"
|
|
12
|
+
|
|
13
|
+
export default function AccountAddresses({
|
|
14
|
+
customer,
|
|
15
|
+
countryCode,
|
|
16
|
+
}: {
|
|
17
|
+
customer: HttpTypes.StoreCustomer
|
|
18
|
+
countryCode: string
|
|
19
|
+
}) {
|
|
20
|
+
const addresses = customer.addresses ?? []
|
|
21
|
+
const [editingId, setEditingId] = useState<string | null>(null)
|
|
22
|
+
const [showAdd, setShowAdd] = useState(false)
|
|
23
|
+
const hasDefault = addresses.some((a) => a.is_default_shipping || a.is_default_billing)
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div className="space-y-8">
|
|
27
|
+
<div className="flex flex-wrap items-end justify-between gap-4">
|
|
28
|
+
<div>
|
|
29
|
+
<h2 className={acct.title}>Saved addresses</h2>
|
|
30
|
+
<p className={`text-sm ${acct.muted}`}>Manage shipping and billing addresses.</p>
|
|
31
|
+
</div>
|
|
32
|
+
<button type="button" onClick={() => setShowAdd(true)} className={acct.btnPrimary}>
|
|
33
|
+
Add address
|
|
34
|
+
</button>
|
|
35
|
+
</div>
|
|
36
|
+
|
|
37
|
+
{addresses.length === 0 && !showAdd && (
|
|
38
|
+
<div className={`${acct.cardMuted} p-8 text-center text-sm ${acct.muted}`}>
|
|
39
|
+
No saved addresses yet.
|
|
40
|
+
</div>
|
|
41
|
+
)}
|
|
42
|
+
|
|
43
|
+
<ul className="space-y-4">
|
|
44
|
+
{addresses.map((address) => (
|
|
45
|
+
<li key={address.id} className={`${acct.card} p-5`}>
|
|
46
|
+
{editingId === address.id ? (
|
|
47
|
+
<AddressForm
|
|
48
|
+
countryCode={countryCode}
|
|
49
|
+
address={address}
|
|
50
|
+
addressId={address.id}
|
|
51
|
+
defaultChecked={!!(address.is_default_shipping || address.is_default_billing)}
|
|
52
|
+
onDone={() => setEditingId(null)}
|
|
53
|
+
onCancel={() => setEditingId(null)}
|
|
54
|
+
/>
|
|
55
|
+
) : (
|
|
56
|
+
<div className="flex flex-wrap justify-between gap-4">
|
|
57
|
+
<div className="text-sm text-body space-y-1">
|
|
58
|
+
<p className="font-semibold text-heading">
|
|
59
|
+
{[address.first_name, address.last_name].filter(Boolean).join(" ")}
|
|
60
|
+
</p>
|
|
61
|
+
<p>{address.address_1}</p>
|
|
62
|
+
{address.address_2 && <p>{address.address_2}</p>}
|
|
63
|
+
<p>
|
|
64
|
+
{[address.city, address.province, address.postal_code].filter(Boolean).join(", ")}
|
|
65
|
+
</p>
|
|
66
|
+
<p className="uppercase text-xs text-muted">{address.country_code}</p>
|
|
67
|
+
{address.phone && <p>{address.phone}</p>}
|
|
68
|
+
{(address.is_default_shipping || address.is_default_billing) && (
|
|
69
|
+
<p className="text-xs text-brand-accent font-medium pt-1">
|
|
70
|
+
{address.is_default_shipping && address.is_default_billing
|
|
71
|
+
? "Default shipping & billing"
|
|
72
|
+
: address.is_default_shipping
|
|
73
|
+
? "Default shipping"
|
|
74
|
+
: "Default billing"}
|
|
75
|
+
</p>
|
|
76
|
+
)}
|
|
77
|
+
</div>
|
|
78
|
+
<div className="flex flex-col sm:flex-row gap-2 shrink-0 items-end sm:items-center">
|
|
79
|
+
{!address.is_default_shipping && (
|
|
80
|
+
<MakeDefaultButton addressId={address.id!} />
|
|
81
|
+
)}
|
|
82
|
+
<button
|
|
83
|
+
type="button"
|
|
84
|
+
onClick={() => setEditingId(address.id!)}
|
|
85
|
+
className={`${acct.btnOutline} !py-2 !px-3`}
|
|
86
|
+
>
|
|
87
|
+
Edit
|
|
88
|
+
</button>
|
|
89
|
+
<DeleteAddressButton addressId={address.id!} />
|
|
90
|
+
</div>
|
|
91
|
+
</div>
|
|
92
|
+
)}
|
|
93
|
+
</li>
|
|
94
|
+
))}
|
|
95
|
+
</ul>
|
|
96
|
+
|
|
97
|
+
{showAdd && (
|
|
98
|
+
<div className={`${acct.card} p-5`}>
|
|
99
|
+
<h3 className="text-sm font-semibold text-heading mb-4">New address</h3>
|
|
100
|
+
<AddressForm
|
|
101
|
+
countryCode={countryCode}
|
|
102
|
+
defaultChecked={addresses.length === 0 || !hasDefault}
|
|
103
|
+
onDone={() => setShowAdd(false)}
|
|
104
|
+
onCancel={() => setShowAdd(false)}
|
|
105
|
+
/>
|
|
106
|
+
</div>
|
|
107
|
+
)}
|
|
108
|
+
</div>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function AddressForm({
|
|
113
|
+
countryCode,
|
|
114
|
+
address,
|
|
115
|
+
addressId,
|
|
116
|
+
defaultChecked = false,
|
|
117
|
+
onDone,
|
|
118
|
+
onCancel,
|
|
119
|
+
}: {
|
|
120
|
+
countryCode: string
|
|
121
|
+
address?: HttpTypes.StoreCustomerAddress
|
|
122
|
+
addressId?: string
|
|
123
|
+
defaultChecked?: boolean
|
|
124
|
+
onDone: () => void
|
|
125
|
+
onCancel: () => void
|
|
126
|
+
}) {
|
|
127
|
+
const action = addressId ? updateCustomerAddress : addCustomerAddress
|
|
128
|
+
const [state, formAction, pending] = useActionState(
|
|
129
|
+
async (prev: Record<string, unknown>, formData: FormData) => {
|
|
130
|
+
const makeDefault = formData.get("is_default") === "on"
|
|
131
|
+
const result = await action(
|
|
132
|
+
addressId
|
|
133
|
+
? { ...prev, addressId }
|
|
134
|
+
: {
|
|
135
|
+
...prev,
|
|
136
|
+
isDefaultShipping: makeDefault,
|
|
137
|
+
isDefaultBilling: makeDefault,
|
|
138
|
+
},
|
|
139
|
+
formData
|
|
140
|
+
)
|
|
141
|
+
if (result?.success) {
|
|
142
|
+
if (addressId && makeDefault && !address?.is_default_shipping) {
|
|
143
|
+
const defaultResult = await setDefaultAddress(addressId)
|
|
144
|
+
if (!defaultResult?.success) {
|
|
145
|
+
return {
|
|
146
|
+
success: false,
|
|
147
|
+
error: defaultResult?.error || "Failed to set default address",
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
onDone()
|
|
152
|
+
window.location.reload()
|
|
153
|
+
}
|
|
154
|
+
return result
|
|
155
|
+
},
|
|
156
|
+
{ success: false, error: null as string | null }
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<form action={formAction} className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
161
|
+
<Field label="First name" name="first_name" defaultValue={address?.first_name || ""} />
|
|
162
|
+
<Field label="Last name" name="last_name" defaultValue={address?.last_name || ""} />
|
|
163
|
+
<Field label="Address line 1" name="address_1" className="sm:col-span-2" defaultValue={address?.address_1 || ""} />
|
|
164
|
+
<Field label="Address line 2" name="address_2" className="sm:col-span-2" defaultValue={address?.address_2 || ""} />
|
|
165
|
+
<Field label="City" name="city" defaultValue={address?.city || ""} />
|
|
166
|
+
<Field label="State / Province" name="province" defaultValue={address?.province || ""} />
|
|
167
|
+
<Field label="Postal code" name="postal_code" defaultValue={address?.postal_code || ""} />
|
|
168
|
+
<Field
|
|
169
|
+
label="Country code"
|
|
170
|
+
name="country_code"
|
|
171
|
+
defaultValue={address?.country_code || countryCode}
|
|
172
|
+
/>
|
|
173
|
+
<Field label="Phone" name="phone" type="tel" defaultValue={address?.phone || ""} />
|
|
174
|
+
<label className="sm:col-span-2 flex items-center gap-2 text-sm text-body cursor-pointer">
|
|
175
|
+
<input
|
|
176
|
+
type="checkbox"
|
|
177
|
+
name="is_default"
|
|
178
|
+
defaultChecked={defaultChecked}
|
|
179
|
+
className="size-4 accent-[var(--color-brand-accent,#7c3aed)]"
|
|
180
|
+
/>
|
|
181
|
+
<span>Set as default shipping address</span>
|
|
182
|
+
</label>
|
|
183
|
+
{state?.error && (
|
|
184
|
+
<p className="sm:col-span-2 text-sm text-red-600">{String(state.error)}</p>
|
|
185
|
+
)}
|
|
186
|
+
<div className="sm:col-span-2 flex gap-3">
|
|
187
|
+
<button type="submit" disabled={pending} className={acct.btnPrimary}>
|
|
188
|
+
{pending ? "Saving…" : "Save address"}
|
|
189
|
+
</button>
|
|
190
|
+
<button type="button" onClick={onCancel} className={acct.btnOutline}>
|
|
191
|
+
Cancel
|
|
192
|
+
</button>
|
|
193
|
+
</div>
|
|
194
|
+
</form>
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function MakeDefaultButton({ addressId }: { addressId: string }) {
|
|
199
|
+
const [pending, setPending] = useState(false)
|
|
200
|
+
const [error, setError] = useState<string | null>(null)
|
|
201
|
+
|
|
202
|
+
async function handleDefault() {
|
|
203
|
+
setPending(true)
|
|
204
|
+
setError(null)
|
|
205
|
+
try {
|
|
206
|
+
const result = await setDefaultAddress(addressId)
|
|
207
|
+
if (!result?.success) {
|
|
208
|
+
setError(result?.error || "Failed to set default address")
|
|
209
|
+
setPending(false)
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
window.location.reload()
|
|
213
|
+
} catch (err) {
|
|
214
|
+
setError(err instanceof Error ? err.message : "Failed to set default address")
|
|
215
|
+
setPending(false)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return (
|
|
220
|
+
<div className="flex flex-col items-end gap-1">
|
|
221
|
+
<button
|
|
222
|
+
type="button"
|
|
223
|
+
onClick={handleDefault}
|
|
224
|
+
disabled={pending}
|
|
225
|
+
className="text-xs text-brand-accent hover:underline disabled:opacity-60"
|
|
226
|
+
>
|
|
227
|
+
{pending ? "Setting…" : "Make default"}
|
|
228
|
+
</button>
|
|
229
|
+
{error && <p className="text-xs text-red-600 max-w-[12rem] text-right">{error}</p>}
|
|
230
|
+
</div>
|
|
231
|
+
)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function DeleteAddressButton({ addressId }: { addressId: string }) {
|
|
235
|
+
const [pending, setPending] = useState(false)
|
|
236
|
+
|
|
237
|
+
async function handleDelete() {
|
|
238
|
+
if (!confirm("Delete this address?")) return
|
|
239
|
+
setPending(true)
|
|
240
|
+
try {
|
|
241
|
+
await deleteCustomerAddress(addressId)
|
|
242
|
+
window.location.reload()
|
|
243
|
+
} finally {
|
|
244
|
+
setPending(false)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return (
|
|
249
|
+
<button
|
|
250
|
+
type="button"
|
|
251
|
+
onClick={handleDelete}
|
|
252
|
+
disabled={pending}
|
|
253
|
+
className="text-xs text-red-600 hover:underline disabled:opacity-60"
|
|
254
|
+
>
|
|
255
|
+
{pending ? "Deleting…" : "Delete"}
|
|
256
|
+
</button>
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function Field({
|
|
261
|
+
label,
|
|
262
|
+
name,
|
|
263
|
+
type = "text",
|
|
264
|
+
defaultValue,
|
|
265
|
+
className = "",
|
|
266
|
+
}: {
|
|
267
|
+
label: string
|
|
268
|
+
name: string
|
|
269
|
+
type?: string
|
|
270
|
+
defaultValue?: string
|
|
271
|
+
className?: string
|
|
272
|
+
}) {
|
|
273
|
+
return (
|
|
274
|
+
<label className={`block ${className}`}>
|
|
275
|
+
<span className="text-xs font-semibold uppercase tracking-wider text-muted mb-1.5 block">
|
|
276
|
+
{label}
|
|
277
|
+
</span>
|
|
278
|
+
<input
|
|
279
|
+
name={name}
|
|
280
|
+
type={type}
|
|
281
|
+
defaultValue={defaultValue}
|
|
282
|
+
required={name !== "address_2"}
|
|
283
|
+
className="w-full border border-cart-border rounded-none px-3 py-2.5 text-sm bg-surface text-heading focus:outline-none focus:border-brand-accent"
|
|
284
|
+
/>
|
|
285
|
+
</label>
|
|
286
|
+
)
|
|
287
|
+
}
|