@pradip1995/segment-primitives 0.4.14 → 0.4.15
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 -1
- package/src/Button.tsx +19 -19
- package/src/Card.tsx +10 -10
- package/src/coupon-apply-result.test.ts +73 -73
- package/src/coupon-apply-result.ts +41 -41
- package/src/coupon-apply.tsx +184 -184
- package/src/format-price.ts +12 -12
- package/src/index.ts +2 -0
- package/src/is-next-redirect.ts +86 -86
- package/src/localized-link.tsx +37 -37
- package/src/notify-wishlist.ts +9 -9
- package/src/optimized-image-src.ts +16 -0
- package/src/order-lines.test.ts +246 -246
- package/src/order-lines.ts +151 -151
- package/src/product-selection-context.tsx +56 -56
- package/src/product-variants.ts +381 -381
- package/src/resolve-media-url.ts +146 -146
- package/src/revalidate-wishlist.ts +8 -8
- package/src/section-variants.ts +13 -13
- package/src/skip-image-optimization.test.ts +27 -0
- package/src/skip-image-optimization.ts +11 -0
- package/src/use-wishlist-count.ts +32 -32
- package/src/wishlist-count.test.ts +96 -96
- package/src/wishlist-count.ts +26 -26
package/src/order-lines.ts
CHANGED
|
@@ -1,151 +1,151 @@
|
|
|
1
|
-
export type OrderLineQuantities = {
|
|
2
|
-
fulfilled_quantity?: number | null
|
|
3
|
-
delivered_quantity?: number | null
|
|
4
|
-
return_requested_quantity?: number | null
|
|
5
|
-
returned_quantity?: number | null
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export type OrderLineLike = {
|
|
9
|
-
id: string
|
|
10
|
-
product_id?: string | null
|
|
11
|
-
variant_id?: string | null
|
|
12
|
-
quantity?: number | null
|
|
13
|
-
total?: number | null
|
|
14
|
-
unit_price?: number | null
|
|
15
|
-
created_at?: string | Date | null
|
|
16
|
-
detail?: OrderLineQuantities | null
|
|
17
|
-
} & OrderLineQuantities
|
|
18
|
-
|
|
19
|
-
export type OrderExchangeLike = {
|
|
20
|
-
canceled_at?: string | Date | null
|
|
21
|
-
additional_items?: Array<{ id?: string | null; item_id?: string | null }> | null
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type OrderLike = {
|
|
25
|
-
items?: OrderLineLike[] | null
|
|
26
|
-
shipping_total?: number | null
|
|
27
|
-
total?: number | null
|
|
28
|
-
exchanges?: OrderExchangeLike[] | null
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function qty(value?: number | null): number {
|
|
32
|
-
const n = Number(value)
|
|
33
|
-
return Number.isFinite(n) ? n : 0
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function lineQty(item: OrderLineLike, key: keyof OrderLineQuantities): number {
|
|
37
|
-
return qty(item.detail?.[key] ?? item[key])
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function itemCreatedAt(item: OrderLineLike): number {
|
|
41
|
-
if (!item.created_at) return 0
|
|
42
|
-
const t = new Date(item.created_at).getTime()
|
|
43
|
-
return Number.isFinite(t) ? t : 0
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function isCanceled(value?: string | Date | null): boolean {
|
|
47
|
-
return Boolean(value)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function isInboundExchangeItem(item: OrderLineLike): boolean {
|
|
51
|
-
return (
|
|
52
|
-
lineQty(item, "return_requested_quantity") > 0 ||
|
|
53
|
-
lineQty(item, "returned_quantity") > 0
|
|
54
|
-
)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function outboundExchangeItemIds(
|
|
58
|
-
items: OrderLineLike[],
|
|
59
|
-
exchanges?: OrderExchangeLike[] | null
|
|
60
|
-
): Set<string> {
|
|
61
|
-
const ids = new Set<string>()
|
|
62
|
-
for (const exchange of exchanges || []) {
|
|
63
|
-
if (isCanceled(exchange.canceled_at)) continue
|
|
64
|
-
for (const extra of exchange.additional_items || []) {
|
|
65
|
-
const id = extra.item_id || extra.id
|
|
66
|
-
if (id) ids.add(id)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (ids.size > 0) return ids
|
|
70
|
-
|
|
71
|
-
const inbound = items.filter(isInboundExchangeItem)
|
|
72
|
-
if (!inbound.length) return ids
|
|
73
|
-
|
|
74
|
-
const inboundIds = new Set(inbound.map((item) => item.id))
|
|
75
|
-
const inboundByProduct = new Map<string, OrderLineLike[]>()
|
|
76
|
-
for (const item of inbound) {
|
|
77
|
-
if (!item.product_id) continue
|
|
78
|
-
const list = inboundByProduct.get(item.product_id) || []
|
|
79
|
-
list.push(item)
|
|
80
|
-
inboundByProduct.set(item.product_id, list)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
for (const item of items) {
|
|
84
|
-
if (inboundIds.has(item.id) || !item.product_id) continue
|
|
85
|
-
const matches = inboundByProduct.get(item.product_id)
|
|
86
|
-
if (!matches?.length) continue
|
|
87
|
-
|
|
88
|
-
const laterThanInbound = matches.some((origin) => {
|
|
89
|
-
const originAt = itemCreatedAt(origin)
|
|
90
|
-
const candidateAt = itemCreatedAt(item)
|
|
91
|
-
return originAt > 0 && candidateAt > originAt + 1000
|
|
92
|
-
})
|
|
93
|
-
const pendingReplacement =
|
|
94
|
-
lineQty(item, "fulfilled_quantity") === 0 &&
|
|
95
|
-
lineQty(item, "delivered_quantity") === 0
|
|
96
|
-
|
|
97
|
-
if (laterThanInbound || pendingReplacement) {
|
|
98
|
-
ids.add(item.id)
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return ids
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function presentOrderLines<T extends OrderLineLike>(order: {
|
|
106
|
-
items?: T[] | null
|
|
107
|
-
shipping_total?: number | null
|
|
108
|
-
total?: number | null
|
|
109
|
-
exchanges?: OrderExchangeLike[] | null
|
|
110
|
-
}): {
|
|
111
|
-
purchasedItems: T[]
|
|
112
|
-
outboundItems: T[]
|
|
113
|
-
inboundItems: T[]
|
|
114
|
-
reorderItems: T[]
|
|
115
|
-
paidSubtotal: number
|
|
116
|
-
paidTotal: number
|
|
117
|
-
hasActiveExchange: boolean
|
|
118
|
-
originalFulfillmentIsComplete: boolean
|
|
119
|
-
} {
|
|
120
|
-
const items = (order.items || []) as T[]
|
|
121
|
-
const outboundIds = outboundExchangeItemIds(items, order.exchanges)
|
|
122
|
-
const outboundItems = items.filter((item) => outboundIds.has(item.id))
|
|
123
|
-
const purchasedItems = items.filter((item) => !outboundIds.has(item.id))
|
|
124
|
-
const inboundItems = purchasedItems.filter(isInboundExchangeItem)
|
|
125
|
-
const paidSubtotal = purchasedItems.reduce((sum, item) => {
|
|
126
|
-
if (typeof item.total === "number") return sum + item.total
|
|
127
|
-
return sum + qty(item.unit_price) * qty(item.quantity)
|
|
128
|
-
}, 0)
|
|
129
|
-
const shipping = qty(order.shipping_total)
|
|
130
|
-
const hasActiveExchange = inboundItems.some(
|
|
131
|
-
(item) => lineQty(item, "return_requested_quantity") > 0
|
|
132
|
-
)
|
|
133
|
-
const originalFulfillmentIsComplete =
|
|
134
|
-
purchasedItems.length > 0 &&
|
|
135
|
-
purchasedItems.every(
|
|
136
|
-
(item) =>
|
|
137
|
-
lineQty(item, "delivered_quantity") > 0 ||
|
|
138
|
-
lineQty(item, "fulfilled_quantity") > 0
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
return {
|
|
142
|
-
purchasedItems,
|
|
143
|
-
outboundItems,
|
|
144
|
-
inboundItems,
|
|
145
|
-
reorderItems: purchasedItems,
|
|
146
|
-
paidSubtotal,
|
|
147
|
-
paidTotal: paidSubtotal + shipping,
|
|
148
|
-
hasActiveExchange,
|
|
149
|
-
originalFulfillmentIsComplete,
|
|
150
|
-
}
|
|
151
|
-
}
|
|
1
|
+
export type OrderLineQuantities = {
|
|
2
|
+
fulfilled_quantity?: number | null
|
|
3
|
+
delivered_quantity?: number | null
|
|
4
|
+
return_requested_quantity?: number | null
|
|
5
|
+
returned_quantity?: number | null
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type OrderLineLike = {
|
|
9
|
+
id: string
|
|
10
|
+
product_id?: string | null
|
|
11
|
+
variant_id?: string | null
|
|
12
|
+
quantity?: number | null
|
|
13
|
+
total?: number | null
|
|
14
|
+
unit_price?: number | null
|
|
15
|
+
created_at?: string | Date | null
|
|
16
|
+
detail?: OrderLineQuantities | null
|
|
17
|
+
} & OrderLineQuantities
|
|
18
|
+
|
|
19
|
+
export type OrderExchangeLike = {
|
|
20
|
+
canceled_at?: string | Date | null
|
|
21
|
+
additional_items?: Array<{ id?: string | null; item_id?: string | null }> | null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type OrderLike = {
|
|
25
|
+
items?: OrderLineLike[] | null
|
|
26
|
+
shipping_total?: number | null
|
|
27
|
+
total?: number | null
|
|
28
|
+
exchanges?: OrderExchangeLike[] | null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function qty(value?: number | null): number {
|
|
32
|
+
const n = Number(value)
|
|
33
|
+
return Number.isFinite(n) ? n : 0
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function lineQty(item: OrderLineLike, key: keyof OrderLineQuantities): number {
|
|
37
|
+
return qty(item.detail?.[key] ?? item[key])
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function itemCreatedAt(item: OrderLineLike): number {
|
|
41
|
+
if (!item.created_at) return 0
|
|
42
|
+
const t = new Date(item.created_at).getTime()
|
|
43
|
+
return Number.isFinite(t) ? t : 0
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isCanceled(value?: string | Date | null): boolean {
|
|
47
|
+
return Boolean(value)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isInboundExchangeItem(item: OrderLineLike): boolean {
|
|
51
|
+
return (
|
|
52
|
+
lineQty(item, "return_requested_quantity") > 0 ||
|
|
53
|
+
lineQty(item, "returned_quantity") > 0
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function outboundExchangeItemIds(
|
|
58
|
+
items: OrderLineLike[],
|
|
59
|
+
exchanges?: OrderExchangeLike[] | null
|
|
60
|
+
): Set<string> {
|
|
61
|
+
const ids = new Set<string>()
|
|
62
|
+
for (const exchange of exchanges || []) {
|
|
63
|
+
if (isCanceled(exchange.canceled_at)) continue
|
|
64
|
+
for (const extra of exchange.additional_items || []) {
|
|
65
|
+
const id = extra.item_id || extra.id
|
|
66
|
+
if (id) ids.add(id)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (ids.size > 0) return ids
|
|
70
|
+
|
|
71
|
+
const inbound = items.filter(isInboundExchangeItem)
|
|
72
|
+
if (!inbound.length) return ids
|
|
73
|
+
|
|
74
|
+
const inboundIds = new Set(inbound.map((item) => item.id))
|
|
75
|
+
const inboundByProduct = new Map<string, OrderLineLike[]>()
|
|
76
|
+
for (const item of inbound) {
|
|
77
|
+
if (!item.product_id) continue
|
|
78
|
+
const list = inboundByProduct.get(item.product_id) || []
|
|
79
|
+
list.push(item)
|
|
80
|
+
inboundByProduct.set(item.product_id, list)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const item of items) {
|
|
84
|
+
if (inboundIds.has(item.id) || !item.product_id) continue
|
|
85
|
+
const matches = inboundByProduct.get(item.product_id)
|
|
86
|
+
if (!matches?.length) continue
|
|
87
|
+
|
|
88
|
+
const laterThanInbound = matches.some((origin) => {
|
|
89
|
+
const originAt = itemCreatedAt(origin)
|
|
90
|
+
const candidateAt = itemCreatedAt(item)
|
|
91
|
+
return originAt > 0 && candidateAt > originAt + 1000
|
|
92
|
+
})
|
|
93
|
+
const pendingReplacement =
|
|
94
|
+
lineQty(item, "fulfilled_quantity") === 0 &&
|
|
95
|
+
lineQty(item, "delivered_quantity") === 0
|
|
96
|
+
|
|
97
|
+
if (laterThanInbound || pendingReplacement) {
|
|
98
|
+
ids.add(item.id)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return ids
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function presentOrderLines<T extends OrderLineLike>(order: {
|
|
106
|
+
items?: T[] | null
|
|
107
|
+
shipping_total?: number | null
|
|
108
|
+
total?: number | null
|
|
109
|
+
exchanges?: OrderExchangeLike[] | null
|
|
110
|
+
}): {
|
|
111
|
+
purchasedItems: T[]
|
|
112
|
+
outboundItems: T[]
|
|
113
|
+
inboundItems: T[]
|
|
114
|
+
reorderItems: T[]
|
|
115
|
+
paidSubtotal: number
|
|
116
|
+
paidTotal: number
|
|
117
|
+
hasActiveExchange: boolean
|
|
118
|
+
originalFulfillmentIsComplete: boolean
|
|
119
|
+
} {
|
|
120
|
+
const items = (order.items || []) as T[]
|
|
121
|
+
const outboundIds = outboundExchangeItemIds(items, order.exchanges)
|
|
122
|
+
const outboundItems = items.filter((item) => outboundIds.has(item.id))
|
|
123
|
+
const purchasedItems = items.filter((item) => !outboundIds.has(item.id))
|
|
124
|
+
const inboundItems = purchasedItems.filter(isInboundExchangeItem)
|
|
125
|
+
const paidSubtotal = purchasedItems.reduce((sum, item) => {
|
|
126
|
+
if (typeof item.total === "number") return sum + item.total
|
|
127
|
+
return sum + qty(item.unit_price) * qty(item.quantity)
|
|
128
|
+
}, 0)
|
|
129
|
+
const shipping = qty(order.shipping_total)
|
|
130
|
+
const hasActiveExchange = inboundItems.some(
|
|
131
|
+
(item) => lineQty(item, "return_requested_quantity") > 0
|
|
132
|
+
)
|
|
133
|
+
const originalFulfillmentIsComplete =
|
|
134
|
+
purchasedItems.length > 0 &&
|
|
135
|
+
purchasedItems.every(
|
|
136
|
+
(item) =>
|
|
137
|
+
lineQty(item, "delivered_quantity") > 0 ||
|
|
138
|
+
lineQty(item, "fulfilled_quantity") > 0
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
purchasedItems,
|
|
143
|
+
outboundItems,
|
|
144
|
+
inboundItems,
|
|
145
|
+
reorderItems: purchasedItems,
|
|
146
|
+
paidSubtotal,
|
|
147
|
+
paidTotal: paidSubtotal + shipping,
|
|
148
|
+
hasActiveExchange,
|
|
149
|
+
originalFulfillmentIsComplete,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
createContext,
|
|
5
|
-
useContext,
|
|
6
|
-
useMemo,
|
|
7
|
-
useState,
|
|
8
|
-
type Dispatch,
|
|
9
|
-
type ReactNode,
|
|
10
|
-
type SetStateAction,
|
|
11
|
-
} from "react"
|
|
12
|
-
import type { ProductOptionsMap } from "./product-variants"
|
|
13
|
-
|
|
14
|
-
type ProductSelectionContextValue = {
|
|
15
|
-
selectedOptions: ProductOptionsMap
|
|
16
|
-
setSelectedOptions: Dispatch<SetStateAction<ProductOptionsMap>>
|
|
17
|
-
selectedVariantId: string | undefined
|
|
18
|
-
setSelectedVariantId: Dispatch<SetStateAction<string | undefined>>
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const ProductSelectionContext = createContext<ProductSelectionContextValue | undefined>(
|
|
22
|
-
undefined
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
export function ProductSelectionProvider({
|
|
26
|
-
children,
|
|
27
|
-
initialOptions = {},
|
|
28
|
-
initialVariantId,
|
|
29
|
-
}: {
|
|
30
|
-
children: ReactNode
|
|
31
|
-
initialOptions?: ProductOptionsMap
|
|
32
|
-
initialVariantId?: string
|
|
33
|
-
}) {
|
|
34
|
-
const [selectedOptions, setSelectedOptions] = useState<ProductOptionsMap>(initialOptions)
|
|
35
|
-
const [selectedVariantId, setSelectedVariantId] = useState<string | undefined>(
|
|
36
|
-
initialVariantId
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
const value = useMemo(
|
|
40
|
-
() => ({
|
|
41
|
-
selectedOptions,
|
|
42
|
-
setSelectedOptions,
|
|
43
|
-
selectedVariantId,
|
|
44
|
-
setSelectedVariantId,
|
|
45
|
-
}),
|
|
46
|
-
[selectedOptions, selectedVariantId]
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
return (
|
|
50
|
-
<ProductSelectionContext.Provider value={value}>{children}</ProductSelectionContext.Provider>
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function useProductSelection() {
|
|
55
|
-
return useContext(ProductSelectionContext)
|
|
56
|
-
}
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useContext,
|
|
6
|
+
useMemo,
|
|
7
|
+
useState,
|
|
8
|
+
type Dispatch,
|
|
9
|
+
type ReactNode,
|
|
10
|
+
type SetStateAction,
|
|
11
|
+
} from "react"
|
|
12
|
+
import type { ProductOptionsMap } from "./product-variants"
|
|
13
|
+
|
|
14
|
+
type ProductSelectionContextValue = {
|
|
15
|
+
selectedOptions: ProductOptionsMap
|
|
16
|
+
setSelectedOptions: Dispatch<SetStateAction<ProductOptionsMap>>
|
|
17
|
+
selectedVariantId: string | undefined
|
|
18
|
+
setSelectedVariantId: Dispatch<SetStateAction<string | undefined>>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ProductSelectionContext = createContext<ProductSelectionContextValue | undefined>(
|
|
22
|
+
undefined
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
export function ProductSelectionProvider({
|
|
26
|
+
children,
|
|
27
|
+
initialOptions = {},
|
|
28
|
+
initialVariantId,
|
|
29
|
+
}: {
|
|
30
|
+
children: ReactNode
|
|
31
|
+
initialOptions?: ProductOptionsMap
|
|
32
|
+
initialVariantId?: string
|
|
33
|
+
}) {
|
|
34
|
+
const [selectedOptions, setSelectedOptions] = useState<ProductOptionsMap>(initialOptions)
|
|
35
|
+
const [selectedVariantId, setSelectedVariantId] = useState<string | undefined>(
|
|
36
|
+
initialVariantId
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
const value = useMemo(
|
|
40
|
+
() => ({
|
|
41
|
+
selectedOptions,
|
|
42
|
+
setSelectedOptions,
|
|
43
|
+
selectedVariantId,
|
|
44
|
+
setSelectedVariantId,
|
|
45
|
+
}),
|
|
46
|
+
[selectedOptions, selectedVariantId]
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<ProductSelectionContext.Provider value={value}>{children}</ProductSelectionContext.Provider>
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function useProductSelection() {
|
|
55
|
+
return useContext(ProductSelectionContext)
|
|
56
|
+
}
|