@pradip1995/segment-login-template 0.5.18 → 0.5.19
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
CHANGED
package/src/account-orders.tsx
CHANGED
|
@@ -7,6 +7,7 @@ import { addToCart } from "@pradip1995/commerce-core/client/actions/cart"
|
|
|
7
7
|
import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
|
|
8
8
|
import { formatPrice } from "@pradip1995/segment-primitives/format-price"
|
|
9
9
|
import { presentOrderLines } from "@pradip1995/segment-primitives/order-lines"
|
|
10
|
+
import { applyReorderCheckoutAddress, clearCartLineItems } from "./reorder-checkout-address"
|
|
10
11
|
import { acct } from "./account-theme"
|
|
11
12
|
import {
|
|
12
13
|
resolveOrderHistoryStatus,
|
|
@@ -81,6 +82,7 @@ export default function AccountOrders({
|
|
|
81
82
|
setReorderingId(order.id)
|
|
82
83
|
try {
|
|
83
84
|
const reorderItems = presentOrderLines(order).reorderItems
|
|
85
|
+
await clearCartLineItems()
|
|
84
86
|
for (const item of reorderItems) {
|
|
85
87
|
if (!item.variant_id) continue
|
|
86
88
|
await addToCart({
|
|
@@ -89,7 +91,8 @@ export default function AccountOrders({
|
|
|
89
91
|
countryCode,
|
|
90
92
|
})
|
|
91
93
|
}
|
|
92
|
-
|
|
94
|
+
await applyReorderCheckoutAddress(order)
|
|
95
|
+
router.push(`/${countryCode}/checkout`)
|
|
93
96
|
} catch {
|
|
94
97
|
setReorderingId(null)
|
|
95
98
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { HttpTypes } from "@medusajs/types"
|
|
2
|
+
import {
|
|
3
|
+
deleteLineItem,
|
|
4
|
+
retrieveCart,
|
|
5
|
+
updateAddressSilently,
|
|
6
|
+
} from "@pradip1995/commerce-core/client/actions/cart"
|
|
7
|
+
import { retrieveCustomer } from "@pradip1995/commerce-core/data/customer"
|
|
8
|
+
import { retrieveOrder } from "@pradip1995/commerce-core/data/orders"
|
|
9
|
+
|
|
10
|
+
type AddressLike = {
|
|
11
|
+
first_name?: string | null
|
|
12
|
+
last_name?: string | null
|
|
13
|
+
address_1?: string | null
|
|
14
|
+
address_2?: string | null
|
|
15
|
+
city?: string | null
|
|
16
|
+
country_code?: string | null
|
|
17
|
+
postal_code?: string | null
|
|
18
|
+
province?: string | null
|
|
19
|
+
phone?: string | null
|
|
20
|
+
company?: string | null
|
|
21
|
+
is_default_shipping?: boolean | null
|
|
22
|
+
is_default_billing?: boolean | null
|
|
23
|
+
metadata?: Record<string, unknown> | null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function pickDefaultCustomerAddress(
|
|
27
|
+
customer: HttpTypes.StoreCustomer | null | undefined
|
|
28
|
+
): AddressLike | undefined {
|
|
29
|
+
const addresses = (customer?.addresses ?? []) as AddressLike[]
|
|
30
|
+
return (
|
|
31
|
+
addresses.find((address) => address.is_default_shipping) ||
|
|
32
|
+
addresses.find(
|
|
33
|
+
(address) =>
|
|
34
|
+
address.metadata?.is_default === true || address.metadata?.is_default === "true"
|
|
35
|
+
) ||
|
|
36
|
+
addresses.find((address) => address.is_default_billing)
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function hasUsableAddress(address?: AddressLike | null): address is AddressLike {
|
|
41
|
+
return !!(address && (address.address_1?.trim() || address.postal_code?.trim()))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function toCartAddress(source: AddressLike) {
|
|
45
|
+
return {
|
|
46
|
+
first_name: source.first_name || undefined,
|
|
47
|
+
last_name: source.last_name || undefined,
|
|
48
|
+
address_1: source.address_1 || undefined,
|
|
49
|
+
address_2: source.address_2 || undefined,
|
|
50
|
+
city: source.city || undefined,
|
|
51
|
+
country_code: source.country_code || undefined,
|
|
52
|
+
postal_code: source.postal_code || undefined,
|
|
53
|
+
province: source.province || undefined,
|
|
54
|
+
phone: source.phone || undefined,
|
|
55
|
+
company: source.company || undefined,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Clear existing bag so each reorder starts from the order qty, not stacked qty. */
|
|
60
|
+
export async function clearCartLineItems() {
|
|
61
|
+
const cart = await retrieveCart().catch(() => null)
|
|
62
|
+
const items = cart?.items || []
|
|
63
|
+
for (const item of items) {
|
|
64
|
+
if (!item.id) continue
|
|
65
|
+
await deleteLineItem(item.id)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Prefill checkout cart address after reorder:
|
|
71
|
+
* 1) customer default address, else
|
|
72
|
+
* 2) shipping/billing from the reordered order.
|
|
73
|
+
*/
|
|
74
|
+
export async function applyReorderCheckoutAddress(order: HttpTypes.StoreOrder) {
|
|
75
|
+
try {
|
|
76
|
+
const customer = await retrieveCustomer().catch(() => null)
|
|
77
|
+
if (customer?.id === "pending_deletion") return
|
|
78
|
+
|
|
79
|
+
let source = pickDefaultCustomerAddress(customer)
|
|
80
|
+
|
|
81
|
+
if (!hasUsableAddress(source)) {
|
|
82
|
+
source = order.shipping_address || order.billing_address
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!hasUsableAddress(source) && order.id) {
|
|
86
|
+
const fullOrder = await retrieveOrder(order.id).catch(() => null)
|
|
87
|
+
source = fullOrder?.shipping_address || fullOrder?.billing_address
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!hasUsableAddress(source)) return
|
|
91
|
+
|
|
92
|
+
await updateAddressSilently({
|
|
93
|
+
email: order.email || customer?.email || undefined,
|
|
94
|
+
shipping_address: toCartAddress(source),
|
|
95
|
+
same_as_billing: true,
|
|
96
|
+
assignShipping: true,
|
|
97
|
+
})
|
|
98
|
+
} catch {
|
|
99
|
+
// Checkout can still open; user can fill address manually.
|
|
100
|
+
}
|
|
101
|
+
}
|