@pradip1995/segment-primitives 0.4.8 → 0.4.10
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pradip1995/segment-primitives",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.10",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -22,10 +22,6 @@
|
|
|
22
22
|
"./product-selection-context": "./src/product-selection-context.tsx",
|
|
23
23
|
"./is-next-redirect": "./src/is-next-redirect.ts"
|
|
24
24
|
},
|
|
25
|
-
"scripts": {
|
|
26
|
-
"typecheck": "tsc --noEmit",
|
|
27
|
-
"lint": "tsc --noEmit"
|
|
28
|
-
},
|
|
29
25
|
"peerDependencies": {
|
|
30
26
|
"@medusajs/types": "^2.0.0",
|
|
31
27
|
"@pradip1995/commerce-core": "^4.0.0",
|
|
@@ -40,5 +36,10 @@
|
|
|
40
36
|
"@types/react": "^19",
|
|
41
37
|
"react": "19.0.3",
|
|
42
38
|
"typescript": "^5.7.2"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"typecheck": "tsc --noEmit",
|
|
42
|
+
"lint": "tsc --noEmit",
|
|
43
|
+
"test": "node --experimental-strip-types --test src/*.test.ts"
|
|
43
44
|
}
|
|
44
|
-
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, it } from "node:test"
|
|
2
|
+
import assert from "node:assert/strict"
|
|
3
|
+
import {
|
|
4
|
+
COUPON_APPLY_NO_ITEM_MATCH,
|
|
5
|
+
COUPON_APPLY_SUCCESS,
|
|
6
|
+
couponApplyKind,
|
|
7
|
+
couponApplyMessage,
|
|
8
|
+
type CouponApplyCart,
|
|
9
|
+
type CouponApplyKind,
|
|
10
|
+
} from "./coupon-apply-result.ts"
|
|
11
|
+
|
|
12
|
+
const KIND_CASES: { name: string; cart: CouponApplyCart; want: CouponApplyKind }[] = [
|
|
13
|
+
{
|
|
14
|
+
name: "positive discount_total is success",
|
|
15
|
+
cart: { discount_total: 86 },
|
|
16
|
+
want: "success",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: "explicit zero discount_total means the code did not hit bag items",
|
|
20
|
+
cart: { discount_total: 0, promotions: [{ code: "WELCOME10" }] },
|
|
21
|
+
want: "no-item-match",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "missing discount_total after a successful apply is not a false error",
|
|
25
|
+
cart: { promotions: [{ code: "WELCOME10" }] },
|
|
26
|
+
want: "success",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "null discount_total after a successful apply is not a false error",
|
|
30
|
+
cart: { discount_total: null },
|
|
31
|
+
want: "success",
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: "retrieve failure after a successful apply is not a false error",
|
|
35
|
+
cart: null,
|
|
36
|
+
want: "success",
|
|
37
|
+
},
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
const MESSAGE_CASES: { name: string; kind: CouponApplyKind; wantError: string; wantSuccess: string }[] =
|
|
41
|
+
[
|
|
42
|
+
{
|
|
43
|
+
name: "success copy",
|
|
44
|
+
kind: "success",
|
|
45
|
+
wantError: "",
|
|
46
|
+
wantSuccess: COUPON_APPLY_SUCCESS,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: "no-item-match copy",
|
|
50
|
+
kind: "no-item-match",
|
|
51
|
+
wantError: COUPON_APPLY_NO_ITEM_MATCH,
|
|
52
|
+
wantSuccess: "",
|
|
53
|
+
},
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
describe("couponApplyKind", () => {
|
|
57
|
+
for (const tc of KIND_CASES) {
|
|
58
|
+
it(tc.name, () => {
|
|
59
|
+
assert.equal(couponApplyKind(tc.cart), tc.want)
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
describe("couponApplyMessage", () => {
|
|
65
|
+
for (const tc of MESSAGE_CASES) {
|
|
66
|
+
it(tc.name, () => {
|
|
67
|
+
assert.deepEqual(couponApplyMessage(tc.kind), {
|
|
68
|
+
error: tc.wantError,
|
|
69
|
+
success: tc.wantSuccess,
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type CouponApplyKind = "success" | "no-item-match"
|
|
2
|
+
|
|
3
|
+
export type CouponApplyCart = {
|
|
4
|
+
discount_total?: number | null
|
|
5
|
+
promotions?: { code?: string | null }[] | null
|
|
6
|
+
} | null
|
|
7
|
+
|
|
8
|
+
export const COUPON_APPLY_SUCCESS = "Coupon applied successfully!"
|
|
9
|
+
export const COUPON_APPLY_NO_ITEM_MATCH =
|
|
10
|
+
"Coupon added, but it doesn't apply to the items in your bag."
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* `applyPromotions` already succeeded. Decide checkout copy from the cart
|
|
14
|
+
* fetched afterwards — missing `discount_total` must not look like a failed coupon.
|
|
15
|
+
*/
|
|
16
|
+
export function couponApplyKind(cart: CouponApplyCart): CouponApplyKind {
|
|
17
|
+
if (cart == null) {
|
|
18
|
+
return "success"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (cart.discount_total != null && Number(cart.discount_total) === 0) {
|
|
22
|
+
return "no-item-match"
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (Number(cart.discount_total ?? 0) > 0) {
|
|
26
|
+
return "success"
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return "success"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function couponApplyMessage(kind: CouponApplyKind): {
|
|
33
|
+
error: string
|
|
34
|
+
success: string
|
|
35
|
+
} {
|
|
36
|
+
if (kind === "success") {
|
|
37
|
+
return { error: "", success: COUPON_APPLY_SUCCESS }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return { error: COUPON_APPLY_NO_ITEM_MATCH, success: "" }
|
|
41
|
+
}
|
package/src/coupon-apply.tsx
CHANGED
|
@@ -3,15 +3,21 @@
|
|
|
3
3
|
import { useState } from "react"
|
|
4
4
|
import { useRouter } from "next/navigation"
|
|
5
5
|
import type { HttpTypes } from "@medusajs/types"
|
|
6
|
-
import { applyPromotions } from "@pradip1995/commerce-core/client/actions/cart"
|
|
7
6
|
import { formatPrice } from "./format-price"
|
|
7
|
+
import { couponApplyKind, couponApplyMessage } from "./coupon-apply-result"
|
|
8
|
+
|
|
9
|
+
// Use require to prevent TS from attempting to typecheck the raw .ts files published in commerce-core
|
|
10
|
+
const { applyPromotions, retrieveCart } = require("@pradip1995/commerce-core/client/actions/cart") as {
|
|
11
|
+
applyPromotions: (codes: string[], cartId: string) => Promise<any>
|
|
12
|
+
retrieveCart: (cartId?: string) => Promise<any>
|
|
13
|
+
}
|
|
8
14
|
|
|
9
15
|
type CouponApplyProps = {
|
|
10
16
|
cart: HttpTypes.StoreCart & { promotions?: HttpTypes.StorePromotion[] }
|
|
11
17
|
className?: string
|
|
12
18
|
}
|
|
13
19
|
|
|
14
|
-
function promotionLabel(promotion:
|
|
20
|
+
function promotionLabel(promotion: any) {
|
|
15
21
|
const method = promotion.application_method
|
|
16
22
|
if (method?.value == null) return null
|
|
17
23
|
|
|
@@ -73,13 +79,10 @@ export default function CouponApply({ cart, className = "" }: CouponApplyProps)
|
|
|
73
79
|
|
|
74
80
|
try {
|
|
75
81
|
const updatedCart = await applyPromotions(codes, cart.id)
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
} else {
|
|
81
|
-
setErrorMessage("Coupon added, but it doesn't apply to the items in your bag.")
|
|
82
|
-
}
|
|
82
|
+
const freshCart = (await retrieveCart(cart.id).catch(() => null)) ?? updatedCart
|
|
83
|
+
const { error, success } = couponApplyMessage(couponApplyKind(freshCart))
|
|
84
|
+
setErrorMessage(error)
|
|
85
|
+
setSuccessMessage(success)
|
|
83
86
|
setCode("")
|
|
84
87
|
refreshCart()
|
|
85
88
|
} catch {
|
package/src/resolve-media-url.ts
CHANGED
|
@@ -9,8 +9,9 @@ export function resolveMediaUrl(
|
|
|
9
9
|
|
|
10
10
|
// Uploads stored while AWS_FILE_URL / backend_url was unset:
|
|
11
11
|
// "undefined/uploadsFoo.webp" or "undefined/uploads/Foo.webp"
|
|
12
|
-
|
|
13
|
-
if (!
|
|
12
|
+
const rewritten = rewriteBrokenUndefinedUploadUrl(trimmed)
|
|
13
|
+
if (!rewritten) return null
|
|
14
|
+
trimmed = rewritten
|
|
14
15
|
|
|
15
16
|
if (trimmed.startsWith("/")) {
|
|
16
17
|
return trimmed
|