@pradip1995/segment-jewelry-account-pages 0.1.1 → 0.1.2
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 +3 -2
- package/src/address-actions.ts +26 -0
- package/src/default-address.test.ts +186 -0
- package/src/default-address.ts +47 -0
- package/src/modern-account-addresses.tsx +44 -12
- package/src/modern-account-profile.tsx +96 -20
- package/src/modern-account.css +44 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pradip1995/segment-jewelry-account-pages",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"typecheck": "tsc --noEmit",
|
|
43
|
-
"lint": "tsc --noEmit"
|
|
43
|
+
"lint": "tsc --noEmit",
|
|
44
|
+
"test": "node --experimental-strip-types --test src/default-address.test.ts"
|
|
44
45
|
}
|
|
45
46
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use server"
|
|
2
|
+
|
|
3
|
+
import { setDefaultAddress } from "@pradip1995/commerce-core/data/customer"
|
|
4
|
+
|
|
5
|
+
/** Sets this address as the default shipping address. Medusa unsets the previous shipping default. */
|
|
6
|
+
export async function setExclusiveDefaultAddress(addressId: string) {
|
|
7
|
+
if (!addressId) {
|
|
8
|
+
return { success: false, error: "Address id is required" }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const result = await setDefaultAddress(addressId)
|
|
13
|
+
if (!result?.success) {
|
|
14
|
+
return {
|
|
15
|
+
success: false,
|
|
16
|
+
error: result?.error || "Failed to set default address",
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return { success: true, error: null }
|
|
20
|
+
} catch (err) {
|
|
21
|
+
return {
|
|
22
|
+
success: false,
|
|
23
|
+
error: err instanceof Error ? err.message : "Failed to set default address",
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import {
|
|
4
|
+
addressTypeFromMetadata,
|
|
5
|
+
isExclusiveDefaultAddress,
|
|
6
|
+
resolveAddressType,
|
|
7
|
+
resolveDefaultAddressId,
|
|
8
|
+
sortAddressesDefaultFirst,
|
|
9
|
+
} from "./default-address.ts"
|
|
10
|
+
|
|
11
|
+
describe("resolveDefaultAddressId", () => {
|
|
12
|
+
const cases: Array<{
|
|
13
|
+
name: string
|
|
14
|
+
addresses: Parameters<typeof resolveDefaultAddressId>[0]
|
|
15
|
+
expected: string | null
|
|
16
|
+
}> = [
|
|
17
|
+
{ name: "empty list", addresses: [], expected: null },
|
|
18
|
+
{
|
|
19
|
+
name: "no default flags",
|
|
20
|
+
addresses: [
|
|
21
|
+
{ id: "a1" },
|
|
22
|
+
{ id: "a2", is_default_shipping: false, is_default_billing: false },
|
|
23
|
+
],
|
|
24
|
+
expected: null,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: "only shipping default",
|
|
28
|
+
addresses: [
|
|
29
|
+
{ id: "a1", is_default_shipping: true },
|
|
30
|
+
{ id: "a2", is_default_billing: false },
|
|
31
|
+
],
|
|
32
|
+
expected: "a1",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "only billing default",
|
|
36
|
+
addresses: [
|
|
37
|
+
{ id: "a1", is_default_billing: true },
|
|
38
|
+
{ id: "a2" },
|
|
39
|
+
],
|
|
40
|
+
expected: "a1",
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "shipping wins when both addresses claim default",
|
|
44
|
+
addresses: [
|
|
45
|
+
{ id: "ship", is_default_shipping: true, is_default_billing: false },
|
|
46
|
+
{ id: "bill", is_default_shipping: false, is_default_billing: true },
|
|
47
|
+
],
|
|
48
|
+
expected: "ship",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: "same address shipping and billing",
|
|
52
|
+
addresses: [
|
|
53
|
+
{ id: "only", is_default_shipping: true, is_default_billing: true },
|
|
54
|
+
{ id: "other" },
|
|
55
|
+
],
|
|
56
|
+
expected: "only",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "ignores default without id",
|
|
60
|
+
addresses: [{ is_default_shipping: true }, { id: "a2", is_default_billing: true }],
|
|
61
|
+
expected: "a2",
|
|
62
|
+
},
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
for (const row of cases) {
|
|
66
|
+
it(row.name, () => {
|
|
67
|
+
assert.equal(resolveDefaultAddressId(row.addresses), row.expected)
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe("isExclusiveDefaultAddress", () => {
|
|
73
|
+
const cases: Array<{
|
|
74
|
+
name: string
|
|
75
|
+
address: { id?: string | null }
|
|
76
|
+
defaultAddressId: string | null
|
|
77
|
+
expected: boolean
|
|
78
|
+
}> = [
|
|
79
|
+
{ name: "match", address: { id: "a1" }, defaultAddressId: "a1", expected: true },
|
|
80
|
+
{ name: "other address", address: { id: "a2" }, defaultAddressId: "a1", expected: false },
|
|
81
|
+
{ name: "no default selected", address: { id: "a1" }, defaultAddressId: null, expected: false },
|
|
82
|
+
{ name: "missing id", address: {}, defaultAddressId: "a1", expected: false },
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
for (const row of cases) {
|
|
86
|
+
it(row.name, () => {
|
|
87
|
+
assert.equal(
|
|
88
|
+
isExclusiveDefaultAddress(row.address, row.defaultAddressId),
|
|
89
|
+
row.expected
|
|
90
|
+
)
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe("sortAddressesDefaultFirst", () => {
|
|
96
|
+
const cases: Array<{
|
|
97
|
+
name: string
|
|
98
|
+
addresses: Array<{ id: string; is_default_shipping?: boolean; is_default_billing?: boolean }>
|
|
99
|
+
expected: string[]
|
|
100
|
+
}> = [
|
|
101
|
+
{
|
|
102
|
+
name: "empty list",
|
|
103
|
+
addresses: [],
|
|
104
|
+
expected: [],
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: "no default keeps order",
|
|
108
|
+
addresses: [{ id: "a" }, { id: "b" }, { id: "c" }],
|
|
109
|
+
expected: ["a", "b", "c"],
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: "shipping default moves to top",
|
|
113
|
+
addresses: [
|
|
114
|
+
{ id: "a" },
|
|
115
|
+
{ id: "b", is_default_shipping: true },
|
|
116
|
+
{ id: "c" },
|
|
117
|
+
],
|
|
118
|
+
expected: ["b", "a", "c"],
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: "billing default used when no shipping default",
|
|
122
|
+
addresses: [
|
|
123
|
+
{ id: "a" },
|
|
124
|
+
{ id: "b", is_default_billing: true },
|
|
125
|
+
],
|
|
126
|
+
expected: ["b", "a"],
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: "shipping wins if both flags exist on different addresses",
|
|
130
|
+
addresses: [
|
|
131
|
+
{ id: "bill", is_default_billing: true },
|
|
132
|
+
{ id: "ship", is_default_shipping: true },
|
|
133
|
+
],
|
|
134
|
+
expected: ["ship", "bill"],
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: "already first stays first",
|
|
138
|
+
addresses: [{ id: "def", is_default_shipping: true }, { id: "other" }],
|
|
139
|
+
expected: ["def", "other"],
|
|
140
|
+
},
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
for (const row of cases) {
|
|
144
|
+
it(row.name, () => {
|
|
145
|
+
assert.deepEqual(
|
|
146
|
+
sortAddressesDefaultFirst(row.addresses).map((address) => address.id),
|
|
147
|
+
row.expected
|
|
148
|
+
)
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe("resolveAddressType", () => {
|
|
154
|
+
const cases: Array<{ name: string; value: unknown; expected: "HOME" | "OFFICE" }> = [
|
|
155
|
+
{ name: "empty string", value: "", expected: "HOME" },
|
|
156
|
+
{ name: "null", value: null, expected: "HOME" },
|
|
157
|
+
{ name: "undefined", value: undefined, expected: "HOME" },
|
|
158
|
+
{ name: "home lowercase", value: "home", expected: "HOME" },
|
|
159
|
+
{ name: "HOME uppercase", value: "HOME", expected: "HOME" },
|
|
160
|
+
{ name: "office lowercase", value: "office", expected: "OFFICE" },
|
|
161
|
+
{ name: "OFFICE uppercase", value: "OFFICE", expected: "OFFICE" },
|
|
162
|
+
{ name: "Office mixed", value: " Office ", expected: "OFFICE" },
|
|
163
|
+
{ name: "unknown falls back to home", value: "other", expected: "HOME" },
|
|
164
|
+
]
|
|
165
|
+
|
|
166
|
+
for (const row of cases) {
|
|
167
|
+
it(row.name, () => {
|
|
168
|
+
assert.equal(resolveAddressType(row.value), row.expected)
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
describe("addressTypeFromMetadata", () => {
|
|
174
|
+
const cases: Array<{ name: string; metadata: unknown; expected: "HOME" | "OFFICE" }> = [
|
|
175
|
+
{ name: "missing metadata", metadata: null, expected: "HOME" },
|
|
176
|
+
{ name: "empty object", metadata: {}, expected: "HOME" },
|
|
177
|
+
{ name: "home type", metadata: { address_type: "HOME" }, expected: "HOME" },
|
|
178
|
+
{ name: "office type", metadata: { address_type: "office" }, expected: "OFFICE" },
|
|
179
|
+
]
|
|
180
|
+
|
|
181
|
+
for (const row of cases) {
|
|
182
|
+
it(row.name, () => {
|
|
183
|
+
assert.equal(addressTypeFromMetadata(row.metadata), row.expected)
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type DefaultAddressFlags = {
|
|
2
|
+
id?: string | null
|
|
3
|
+
is_default_shipping?: boolean | null
|
|
4
|
+
is_default_billing?: boolean | null
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const ADDRESS_TYPES = ["HOME", "OFFICE"] as const
|
|
8
|
+
export type AddressType = (typeof ADDRESS_TYPES)[number]
|
|
9
|
+
|
|
10
|
+
/** One default only: shipping wins, then billing. */
|
|
11
|
+
export function resolveDefaultAddressId(
|
|
12
|
+
addresses: DefaultAddressFlags[]
|
|
13
|
+
): string | null {
|
|
14
|
+
const shipping = addresses.find((address) => address.is_default_shipping && address.id)
|
|
15
|
+
if (shipping?.id) return shipping.id
|
|
16
|
+
const billing = addresses.find((address) => address.is_default_billing && address.id)
|
|
17
|
+
return billing?.id ?? null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isExclusiveDefaultAddress(
|
|
21
|
+
address: DefaultAddressFlags,
|
|
22
|
+
defaultAddressId: string | null
|
|
23
|
+
): boolean {
|
|
24
|
+
return Boolean(address.id) && address.id === defaultAddressId
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Default address first; original order kept for the rest. */
|
|
28
|
+
export function sortAddressesDefaultFirst<T extends DefaultAddressFlags>(
|
|
29
|
+
addresses: T[]
|
|
30
|
+
): T[] {
|
|
31
|
+
const defaultId = resolveDefaultAddressId(addresses)
|
|
32
|
+
if (!defaultId) return addresses
|
|
33
|
+
return [...addresses].sort((left, right) => {
|
|
34
|
+
if (left.id === defaultId) return -1
|
|
35
|
+
if (right.id === defaultId) return 1
|
|
36
|
+
return 0
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function resolveAddressType(value: unknown): AddressType {
|
|
41
|
+
return String(value ?? "").trim().toUpperCase() === "OFFICE" ? "OFFICE" : "HOME"
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function addressTypeFromMetadata(metadata: unknown): AddressType {
|
|
45
|
+
if (!metadata || typeof metadata !== "object") return "HOME"
|
|
46
|
+
return resolveAddressType((metadata as Record<string, unknown>).address_type)
|
|
47
|
+
}
|
|
@@ -5,9 +5,17 @@ import type { HttpTypes } from "@medusajs/types"
|
|
|
5
5
|
import {
|
|
6
6
|
addCustomerAddress,
|
|
7
7
|
deleteCustomerAddress,
|
|
8
|
-
setDefaultAddress,
|
|
9
8
|
updateCustomerAddress,
|
|
10
9
|
} from "@pradip1995/commerce-core/data/customer"
|
|
10
|
+
import { setExclusiveDefaultAddress } from "./address-actions"
|
|
11
|
+
import {
|
|
12
|
+
ADDRESS_TYPES,
|
|
13
|
+
addressTypeFromMetadata,
|
|
14
|
+
isExclusiveDefaultAddress,
|
|
15
|
+
resolveAddressType,
|
|
16
|
+
resolveDefaultAddressId,
|
|
17
|
+
sortAddressesDefaultFirst,
|
|
18
|
+
} from "./default-address"
|
|
11
19
|
|
|
12
20
|
export default function ModernAccountAddresses({
|
|
13
21
|
customer,
|
|
@@ -16,10 +24,11 @@ export default function ModernAccountAddresses({
|
|
|
16
24
|
customer: HttpTypes.StoreCustomer
|
|
17
25
|
countryCode: string
|
|
18
26
|
}) {
|
|
19
|
-
const addresses = customer.addresses ?? []
|
|
27
|
+
const addresses = sortAddressesDefaultFirst(customer.addresses ?? [])
|
|
20
28
|
const [editingId, setEditingId] = useState<string | null>(null)
|
|
21
29
|
const [showAdd, setShowAdd] = useState(false)
|
|
22
|
-
const
|
|
30
|
+
const defaultAddressId = resolveDefaultAddressId(addresses)
|
|
31
|
+
const hasDefault = Boolean(defaultAddressId)
|
|
23
32
|
const editingAddress = addresses.find((address) => address.id === editingId)
|
|
24
33
|
|
|
25
34
|
return (
|
|
@@ -54,7 +63,7 @@ export default function ModernAccountAddresses({
|
|
|
54
63
|
|
|
55
64
|
<div style={{ display: "flex", flexDirection: "column", gap: "1.25rem" }}>
|
|
56
65
|
{addresses.map((address) => {
|
|
57
|
-
const isDefault = address
|
|
66
|
+
const isDefault = isExclusiveDefaultAddress(address, defaultAddressId)
|
|
58
67
|
const name = [address.first_name, address.last_name].filter(Boolean).join(" ")
|
|
59
68
|
return (
|
|
60
69
|
<div
|
|
@@ -111,7 +120,7 @@ export default function ModernAccountAddresses({
|
|
|
111
120
|
color: "var(--lux-onyx)",
|
|
112
121
|
}}
|
|
113
122
|
>
|
|
114
|
-
|
|
123
|
+
{addressTypeFromMetadata(address.metadata)}
|
|
115
124
|
</span>
|
|
116
125
|
</div>
|
|
117
126
|
</div>
|
|
@@ -222,9 +231,7 @@ export default function ModernAccountAddresses({
|
|
|
222
231
|
countryCode={countryCode}
|
|
223
232
|
address={editingAddress}
|
|
224
233
|
addressId={editingAddress.id}
|
|
225
|
-
defaultChecked={
|
|
226
|
-
!!(editingAddress.is_default_shipping || editingAddress.is_default_billing)
|
|
227
|
-
}
|
|
234
|
+
defaultChecked={isExclusiveDefaultAddress(editingAddress, defaultAddressId)}
|
|
228
235
|
onDone={() => setEditingId(null)}
|
|
229
236
|
onCancel={() => setEditingId(null)}
|
|
230
237
|
/>
|
|
@@ -309,8 +316,8 @@ function AddressForm({
|
|
|
309
316
|
formData
|
|
310
317
|
)
|
|
311
318
|
if (result?.success) {
|
|
312
|
-
if (addressId && makeDefault
|
|
313
|
-
const defaultResult = await
|
|
319
|
+
if (addressId && makeDefault) {
|
|
320
|
+
const defaultResult = await setExclusiveDefaultAddress(addressId)
|
|
314
321
|
if (!defaultResult?.success) {
|
|
315
322
|
return {
|
|
316
323
|
success: false,
|
|
@@ -352,6 +359,8 @@ function AddressForm({
|
|
|
352
359
|
/>
|
|
353
360
|
<Field label="Phone" name="phone" type="tel" defaultValue={address?.phone || ""} />
|
|
354
361
|
|
|
362
|
+
<AddressTypeField defaultValue={addressTypeFromMetadata(address?.metadata)} />
|
|
363
|
+
|
|
355
364
|
<div className="modern-field--full">
|
|
356
365
|
<label style={{ display: "flex", alignItems: "center", gap: "0.5rem", fontSize: "0.8125rem", cursor: "pointer" }}>
|
|
357
366
|
<input
|
|
@@ -398,15 +407,15 @@ function MakeDefaultButton({ addressId }: { addressId: string }) {
|
|
|
398
407
|
setPending(true)
|
|
399
408
|
setError(null)
|
|
400
409
|
try {
|
|
401
|
-
const result = await
|
|
410
|
+
const result = await setExclusiveDefaultAddress(addressId)
|
|
402
411
|
if (!result?.success) {
|
|
403
412
|
setError(result?.error || "Failed to set default address")
|
|
404
|
-
setPending(false)
|
|
405
413
|
return
|
|
406
414
|
}
|
|
407
415
|
window.location.reload()
|
|
408
416
|
} catch (err) {
|
|
409
417
|
setError(err instanceof Error ? err.message : "Failed to set default address")
|
|
418
|
+
} finally {
|
|
410
419
|
setPending(false)
|
|
411
420
|
}
|
|
412
421
|
}
|
|
@@ -475,6 +484,29 @@ function DeleteAddressButton({ addressId }: { addressId: string }) {
|
|
|
475
484
|
)
|
|
476
485
|
}
|
|
477
486
|
|
|
487
|
+
function AddressTypeField({ defaultValue }: { defaultValue: string }) {
|
|
488
|
+
const selected = resolveAddressType(defaultValue)
|
|
489
|
+
|
|
490
|
+
return (
|
|
491
|
+
<fieldset className="modern-field modern-field--full modern-address-type" style={{ border: 0, margin: 0, padding: 0 }}>
|
|
492
|
+
<legend className="modern-field__label">Address type</legend>
|
|
493
|
+
<div className="modern-address-type__options">
|
|
494
|
+
{ADDRESS_TYPES.map((type) => (
|
|
495
|
+
<label key={type} className="modern-address-type__option">
|
|
496
|
+
<input
|
|
497
|
+
type="radio"
|
|
498
|
+
name="address_type"
|
|
499
|
+
value={type}
|
|
500
|
+
defaultChecked={selected === type}
|
|
501
|
+
/>
|
|
502
|
+
<span>{type === "HOME" ? "Home" : "Office"}</span>
|
|
503
|
+
</label>
|
|
504
|
+
))}
|
|
505
|
+
</div>
|
|
506
|
+
</fieldset>
|
|
507
|
+
)
|
|
508
|
+
}
|
|
509
|
+
|
|
478
510
|
function Field({
|
|
479
511
|
label,
|
|
480
512
|
name,
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
import { useActionState, useState } from "react"
|
|
4
4
|
import type { HttpTypes } from "@medusajs/types"
|
|
5
5
|
import { updateCustomer } from "@pradip1995/commerce-core/data/customer"
|
|
6
|
+
import {
|
|
7
|
+
isValidIndianPhone,
|
|
8
|
+
normalizeIndianPhone,
|
|
9
|
+
} from "@pradip1995/commerce-core/util/phone"
|
|
6
10
|
import {
|
|
7
11
|
sendRegistrationOtp,
|
|
8
12
|
verifyRegistrationOtp,
|
|
@@ -22,6 +26,21 @@ function getInitials(name: string): string {
|
|
|
22
26
|
return name.slice(0, 2).toUpperCase() || "RA"
|
|
23
27
|
}
|
|
24
28
|
|
|
29
|
+
function phoneDigits(value: string): string {
|
|
30
|
+
return value.replace(/\D/g, "")
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function readVerified(
|
|
34
|
+
customer: HttpTypes.StoreCustomer,
|
|
35
|
+
field: "email_verified" | "phone_verified"
|
|
36
|
+
): boolean {
|
|
37
|
+
const direct = (customer as Record<string, unknown>)[field]
|
|
38
|
+
if (direct === true || direct === "true") return true
|
|
39
|
+
const meta = customer.metadata as Record<string, unknown> | undefined
|
|
40
|
+
const fromMeta = meta?.[field]
|
|
41
|
+
return fromMeta === true || fromMeta === "true"
|
|
42
|
+
}
|
|
43
|
+
|
|
25
44
|
export default function ModernAccountProfile({
|
|
26
45
|
customer,
|
|
27
46
|
countryCode,
|
|
@@ -32,6 +51,8 @@ export default function ModernAccountProfile({
|
|
|
32
51
|
OtpComponent?: OtpVerificationComponent
|
|
33
52
|
}) {
|
|
34
53
|
const [message, setMessage] = useState<string | null>(null)
|
|
54
|
+
const [phone, setPhone] = useState(customer.phone || "")
|
|
55
|
+
const [phoneError, setPhoneError] = useState<string | null>(null)
|
|
35
56
|
const [otpOpen, setOtpOpen] = useState(false)
|
|
36
57
|
const [otp, setOtp] = useState("")
|
|
37
58
|
const [otpToken, setOtpToken] = useState<string | null>(null)
|
|
@@ -40,6 +61,9 @@ export default function ModernAccountProfile({
|
|
|
40
61
|
)
|
|
41
62
|
const [otpError, setOtpError] = useState<string | null>(null)
|
|
42
63
|
const [otpPending, setOtpPending] = useState(false)
|
|
64
|
+
const [phoneVerified, setPhoneVerified] = useState(() =>
|
|
65
|
+
readVerified(customer, "phone_verified")
|
|
66
|
+
)
|
|
43
67
|
|
|
44
68
|
async function saveProfile(
|
|
45
69
|
_prev: { ok: boolean; error: string | null },
|
|
@@ -48,11 +72,15 @@ export default function ModernAccountProfile({
|
|
|
48
72
|
try {
|
|
49
73
|
const fullName = String(formData.get("full_name") || "").trim()
|
|
50
74
|
const [first_name, ...rest] = fullName.split(/\s+/)
|
|
75
|
+
const e164 = normalizeIndianPhone(phone)
|
|
51
76
|
await updateCustomer({
|
|
52
77
|
first_name: first_name || "",
|
|
53
78
|
last_name: rest.join(" ") || ".",
|
|
54
|
-
phone:
|
|
79
|
+
phone: e164 || undefined,
|
|
55
80
|
})
|
|
81
|
+
if (e164 && phoneDigits(e164) !== phoneDigits(customer.phone || "")) {
|
|
82
|
+
setPhoneVerified(false)
|
|
83
|
+
}
|
|
56
84
|
return { ok: true, error: null }
|
|
57
85
|
} catch (err) {
|
|
58
86
|
if (isNextRedirect(err)) throw err
|
|
@@ -68,11 +96,43 @@ export default function ModernAccountProfile({
|
|
|
68
96
|
error: null,
|
|
69
97
|
})
|
|
70
98
|
|
|
99
|
+
const phoneReady = isValidIndianPhone(phone)
|
|
100
|
+
const showVerify = !phoneVerified && phoneReady
|
|
101
|
+
|
|
102
|
+
async function ensurePhoneSavedForVerification(): Promise<boolean> {
|
|
103
|
+
setPhoneError(null)
|
|
104
|
+
if (!phoneReady) {
|
|
105
|
+
setPhoneError("Please enter a valid mobile number before verifying.")
|
|
106
|
+
return false
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const e164 = normalizeIndianPhone(phone)
|
|
110
|
+
if (phoneDigits(e164) === phoneDigits(customer.phone || "")) {
|
|
111
|
+
return true
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
await updateCustomer({ phone: e164 })
|
|
116
|
+
return true
|
|
117
|
+
} catch (err) {
|
|
118
|
+
if (isNextRedirect(err)) throw err
|
|
119
|
+
setPhoneError(
|
|
120
|
+
safeErrorMessage(err, "Failed to save mobile number. Please try again.")
|
|
121
|
+
)
|
|
122
|
+
return false
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
71
126
|
async function sendVerifyOtp(type: "email_verification" | "phone_verification") {
|
|
72
127
|
setOtpType(type)
|
|
73
128
|
setOtpPending(true)
|
|
74
129
|
setOtpError(null)
|
|
130
|
+
setPhoneError(null)
|
|
75
131
|
try {
|
|
132
|
+
if (type === "phone_verification") {
|
|
133
|
+
const saved = await ensurePhoneSavedForVerification()
|
|
134
|
+
if (!saved) return
|
|
135
|
+
}
|
|
76
136
|
const res = await sendRegistrationOtp(customer.id, type)
|
|
77
137
|
if (!res.success) throw new Error(res.error || "Failed to send code")
|
|
78
138
|
setOtpToken(res.token ?? null)
|
|
@@ -80,7 +140,9 @@ export default function ModernAccountProfile({
|
|
|
80
140
|
setOtpOpen(true)
|
|
81
141
|
} catch (err) {
|
|
82
142
|
if (isNextRedirect(err)) throw err
|
|
83
|
-
|
|
143
|
+
const msg = safeErrorMessage(err, "Failed to send code")
|
|
144
|
+
setOtpError(msg)
|
|
145
|
+
if (type === "phone_verification") setPhoneError(msg)
|
|
84
146
|
} finally {
|
|
85
147
|
setOtpPending(false)
|
|
86
148
|
}
|
|
@@ -99,6 +161,7 @@ export default function ModernAccountProfile({
|
|
|
99
161
|
})
|
|
100
162
|
if (!res.success) throw new Error(res.error || "Invalid code")
|
|
101
163
|
setOtpOpen(false)
|
|
164
|
+
if (otpType === "phone_verification") setPhoneVerified(true)
|
|
102
165
|
setMessage("Verification successful.")
|
|
103
166
|
window.location.reload()
|
|
104
167
|
} catch (err) {
|
|
@@ -108,17 +171,24 @@ export default function ModernAccountProfile({
|
|
|
108
171
|
}
|
|
109
172
|
}
|
|
110
173
|
|
|
174
|
+
function handlePhoneChange(value: string) {
|
|
175
|
+
setPhone(value)
|
|
176
|
+
setPhoneError(null)
|
|
177
|
+
const next = normalizeIndianPhone(value)
|
|
178
|
+
if (next && phoneDigits(next) !== phoneDigits(customer.phone || "")) {
|
|
179
|
+
setPhoneVerified(false)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
111
183
|
const fullName = [customer.first_name, customer.last_name].filter(Boolean).join(" ")
|
|
112
184
|
const initials = getInitials(fullName || customer.email)
|
|
113
|
-
const emailVerified = (customer
|
|
114
|
-
const
|
|
185
|
+
const emailVerified = readVerified(customer, "email_verified")
|
|
186
|
+
const mobileVerified = phoneVerified && Boolean(customer.phone || phoneReady)
|
|
115
187
|
|
|
116
188
|
return (
|
|
117
189
|
<div className="modern-account-page">
|
|
118
|
-
{/* Page Heading */}
|
|
119
190
|
<h2 className="modern-profile-header-title">ACCOUNT DETAILS</h2>
|
|
120
191
|
|
|
121
|
-
{/* Profile Header Banner */}
|
|
122
192
|
<div className="modern-profile-banner">
|
|
123
193
|
<div className="modern-profile-banner__avatar-wrap">
|
|
124
194
|
<div className="modern-profile-banner__avatar">{initials}</div>
|
|
@@ -129,9 +199,9 @@ export default function ModernAccountProfile({
|
|
|
129
199
|
<p className="modern-profile-banner__email">{customer.email}</p>
|
|
130
200
|
|
|
131
201
|
<div className="modern-profile-banner__pills">
|
|
132
|
-
<span className={`modern-status-dot-pill ${
|
|
202
|
+
<span className={`modern-status-dot-pill ${mobileVerified ? "is-verified" : "is-unverified"}`}>
|
|
133
203
|
<span className="modern-status-dot" />
|
|
134
|
-
{
|
|
204
|
+
{mobileVerified ? "Mobile Verified" : "Mobile Unverified"}
|
|
135
205
|
</span>
|
|
136
206
|
|
|
137
207
|
<span className={`modern-status-dot-pill ${emailVerified ? "is-verified" : "is-unverified"}`}>
|
|
@@ -142,7 +212,6 @@ export default function ModernAccountProfile({
|
|
|
142
212
|
</div>
|
|
143
213
|
</div>
|
|
144
214
|
|
|
145
|
-
{/* Personal Information Form Card */}
|
|
146
215
|
<div className="modern-account-card">
|
|
147
216
|
<div className="modern-card-sec-head">
|
|
148
217
|
<h3 className="modern-card-sec-title">Personal Information</h3>
|
|
@@ -151,7 +220,6 @@ export default function ModernAccountProfile({
|
|
|
151
220
|
|
|
152
221
|
<form action={formAction} className="modern-form" style={{ marginTop: "1.25rem" }}>
|
|
153
222
|
<div className="modern-form-grid-2">
|
|
154
|
-
{/* FULL NAME */}
|
|
155
223
|
<div className="modern-field">
|
|
156
224
|
<label className="modern-field__label">FULL NAME*</label>
|
|
157
225
|
<input
|
|
@@ -163,31 +231,42 @@ export default function ModernAccountProfile({
|
|
|
163
231
|
/>
|
|
164
232
|
</div>
|
|
165
233
|
|
|
166
|
-
{/* MOBILE NUMBER */}
|
|
167
234
|
<div className="modern-field">
|
|
168
|
-
<label className="modern-field__label"
|
|
235
|
+
<label className="modern-field__label" htmlFor="account-profile-phone">
|
|
236
|
+
MOBILE NUMBER*
|
|
237
|
+
</label>
|
|
169
238
|
<div style={{ display: "flex", gap: "0.5rem", alignItems: "center" }}>
|
|
170
239
|
<input
|
|
240
|
+
id="account-profile-phone"
|
|
171
241
|
name="phone"
|
|
172
242
|
type="tel"
|
|
173
|
-
|
|
243
|
+
value={phone}
|
|
244
|
+
onChange={(e) => handlePhoneChange(e.target.value)}
|
|
174
245
|
placeholder="+91 9876543210"
|
|
175
246
|
className="modern-field__input"
|
|
176
247
|
style={{ flex: 1 }}
|
|
248
|
+
autoComplete="tel"
|
|
177
249
|
/>
|
|
178
|
-
{
|
|
250
|
+
{showVerify ? (
|
|
179
251
|
<button
|
|
180
252
|
type="button"
|
|
181
253
|
onClick={() => sendVerifyOtp("phone_verification")}
|
|
182
254
|
disabled={otpPending}
|
|
183
255
|
className="modern-btn modern-btn--primary"
|
|
184
256
|
style={{ minHeight: "2.85rem", padding: "0 1.15rem", borderRadius: "8px", flexShrink: 0 }}
|
|
257
|
+
aria-label="Verify mobile number"
|
|
185
258
|
>
|
|
186
|
-
Verify
|
|
259
|
+
{otpPending && otpType === "phone_verification" ? "Sending…" : "Verify"}
|
|
187
260
|
</button>
|
|
261
|
+
) : phoneVerified ? (
|
|
262
|
+
<span className="modern-verified-pill">✓ Verified</span>
|
|
188
263
|
) : null}
|
|
189
264
|
</div>
|
|
190
|
-
{
|
|
265
|
+
{phoneError ? (
|
|
266
|
+
<p role="alert" style={{ margin: "0.35rem 0 0", fontSize: "0.75rem", color: "#b91c1c" }}>
|
|
267
|
+
{phoneError}
|
|
268
|
+
</p>
|
|
269
|
+
) : showVerify ? (
|
|
191
270
|
<p style={{ margin: "0.35rem 0 0", fontSize: "0.75rem", color: "#888888" }}>
|
|
192
271
|
Tap Verify to get an OTP on this number.
|
|
193
272
|
</p>
|
|
@@ -195,7 +274,6 @@ export default function ModernAccountProfile({
|
|
|
195
274
|
</div>
|
|
196
275
|
</div>
|
|
197
276
|
|
|
198
|
-
{/* EMAIL */}
|
|
199
277
|
<div className="modern-field modern-field--full">
|
|
200
278
|
<label className="modern-field__label">EMAIL</label>
|
|
201
279
|
<div style={{ display: "flex", gap: "0.75rem", alignItems: "center" }}>
|
|
@@ -216,9 +294,7 @@ export default function ModernAccountProfile({
|
|
|
216
294
|
Verify Email
|
|
217
295
|
</button>
|
|
218
296
|
) : (
|
|
219
|
-
<span className="modern-verified-pill">
|
|
220
|
-
✓ Verified
|
|
221
|
-
</span>
|
|
297
|
+
<span className="modern-verified-pill">✓ Verified</span>
|
|
222
298
|
)}
|
|
223
299
|
</div>
|
|
224
300
|
</div>
|
package/src/modern-account.css
CHANGED
|
@@ -737,6 +737,50 @@
|
|
|
737
737
|
color: #ffffff;
|
|
738
738
|
}
|
|
739
739
|
|
|
740
|
+
.modern-address-type__options {
|
|
741
|
+
display: flex;
|
|
742
|
+
flex-wrap: wrap;
|
|
743
|
+
gap: 0.5rem;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
.modern-address-type__option {
|
|
747
|
+
position: relative;
|
|
748
|
+
cursor: pointer;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
.modern-address-type__option input {
|
|
752
|
+
position: absolute;
|
|
753
|
+
opacity: 0;
|
|
754
|
+
pointer-events: none;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
.modern-address-type__option span {
|
|
758
|
+
display: inline-flex;
|
|
759
|
+
align-items: center;
|
|
760
|
+
justify-content: center;
|
|
761
|
+
min-height: 2.4rem;
|
|
762
|
+
padding: 0 1.15rem;
|
|
763
|
+
border-radius: 999px;
|
|
764
|
+
border: 1px solid var(--lux-border);
|
|
765
|
+
background: #ffffff;
|
|
766
|
+
color: var(--lux-onyx);
|
|
767
|
+
font-size: 0.75rem;
|
|
768
|
+
font-weight: 700;
|
|
769
|
+
letter-spacing: 0.08em;
|
|
770
|
+
text-transform: uppercase;
|
|
771
|
+
transition: all 0.18s ease;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
.modern-address-type__option input:focus-visible + span {
|
|
775
|
+
box-shadow: 0 0 0 3px rgba(62, 48, 83, 0.18);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
.modern-address-type__option input:checked + span {
|
|
779
|
+
background: var(--lux-plum);
|
|
780
|
+
border-color: var(--lux-plum);
|
|
781
|
+
color: #ffffff;
|
|
782
|
+
}
|
|
783
|
+
|
|
740
784
|
.modern-btn--primary:hover:not(:disabled) {
|
|
741
785
|
background: #2d223f;
|
|
742
786
|
}
|