@pradip1995/segment-jewelry-account-pages 0.1.3 → 0.1.4
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 +8 -7
- 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-order-card.tsx +59 -57
- package/src/modern-account-orders.tsx +7 -1
- package/src/modern-account-profile.tsx +96 -20
- package/src/modern-account.css +49 -0
- package/src/order-history-status.test.ts +156 -0
- package/src/order-history-status.ts +150 -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.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -15,10 +15,6 @@
|
|
|
15
15
|
".": "./src/index.ts",
|
|
16
16
|
"./manifest": "./src/manifest.ts"
|
|
17
17
|
},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"typecheck": "tsc --noEmit",
|
|
20
|
-
"lint": "tsc --noEmit"
|
|
21
|
-
},
|
|
22
18
|
"peerDependencies": {
|
|
23
19
|
"@pradip1995/commerce-auth": "^4.0.0",
|
|
24
20
|
"@pradip1995/commerce-core": "^4.0.0",
|
|
@@ -31,9 +27,9 @@
|
|
|
31
27
|
"@medusajs/types": "^2.8.0",
|
|
32
28
|
"@pradip1995/commerce-auth": "^4.0.0",
|
|
33
29
|
"@pradip1995/commerce-core": "^4.0.0",
|
|
34
|
-
"@pradip1995/segment-login-template": "^0.5.
|
|
30
|
+
"@pradip1995/segment-login-template": "^0.5.14",
|
|
35
31
|
"@pradip1995/segment-primitives": "^0.4.11",
|
|
36
|
-
"@pradip1995/segment-tokens": "^0.3.
|
|
32
|
+
"@pradip1995/segment-tokens": "^0.3.14"
|
|
37
33
|
},
|
|
38
34
|
"devDependencies": {
|
|
39
35
|
"@pradip1995/plugin-sdk": "^0.2.0",
|
|
@@ -41,5 +37,10 @@
|
|
|
41
37
|
"next": ">=15",
|
|
42
38
|
"react": "19.0.3",
|
|
43
39
|
"typescript": "^5.7.2"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"typecheck": "tsc --noEmit",
|
|
43
|
+
"lint": "tsc --noEmit",
|
|
44
|
+
"test": "node --experimental-strip-types --test src/*.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,
|
|
@@ -4,60 +4,63 @@ import Image from "next/image"
|
|
|
4
4
|
import type { HttpTypes } from "@medusajs/types"
|
|
5
5
|
import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
|
|
6
6
|
import { formatPrice } from "@pradip1995/segment-primitives/format-price"
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
7
|
+
import {
|
|
8
|
+
resolveOrderHistoryStatus,
|
|
9
|
+
type OrderHistoryStatusInput,
|
|
10
|
+
type OrderHistoryStatusTone,
|
|
11
|
+
} from "./order-history-status"
|
|
12
|
+
|
|
13
|
+
const STATUS_ICON_CLASS: Record<OrderHistoryStatusTone, string> = {
|
|
14
|
+
cancel: "modern-order-row__status-icon--cancel",
|
|
15
|
+
shipped: "modern-order-row__status-icon--shipped",
|
|
16
|
+
done: "modern-order-row__status-icon--done",
|
|
17
|
+
return: "modern-order-row__status-icon--return",
|
|
18
|
+
processing: "modern-order-row__status-icon--done",
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function StatusIcon({ tone }: { tone: OrderHistoryStatusTone }) {
|
|
22
|
+
if (tone === "cancel") {
|
|
23
|
+
return (
|
|
24
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" width="14" height="14">
|
|
25
|
+
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
|
|
26
|
+
</svg>
|
|
27
|
+
)
|
|
23
28
|
}
|
|
24
|
-
if (
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" width="16" height="16">
|
|
31
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" />
|
|
32
|
-
</svg>
|
|
33
|
-
)
|
|
34
|
-
},
|
|
35
|
-
}
|
|
29
|
+
if (tone === "shipped") {
|
|
30
|
+
return (
|
|
31
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" width="16" height="16">
|
|
32
|
+
<path strokeLinecap="round" strokeLinejoin="round" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" />
|
|
33
|
+
</svg>
|
|
34
|
+
)
|
|
36
35
|
}
|
|
37
|
-
if (
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
36
|
+
if (tone === "return") {
|
|
37
|
+
return (
|
|
38
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="15" height="15">
|
|
39
|
+
<path strokeLinecap="round" strokeLinejoin="round" d="M9 15L3 9m0 0l6-6M3 9h12a6 6 0 010 12h-3" />
|
|
40
|
+
</svg>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
if (tone === "processing") {
|
|
44
|
+
return (
|
|
45
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="15" height="15">
|
|
46
|
+
<circle cx="12" cy="12" r="9" />
|
|
47
|
+
<path strokeLinecap="round" strokeLinejoin="round" d="M12 7v5l3 3" />
|
|
48
|
+
</svg>
|
|
49
|
+
)
|
|
49
50
|
}
|
|
51
|
+
return (
|
|
52
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" width="15" height="15">
|
|
53
|
+
<path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
|
|
54
|
+
</svg>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getStatusDetails(order: HttpTypes.StoreOrder) {
|
|
59
|
+
const resolved = resolveOrderHistoryStatus(order as OrderHistoryStatusInput)
|
|
50
60
|
return {
|
|
51
|
-
label:
|
|
52
|
-
iconClass:
|
|
53
|
-
|
|
54
|
-
return (
|
|
55
|
-
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="15" height="15">
|
|
56
|
-
<circle cx="12" cy="12" r="9" />
|
|
57
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M12 7v5l3 3" />
|
|
58
|
-
</svg>
|
|
59
|
-
)
|
|
60
|
-
},
|
|
61
|
+
label: resolved.label,
|
|
62
|
+
iconClass: STATUS_ICON_CLASS[resolved.tone],
|
|
63
|
+
tone: resolved.tone,
|
|
61
64
|
}
|
|
62
65
|
}
|
|
63
66
|
|
|
@@ -87,9 +90,8 @@ export default function ModernAccountOrderCard({
|
|
|
87
90
|
compact?: boolean
|
|
88
91
|
}) {
|
|
89
92
|
const currency = order.currency_code
|
|
90
|
-
const statusInfo = getStatusDetails(order
|
|
91
|
-
const
|
|
92
|
-
const items = presentation.purchasedItems
|
|
93
|
+
const statusInfo = getStatusDetails(order)
|
|
94
|
+
const items = order.items || []
|
|
93
95
|
|
|
94
96
|
const dateStr = order.created_at
|
|
95
97
|
? new Date(order.created_at).toLocaleDateString("en-US", {
|
|
@@ -118,7 +120,7 @@ export default function ModernAccountOrderCard({
|
|
|
118
120
|
<div className="modern-order-row__col">
|
|
119
121
|
<p className="modern-order-row__label">Total amount</p>
|
|
120
122
|
<p className="modern-order-row__val modern-order-row__val--price">
|
|
121
|
-
{formatPrice(
|
|
123
|
+
{formatPrice(order.total, currency)}
|
|
122
124
|
</p>
|
|
123
125
|
</div>
|
|
124
126
|
|
|
@@ -126,7 +128,7 @@ export default function ModernAccountOrderCard({
|
|
|
126
128
|
<p className="modern-order-row__label">Status</p>
|
|
127
129
|
<div className="modern-order-row__status">
|
|
128
130
|
<div className={`modern-order-row__status-icon ${statusInfo.iconClass}`}>
|
|
129
|
-
<statusInfo.
|
|
131
|
+
<StatusIcon tone={statusInfo.tone} />
|
|
130
132
|
</div>
|
|
131
133
|
<span className="modern-order-row__status-text">{statusInfo.label}</span>
|
|
132
134
|
</div>
|
|
@@ -157,7 +159,7 @@ export default function ModernAccountOrderCard({
|
|
|
157
159
|
|
|
158
160
|
<div className="modern-order-row__status">
|
|
159
161
|
<div className={`modern-order-row__status-icon ${statusInfo.iconClass}`}>
|
|
160
|
-
<statusInfo.
|
|
162
|
+
<StatusIcon tone={statusInfo.tone} />
|
|
161
163
|
</div>
|
|
162
164
|
<span className="modern-order-row__status-text">{statusInfo.label}</span>
|
|
163
165
|
</div>
|
|
@@ -206,7 +208,7 @@ export default function ModernAccountOrderCard({
|
|
|
206
208
|
<div className="modern-order-card__foot">
|
|
207
209
|
<div>
|
|
208
210
|
<span className="modern-order-card__label">Total Amount</span>
|
|
209
|
-
<p className="modern-order-card__total-val">{formatPrice(
|
|
211
|
+
<p className="modern-order-card__total-val">{formatPrice(order.total, currency)}</p>
|
|
210
212
|
</div>
|
|
211
213
|
<LocalizedLink href={`/orders/${order.id}`} className="modern-btn modern-btn--outline">
|
|
212
214
|
View Order Details →
|
|
@@ -4,6 +4,10 @@ import { useMemo, useState } from "react"
|
|
|
4
4
|
import type { HttpTypes } from "@medusajs/types"
|
|
5
5
|
import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
|
|
6
6
|
import ModernAccountOrderCard from "./modern-account-order-card"
|
|
7
|
+
import {
|
|
8
|
+
resolveOrderHistoryStatus,
|
|
9
|
+
type OrderHistoryStatusInput,
|
|
10
|
+
} from "./order-history-status"
|
|
7
11
|
|
|
8
12
|
export default function ModernAccountOrders({
|
|
9
13
|
orders,
|
|
@@ -25,7 +29,9 @@ export default function ModernAccountOrders({
|
|
|
25
29
|
if (!q) return safeOrders
|
|
26
30
|
return safeOrders.filter((order) => {
|
|
27
31
|
const id = String(order.display_id ?? order.id)
|
|
28
|
-
const status =
|
|
32
|
+
const status = resolveOrderHistoryStatus(
|
|
33
|
+
order as OrderHistoryStatusInput
|
|
34
|
+
).label
|
|
29
35
|
const fulfillment = String(order.fulfillment_status ?? "").replace(/_/g, " ")
|
|
30
36
|
return (
|
|
31
37
|
id.includes(q) ||
|
|
@@ -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
|
@@ -504,6 +504,11 @@
|
|
|
504
504
|
color: #b91c1c;
|
|
505
505
|
}
|
|
506
506
|
|
|
507
|
+
.modern-order-row__status-icon--return {
|
|
508
|
+
background: #f5ede6;
|
|
509
|
+
color: #6b432a;
|
|
510
|
+
}
|
|
511
|
+
|
|
507
512
|
.modern-order-row__status-text {
|
|
508
513
|
font-size: 0.875rem;
|
|
509
514
|
font-weight: 700;
|
|
@@ -737,6 +742,50 @@
|
|
|
737
742
|
color: #ffffff;
|
|
738
743
|
}
|
|
739
744
|
|
|
745
|
+
.modern-address-type__options {
|
|
746
|
+
display: flex;
|
|
747
|
+
flex-wrap: wrap;
|
|
748
|
+
gap: 0.5rem;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
.modern-address-type__option {
|
|
752
|
+
position: relative;
|
|
753
|
+
cursor: pointer;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
.modern-address-type__option input {
|
|
757
|
+
position: absolute;
|
|
758
|
+
opacity: 0;
|
|
759
|
+
pointer-events: none;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
.modern-address-type__option span {
|
|
763
|
+
display: inline-flex;
|
|
764
|
+
align-items: center;
|
|
765
|
+
justify-content: center;
|
|
766
|
+
min-height: 2.4rem;
|
|
767
|
+
padding: 0 1.15rem;
|
|
768
|
+
border-radius: 999px;
|
|
769
|
+
border: 1px solid var(--lux-border);
|
|
770
|
+
background: #ffffff;
|
|
771
|
+
color: var(--lux-onyx);
|
|
772
|
+
font-size: 0.75rem;
|
|
773
|
+
font-weight: 700;
|
|
774
|
+
letter-spacing: 0.08em;
|
|
775
|
+
text-transform: uppercase;
|
|
776
|
+
transition: all 0.18s ease;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
.modern-address-type__option input:focus-visible + span {
|
|
780
|
+
box-shadow: 0 0 0 3px rgba(62, 48, 83, 0.18);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
.modern-address-type__option input:checked + span {
|
|
784
|
+
background: var(--lux-plum);
|
|
785
|
+
border-color: var(--lux-plum);
|
|
786
|
+
color: #ffffff;
|
|
787
|
+
}
|
|
788
|
+
|
|
740
789
|
.modern-btn--primary:hover:not(:disabled) {
|
|
741
790
|
background: #2d223f;
|
|
742
791
|
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import {
|
|
4
|
+
resolveOrderHistoryStatus,
|
|
5
|
+
type OrderHistoryStatus,
|
|
6
|
+
type OrderHistoryStatusInput,
|
|
7
|
+
} from "./order-history-status.ts"
|
|
8
|
+
|
|
9
|
+
const CASES: Array<{
|
|
10
|
+
name: string
|
|
11
|
+
order: OrderHistoryStatusInput
|
|
12
|
+
want: OrderHistoryStatus
|
|
13
|
+
}> = [
|
|
14
|
+
{
|
|
15
|
+
name: "stays delivered when there is no return",
|
|
16
|
+
order: { status: "completed", fulfillment_status: "delivered" },
|
|
17
|
+
want: { label: "Delivered", tone: "done" },
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
name: "shows return requested after a return is submitted on a delivered order",
|
|
21
|
+
order: {
|
|
22
|
+
status: "completed",
|
|
23
|
+
fulfillment_status: "delivered",
|
|
24
|
+
returns: [{ status: "requested" }],
|
|
25
|
+
},
|
|
26
|
+
want: { label: "Return Requested", tone: "return" },
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "treats a return with no status as requested",
|
|
30
|
+
order: {
|
|
31
|
+
fulfillment_status: "delivered",
|
|
32
|
+
returns: [{ status: null }],
|
|
33
|
+
},
|
|
34
|
+
want: { label: "Return Requested", tone: "return" },
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "maps open returns to return approved",
|
|
38
|
+
order: {
|
|
39
|
+
fulfillment_status: "delivered",
|
|
40
|
+
returns: [{ status: "open" }],
|
|
41
|
+
},
|
|
42
|
+
want: { label: "Return Approved", tone: "return" },
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: "maps approved returns to return approved",
|
|
46
|
+
order: {
|
|
47
|
+
fulfillment_status: "delivered",
|
|
48
|
+
returns: [{ status: "approved" }],
|
|
49
|
+
},
|
|
50
|
+
want: { label: "Return Approved", tone: "return" },
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "maps partially received returns to return in progress",
|
|
54
|
+
order: {
|
|
55
|
+
fulfillment_status: "delivered",
|
|
56
|
+
returns: [{ status: "partially_received" }],
|
|
57
|
+
},
|
|
58
|
+
want: { label: "Return In Progress", tone: "return" },
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: "maps received returns to returned",
|
|
62
|
+
order: {
|
|
63
|
+
status: "completed",
|
|
64
|
+
fulfillment_status: "delivered",
|
|
65
|
+
returns: [{ status: "received" }],
|
|
66
|
+
},
|
|
67
|
+
want: { label: "Returned", tone: "return" },
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: "maps fulfillment returned when returns are missing from the payload",
|
|
71
|
+
order: { fulfillment_status: "returned" },
|
|
72
|
+
want: { label: "Returned", tone: "return" },
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: "maps fulfillment partially returned to return in progress",
|
|
76
|
+
order: { fulfillment_status: "partially_returned" },
|
|
77
|
+
want: { label: "Return In Progress", tone: "return" },
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: "shows refunded when payment is refunded after a return",
|
|
81
|
+
order: {
|
|
82
|
+
fulfillment_status: "returned",
|
|
83
|
+
payment_status: "refunded",
|
|
84
|
+
returns: [{ status: "received" }],
|
|
85
|
+
},
|
|
86
|
+
want: { label: "Refunded", tone: "return" },
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "shows refunded for a partial refund",
|
|
90
|
+
order: {
|
|
91
|
+
fulfillment_status: "delivered",
|
|
92
|
+
payment_status: "partially_refunded",
|
|
93
|
+
returns: [{ status: "requested" }],
|
|
94
|
+
},
|
|
95
|
+
want: { label: "Refunded", tone: "return" },
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: "ignores canceled returns and keeps delivered",
|
|
99
|
+
order: {
|
|
100
|
+
fulfillment_status: "delivered",
|
|
101
|
+
returns: [{ status: "canceled" }, { status: "cancelled" }],
|
|
102
|
+
},
|
|
103
|
+
want: { label: "Delivered", tone: "done" },
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
name: "ignores exchange and claim returns",
|
|
107
|
+
order: {
|
|
108
|
+
fulfillment_status: "delivered",
|
|
109
|
+
returns: [
|
|
110
|
+
{ status: "requested", exchange_id: "exch_1" },
|
|
111
|
+
{ status: "requested", claim_id: "claim_1" },
|
|
112
|
+
],
|
|
113
|
+
},
|
|
114
|
+
want: { label: "Delivered", tone: "done" },
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "uses the furthest return lifecycle when several returns exist",
|
|
118
|
+
order: {
|
|
119
|
+
fulfillment_status: "delivered",
|
|
120
|
+
returns: [{ status: "requested" }, { status: "received" }],
|
|
121
|
+
},
|
|
122
|
+
want: { label: "Returned", tone: "return" },
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "cancelled order wins over a return",
|
|
126
|
+
order: {
|
|
127
|
+
status: "canceled",
|
|
128
|
+
fulfillment_status: "delivered",
|
|
129
|
+
returns: [{ status: "requested" }],
|
|
130
|
+
},
|
|
131
|
+
want: { label: "Cancelled", tone: "cancel" },
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: "shows shipped when not delivered and no return",
|
|
135
|
+
order: { fulfillment_status: "shipped" },
|
|
136
|
+
want: { label: "Shipped", tone: "shipped" },
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: "shows order confirmed for fulfilled orders",
|
|
140
|
+
order: { fulfillment_status: "fulfilled" },
|
|
141
|
+
want: { label: "Order Confirmed", tone: "done" },
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
name: "shows processing as the default",
|
|
145
|
+
order: { status: "pending" },
|
|
146
|
+
want: { label: "Processing", tone: "processing" },
|
|
147
|
+
},
|
|
148
|
+
]
|
|
149
|
+
|
|
150
|
+
describe("resolveOrderHistoryStatus", () => {
|
|
151
|
+
for (const row of CASES) {
|
|
152
|
+
it(row.name, () => {
|
|
153
|
+
assert.deepEqual(resolveOrderHistoryStatus(row.order), row.want)
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
})
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
export type OrderHistoryStatusTone =
|
|
2
|
+
| "cancel"
|
|
3
|
+
| "shipped"
|
|
4
|
+
| "done"
|
|
5
|
+
| "return"
|
|
6
|
+
| "processing"
|
|
7
|
+
|
|
8
|
+
export type OrderHistoryStatus = {
|
|
9
|
+
label: string
|
|
10
|
+
tone: OrderHistoryStatusTone
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type OrderHistoryReturnLike = {
|
|
14
|
+
status?: string | null
|
|
15
|
+
exchange_id?: string | null
|
|
16
|
+
claim_id?: string | null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type OrderHistoryStatusInput = {
|
|
20
|
+
status?: string | null
|
|
21
|
+
fulfillment_status?: string | null
|
|
22
|
+
payment_status?: string | null
|
|
23
|
+
returns?: OrderHistoryReturnLike[] | null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type ReturnLifecycle =
|
|
27
|
+
| "requested"
|
|
28
|
+
| "approved"
|
|
29
|
+
| "in_progress"
|
|
30
|
+
| "returned"
|
|
31
|
+
| "refunded"
|
|
32
|
+
|
|
33
|
+
const CLOSED_RETURN = new Set(["canceled", "cancelled", "rejected"])
|
|
34
|
+
|
|
35
|
+
const RETURN_RANK: Record<ReturnLifecycle, number> = {
|
|
36
|
+
requested: 1,
|
|
37
|
+
approved: 2,
|
|
38
|
+
in_progress: 3,
|
|
39
|
+
returned: 4,
|
|
40
|
+
refunded: 5,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const RETURN_LABEL: Record<ReturnLifecycle, string> = {
|
|
44
|
+
requested: "Return Requested",
|
|
45
|
+
approved: "Return Approved",
|
|
46
|
+
in_progress: "Return In Progress",
|
|
47
|
+
returned: "Returned",
|
|
48
|
+
refunded: "Refunded",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalize(value?: string | null) {
|
|
52
|
+
return (value || "").toLowerCase().trim()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isStandaloneReturn(ret: OrderHistoryReturnLike) {
|
|
56
|
+
return !ret.exchange_id && !ret.claim_id
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function lifecycleFromReturnStatus(status: string): ReturnLifecycle | null {
|
|
60
|
+
switch (status) {
|
|
61
|
+
case "requested":
|
|
62
|
+
return "requested"
|
|
63
|
+
case "open":
|
|
64
|
+
case "approved":
|
|
65
|
+
return "approved"
|
|
66
|
+
case "partially_received":
|
|
67
|
+
case "in_progress":
|
|
68
|
+
return "in_progress"
|
|
69
|
+
case "received":
|
|
70
|
+
case "returned":
|
|
71
|
+
case "complete":
|
|
72
|
+
case "completed":
|
|
73
|
+
return "returned"
|
|
74
|
+
default:
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function pickHigher(
|
|
80
|
+
current: ReturnLifecycle | null,
|
|
81
|
+
next: ReturnLifecycle
|
|
82
|
+
): ReturnLifecycle {
|
|
83
|
+
if (!current) return next
|
|
84
|
+
return RETURN_RANK[next] > RETURN_RANK[current] ? next : current
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function returnLifecycle(order: OrderHistoryStatusInput): ReturnLifecycle | null {
|
|
88
|
+
const payment = normalize(order.payment_status)
|
|
89
|
+
let best: ReturnLifecycle | null = null
|
|
90
|
+
|
|
91
|
+
if (payment === "refunded" || payment === "partially_refunded") {
|
|
92
|
+
best = "refunded"
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const ret of order.returns || []) {
|
|
96
|
+
if (!isStandaloneReturn(ret)) continue
|
|
97
|
+
const status = normalize(ret.status)
|
|
98
|
+
if (CLOSED_RETURN.has(status)) continue
|
|
99
|
+
const life = status
|
|
100
|
+
? lifecycleFromReturnStatus(status) ?? "requested"
|
|
101
|
+
: "requested"
|
|
102
|
+
best = pickHigher(best, life)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const fulfillment = normalize(order.fulfillment_status)
|
|
106
|
+
if (fulfillment === "returned") {
|
|
107
|
+
best = pickHigher(best, "returned")
|
|
108
|
+
} else if (fulfillment === "partially_returned") {
|
|
109
|
+
best = pickHigher(best, "in_progress")
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return best
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function resolveOrderHistoryStatus(
|
|
116
|
+
order: OrderHistoryStatusInput
|
|
117
|
+
): OrderHistoryStatus {
|
|
118
|
+
const orderStatus = normalize(order.status)
|
|
119
|
+
if (orderStatus === "canceled" || orderStatus === "cancelled") {
|
|
120
|
+
return { label: "Cancelled", tone: "cancel" }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const life = returnLifecycle(order)
|
|
124
|
+
if (life) {
|
|
125
|
+
return { label: RETURN_LABEL[life], tone: "return" }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const fulfillment = normalize(order.fulfillment_status)
|
|
129
|
+
const combined = fulfillment || orderStatus
|
|
130
|
+
|
|
131
|
+
if (combined.includes("cancel")) {
|
|
132
|
+
return { label: "Cancelled", tone: "cancel" }
|
|
133
|
+
}
|
|
134
|
+
if (combined.includes("ship")) {
|
|
135
|
+
return { label: "Shipped", tone: "shipped" }
|
|
136
|
+
}
|
|
137
|
+
if (
|
|
138
|
+
combined === "delivered" ||
|
|
139
|
+
combined === "complete" ||
|
|
140
|
+
combined === "completed" ||
|
|
141
|
+
combined.includes("fulfill")
|
|
142
|
+
) {
|
|
143
|
+
return {
|
|
144
|
+
label: combined === "delivered" ? "Delivered" : "Order Confirmed",
|
|
145
|
+
tone: "done",
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return { label: "Processing", tone: "processing" }
|
|
150
|
+
}
|