@pradip1995/segment-primitives 0.4.12 → 0.4.14
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 +7 -2
- package/src/index.ts +5 -0
- package/src/notify-wishlist.ts +9 -0
- package/src/revalidate-wishlist.ts +8 -0
- package/src/use-wishlist-count.ts +32 -0
- package/src/wishlist-count.test.ts +96 -0
- package/src/wishlist-count.ts +26 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pradip1995/segment-primitives",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.14",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -21,7 +21,11 @@
|
|
|
21
21
|
"./product-variants": "./src/product-variants.ts",
|
|
22
22
|
"./product-selection-context": "./src/product-selection-context.tsx",
|
|
23
23
|
"./is-next-redirect": "./src/is-next-redirect.ts",
|
|
24
|
-
"./order-lines": "./src/order-lines.ts"
|
|
24
|
+
"./order-lines": "./src/order-lines.ts",
|
|
25
|
+
"./wishlist-count": "./src/wishlist-count.ts",
|
|
26
|
+
"./use-wishlist-count": "./src/use-wishlist-count.ts",
|
|
27
|
+
"./notify-wishlist": "./src/notify-wishlist.ts",
|
|
28
|
+
"./revalidate-wishlist": "./src/revalidate-wishlist.ts"
|
|
25
29
|
},
|
|
26
30
|
"peerDependencies": {
|
|
27
31
|
"@medusajs/types": "^2.0.0",
|
|
@@ -35,6 +39,7 @@
|
|
|
35
39
|
"@medusajs/types": "^2.0.0",
|
|
36
40
|
"@pradip1995/plugin-sdk": "^0.2.0",
|
|
37
41
|
"@types/react": "^19",
|
|
42
|
+
"next": ">=15",
|
|
38
43
|
"react": "19.0.3",
|
|
39
44
|
"typescript": "^5.7.2"
|
|
40
45
|
},
|
package/src/index.ts
CHANGED
|
@@ -2,3 +2,8 @@ export { default as LocalizedLink } from "./localized-link"
|
|
|
2
2
|
export { default as CouponApply } from "./coupon-apply"
|
|
3
3
|
export { Button } from "./Button"
|
|
4
4
|
export { Card } from "./Card"
|
|
5
|
+
export {
|
|
6
|
+
isInboundExchangeItem,
|
|
7
|
+
outboundExchangeItemIds,
|
|
8
|
+
presentOrderLines,
|
|
9
|
+
} from "./order-lines"
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { revalidateWishlistCache } from "./revalidate-wishlist"
|
|
4
|
+
import { dispatchWishlistUpdated, type WishlistUpdatedDetail } from "./wishlist-count"
|
|
5
|
+
|
|
6
|
+
export async function notifyWishlistChanged(detail: WishlistUpdatedDetail) {
|
|
7
|
+
dispatchWishlistUpdated(detail)
|
|
8
|
+
await revalidateWishlistCache()
|
|
9
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef, useState } from "react"
|
|
4
|
+
import {
|
|
5
|
+
WISHLIST_UPDATED_EVENT,
|
|
6
|
+
nextWishlistCount,
|
|
7
|
+
type WishlistUpdatedDetail,
|
|
8
|
+
} from "./wishlist-count"
|
|
9
|
+
|
|
10
|
+
export function useWishlistCount(serverCount: number) {
|
|
11
|
+
const initial = nextWishlistCount(serverCount)
|
|
12
|
+
const [count, setCount] = useState(initial)
|
|
13
|
+
const lastServerCount = useRef(initial)
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const next = nextWishlistCount(serverCount)
|
|
17
|
+
if (next === lastServerCount.current) return
|
|
18
|
+
lastServerCount.current = next
|
|
19
|
+
setCount(next)
|
|
20
|
+
}, [serverCount])
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const onUpdate = (event: Event) => {
|
|
24
|
+
const detail = (event as CustomEvent<WishlistUpdatedDetail>).detail
|
|
25
|
+
setCount((current) => nextWishlistCount(current, detail))
|
|
26
|
+
}
|
|
27
|
+
window.addEventListener(WISHLIST_UPDATED_EVENT, onUpdate)
|
|
28
|
+
return () => window.removeEventListener(WISHLIST_UPDATED_EVENT, onUpdate)
|
|
29
|
+
}, [])
|
|
30
|
+
|
|
31
|
+
return count
|
|
32
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import { nextWishlistCount, type WishlistUpdatedDetail } from "./wishlist-count.ts"
|
|
4
|
+
|
|
5
|
+
const CASES: Array<{
|
|
6
|
+
name: string
|
|
7
|
+
current: number
|
|
8
|
+
detail?: WishlistUpdatedDetail | null
|
|
9
|
+
want: number
|
|
10
|
+
}> = [
|
|
11
|
+
{
|
|
12
|
+
name: "removing one item decrements the navbar badge",
|
|
13
|
+
current: 2,
|
|
14
|
+
detail: { delta: -1 },
|
|
15
|
+
want: 1,
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
name: "removing the last item clears the badge",
|
|
19
|
+
current: 1,
|
|
20
|
+
detail: { delta: -1 },
|
|
21
|
+
want: 0,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "removing from an empty wishlist stays at zero",
|
|
25
|
+
current: 0,
|
|
26
|
+
detail: { delta: -1 },
|
|
27
|
+
want: 0,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: "adding an item increments the badge",
|
|
31
|
+
current: 0,
|
|
32
|
+
detail: { delta: 1 },
|
|
33
|
+
want: 1,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "adding onto an existing count increments the badge",
|
|
37
|
+
current: 2,
|
|
38
|
+
detail: { delta: 1 },
|
|
39
|
+
want: 3,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "an explicit count replaces the current badge",
|
|
43
|
+
current: 4,
|
|
44
|
+
detail: { count: 1 },
|
|
45
|
+
want: 1,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "an explicit count wins when delta is also present",
|
|
49
|
+
current: 4,
|
|
50
|
+
detail: { count: 0, delta: 2 },
|
|
51
|
+
want: 0,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "missing detail keeps the current badge",
|
|
55
|
+
current: 3,
|
|
56
|
+
want: 3,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "null detail keeps the current badge",
|
|
60
|
+
current: 3,
|
|
61
|
+
detail: null,
|
|
62
|
+
want: 3,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "empty detail keeps the current badge",
|
|
66
|
+
current: 3,
|
|
67
|
+
detail: {},
|
|
68
|
+
want: 3,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "non-finite current is treated as zero before applying delta",
|
|
72
|
+
current: Number.NaN,
|
|
73
|
+
detail: { delta: 2 },
|
|
74
|
+
want: 2,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: "non-finite delta is ignored",
|
|
78
|
+
current: 5,
|
|
79
|
+
detail: { delta: Number.NaN },
|
|
80
|
+
want: 5,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: "fractional values are truncated",
|
|
84
|
+
current: 2.9,
|
|
85
|
+
detail: { delta: -1.8 },
|
|
86
|
+
want: 1,
|
|
87
|
+
},
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
describe("nextWishlistCount", () => {
|
|
91
|
+
for (const row of CASES) {
|
|
92
|
+
it(row.name, () => {
|
|
93
|
+
assert.equal(nextWishlistCount(row.current, row.detail), row.want)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const WISHLIST_UPDATED_EVENT = "wishlist:updated"
|
|
2
|
+
|
|
3
|
+
export type WishlistUpdatedDetail = {
|
|
4
|
+
delta?: number
|
|
5
|
+
count?: number
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function nextWishlistCount(
|
|
9
|
+
current: number,
|
|
10
|
+
detail?: WishlistUpdatedDetail | null
|
|
11
|
+
): number {
|
|
12
|
+
const base = Number.isFinite(current) ? Math.max(0, Math.trunc(current)) : 0
|
|
13
|
+
if (!detail) return base
|
|
14
|
+
if (typeof detail.count === "number" && Number.isFinite(detail.count)) {
|
|
15
|
+
return Math.max(0, Math.trunc(detail.count))
|
|
16
|
+
}
|
|
17
|
+
if (typeof detail.delta === "number" && Number.isFinite(detail.delta)) {
|
|
18
|
+
return Math.max(0, base + Math.trunc(detail.delta))
|
|
19
|
+
}
|
|
20
|
+
return base
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function dispatchWishlistUpdated(detail: WishlistUpdatedDetail) {
|
|
24
|
+
if (typeof window === "undefined") return
|
|
25
|
+
window.dispatchEvent(new CustomEvent(WISHLIST_UPDATED_EVENT, { detail }))
|
|
26
|
+
}
|