@pradip1995/segment-primitives 0.2.0
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 +38 -0
- package/src/Button.tsx +19 -0
- package/src/Card.tsx +10 -0
- package/src/coupon-apply.tsx +181 -0
- package/src/format-price.ts +12 -0
- package/src/index.ts +4 -0
- package/src/localized-link.tsx +37 -0
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pradip1995/segment-primitives",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"files": [
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.ts",
|
|
14
|
+
"./localized-link": "./src/localized-link.tsx",
|
|
15
|
+
"./coupon-apply": "./src/coupon-apply.tsx",
|
|
16
|
+
"./button": "./src/Button.tsx",
|
|
17
|
+
"./card": "./src/Card.tsx",
|
|
18
|
+
"./format-price": "./src/format-price.ts"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@pradip1995/commerce-core": "^4.0.0",
|
|
22
|
+
"@pradip1995/plugin-sdk": "^0.2.0",
|
|
23
|
+
"react": ">=19",
|
|
24
|
+
"react-dom": ">=19",
|
|
25
|
+
"next": ">=15"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@pradip1995/plugin-sdk": "^0.2.0",
|
|
30
|
+
"@types/react": "^19",
|
|
31
|
+
"react": "19.0.3",
|
|
32
|
+
"typescript": "^5.7.2"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"lint": "tsc --noEmit"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/Button.tsx
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ButtonHTMLAttributes, ReactNode } from "react"
|
|
2
|
+
|
|
3
|
+
type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
|
|
4
|
+
children: ReactNode
|
|
5
|
+
variant?: "primary" | "secondary"
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function Button({ children, variant = "primary", className = "", ...props }: ButtonProps) {
|
|
9
|
+
const base = "inline-flex items-center justify-center px-4 py-2 text-sm font-semibold transition-colors"
|
|
10
|
+
const styles =
|
|
11
|
+
variant === "primary"
|
|
12
|
+
? "bg-brand-accent text-inverse"
|
|
13
|
+
: "border border-brand-accent text-brand-accent"
|
|
14
|
+
return (
|
|
15
|
+
<button className={`${base} ${styles} ${className}`} {...props}>
|
|
16
|
+
{children}
|
|
17
|
+
</button>
|
|
18
|
+
)
|
|
19
|
+
}
|
package/src/Card.tsx
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
|
|
3
|
+
type CardProps = {
|
|
4
|
+
children: ReactNode
|
|
5
|
+
className?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function Card({ children, className = "" }: CardProps) {
|
|
9
|
+
return <div className={`bg-surface rounded-lg p-4 ${className}`}>{children}</div>
|
|
10
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useState } from "react"
|
|
4
|
+
import { useRouter } from "next/navigation"
|
|
5
|
+
import type { HttpTypes } from "@medusajs/types"
|
|
6
|
+
import { applyPromotions } from "@pradip1995/commerce-core/client/actions/cart"
|
|
7
|
+
import { formatPrice } from "./format-price"
|
|
8
|
+
|
|
9
|
+
type CouponApplyProps = {
|
|
10
|
+
cart: HttpTypes.StoreCart & { promotions?: HttpTypes.StorePromotion[] }
|
|
11
|
+
className?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function promotionLabel(promotion: HttpTypes.StorePromotion) {
|
|
15
|
+
const method = promotion.application_method
|
|
16
|
+
if (method?.value == null) return null
|
|
17
|
+
|
|
18
|
+
if (method.type === "percentage") {
|
|
19
|
+
return `${method.value}% off`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return `${formatPrice(Number(method.value), method.currency_code || "inr")} off`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default function CouponApply({ cart, className = "" }: CouponApplyProps) {
|
|
26
|
+
const router = useRouter()
|
|
27
|
+
const [isOpen, setIsOpen] = useState(false)
|
|
28
|
+
const [code, setCode] = useState("")
|
|
29
|
+
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
30
|
+
const [errorMessage, setErrorMessage] = useState("")
|
|
31
|
+
const [successMessage, setSuccessMessage] = useState("")
|
|
32
|
+
|
|
33
|
+
const promotions = cart.promotions ?? []
|
|
34
|
+
|
|
35
|
+
const refreshCart = () => {
|
|
36
|
+
router.refresh()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const removePromotionCode = async (promotionCode: string) => {
|
|
40
|
+
if (!cart.id) return
|
|
41
|
+
|
|
42
|
+
setErrorMessage("")
|
|
43
|
+
setSuccessMessage("")
|
|
44
|
+
setIsSubmitting(true)
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const remaining = promotions
|
|
48
|
+
.filter((promotion) => promotion.code && promotion.code !== promotionCode)
|
|
49
|
+
.map((promotion) => promotion.code!)
|
|
50
|
+
await applyPromotions(remaining, cart.id)
|
|
51
|
+
refreshCart()
|
|
52
|
+
} catch {
|
|
53
|
+
setErrorMessage("Could not remove coupon. Please try again.")
|
|
54
|
+
} finally {
|
|
55
|
+
setIsSubmitting(false)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const handleApply = async (event: React.FormEvent) => {
|
|
60
|
+
event.preventDefault()
|
|
61
|
+
|
|
62
|
+
const trimmed = code.trim()
|
|
63
|
+
if (!trimmed || !cart.id) return
|
|
64
|
+
|
|
65
|
+
setErrorMessage("")
|
|
66
|
+
setSuccessMessage("")
|
|
67
|
+
setIsSubmitting(true)
|
|
68
|
+
|
|
69
|
+
const codes = promotions.filter((p) => p.code).map((p) => p.code!)
|
|
70
|
+
if (!codes.includes(trimmed.toUpperCase()) && !codes.includes(trimmed)) {
|
|
71
|
+
codes.push(trimmed)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
const updatedCart = await applyPromotions(codes, cart.id)
|
|
76
|
+
const hasDiscount = (updatedCart?.discount_total ?? 0) > 0
|
|
77
|
+
|
|
78
|
+
if (hasDiscount) {
|
|
79
|
+
setSuccessMessage("Coupon applied successfully!")
|
|
80
|
+
} else {
|
|
81
|
+
setErrorMessage("Coupon added, but it doesn't apply to the items in your bag.")
|
|
82
|
+
}
|
|
83
|
+
setCode("")
|
|
84
|
+
refreshCart()
|
|
85
|
+
} catch {
|
|
86
|
+
setErrorMessage("Invalid coupon code. Please check the code and try again.")
|
|
87
|
+
} finally {
|
|
88
|
+
setIsSubmitting(false)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<div className={`coupon-apply ${className}`.trim()}>
|
|
94
|
+
<button
|
|
95
|
+
type="button"
|
|
96
|
+
className="coupon-apply__toggle"
|
|
97
|
+
onClick={() => setIsOpen((open) => !open)}
|
|
98
|
+
aria-expanded={isOpen}
|
|
99
|
+
data-testid="add-discount-button"
|
|
100
|
+
>
|
|
101
|
+
<span className="coupon-apply__toggle-icon" aria-hidden>
|
|
102
|
+
%
|
|
103
|
+
</span>
|
|
104
|
+
<span className="coupon-apply__toggle-copy">
|
|
105
|
+
<span className="coupon-apply__toggle-title">Apply coupon</span>
|
|
106
|
+
<span className="coupon-apply__toggle-subtitle">Have a promo code?</span>
|
|
107
|
+
</span>
|
|
108
|
+
<span className={`coupon-apply__chevron${isOpen ? " coupon-apply__chevron--open" : ""}`} aria-hidden>
|
|
109
|
+
›
|
|
110
|
+
</span>
|
|
111
|
+
</button>
|
|
112
|
+
|
|
113
|
+
{isOpen ? (
|
|
114
|
+
<form className="coupon-apply__form" onSubmit={handleApply}>
|
|
115
|
+
<input
|
|
116
|
+
className="coupon-apply__input"
|
|
117
|
+
name="code"
|
|
118
|
+
type="text"
|
|
119
|
+
value={code}
|
|
120
|
+
onChange={(event) => setCode(event.target.value)}
|
|
121
|
+
placeholder="Enter coupon code"
|
|
122
|
+
autoComplete="off"
|
|
123
|
+
data-testid="discount-input"
|
|
124
|
+
/>
|
|
125
|
+
<button
|
|
126
|
+
type="submit"
|
|
127
|
+
className="coupon-apply__submit"
|
|
128
|
+
disabled={isSubmitting || !code.trim()}
|
|
129
|
+
data-testid="discount-apply-button"
|
|
130
|
+
>
|
|
131
|
+
{isSubmitting ? "Applying…" : "Apply"}
|
|
132
|
+
</button>
|
|
133
|
+
</form>
|
|
134
|
+
) : null}
|
|
135
|
+
|
|
136
|
+
{errorMessage ? (
|
|
137
|
+
<p className="coupon-apply__message coupon-apply__message--error" data-testid="discount-error-message">
|
|
138
|
+
{errorMessage}
|
|
139
|
+
</p>
|
|
140
|
+
) : null}
|
|
141
|
+
|
|
142
|
+
{successMessage ? (
|
|
143
|
+
<p className="coupon-apply__message coupon-apply__message--success" data-testid="discount-success-message">
|
|
144
|
+
{successMessage}
|
|
145
|
+
</p>
|
|
146
|
+
) : null}
|
|
147
|
+
|
|
148
|
+
{promotions.length > 0 ? (
|
|
149
|
+
<div className="coupon-apply__applied">
|
|
150
|
+
<p className="coupon-apply__applied-title">Applied offers</p>
|
|
151
|
+
<ul className="coupon-apply__list">
|
|
152
|
+
{promotions.map((promotion) => (
|
|
153
|
+
<li key={promotion.id} className="coupon-apply__item" data-testid="discount-row">
|
|
154
|
+
<div className="coupon-apply__item-copy">
|
|
155
|
+
<span className="coupon-apply__item-code" data-testid="discount-code">
|
|
156
|
+
{promotion.is_automatic ? "Automatic offer" : promotion.code}
|
|
157
|
+
</span>
|
|
158
|
+
{promotionLabel(promotion) ? (
|
|
159
|
+
<span className="coupon-apply__item-value">{promotionLabel(promotion)}</span>
|
|
160
|
+
) : null}
|
|
161
|
+
</div>
|
|
162
|
+
{!promotion.is_automatic && promotion.code ? (
|
|
163
|
+
<button
|
|
164
|
+
type="button"
|
|
165
|
+
className="coupon-apply__remove"
|
|
166
|
+
onClick={() => removePromotionCode(promotion.code!)}
|
|
167
|
+
disabled={isSubmitting}
|
|
168
|
+
aria-label={`Remove coupon ${promotion.code}`}
|
|
169
|
+
data-testid="remove-discount-button"
|
|
170
|
+
>
|
|
171
|
+
Remove
|
|
172
|
+
</button>
|
|
173
|
+
) : null}
|
|
174
|
+
</li>
|
|
175
|
+
))}
|
|
176
|
+
</ul>
|
|
177
|
+
</div>
|
|
178
|
+
) : null}
|
|
179
|
+
</div>
|
|
180
|
+
)
|
|
181
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function formatPrice(
|
|
2
|
+
amount: number | undefined | null,
|
|
3
|
+
currencyCode = "inr"
|
|
4
|
+
): string {
|
|
5
|
+
if (amount == null) return ""
|
|
6
|
+
return new Intl.NumberFormat("en-IN", {
|
|
7
|
+
style: "currency",
|
|
8
|
+
currency: currencyCode.toUpperCase(),
|
|
9
|
+
minimumFractionDigits: 0,
|
|
10
|
+
maximumFractionDigits: 2,
|
|
11
|
+
}).format(amount / 100)
|
|
12
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import Link from "next/link"
|
|
4
|
+
import type { ComponentProps } from "react"
|
|
5
|
+
|
|
6
|
+
type Props = ComponentProps<typeof Link> & {
|
|
7
|
+
href: string
|
|
8
|
+
/** Region prefix (e.g. `in`). Prefer passing from layout data for stable SSR. */
|
|
9
|
+
countryCode?: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const DEFAULT_REGION = process.env.NEXT_PUBLIC_DEFAULT_REGION || "in"
|
|
13
|
+
|
|
14
|
+
export default function LocalizedLink({
|
|
15
|
+
href,
|
|
16
|
+
children,
|
|
17
|
+
countryCode,
|
|
18
|
+
...rest
|
|
19
|
+
}: Props) {
|
|
20
|
+
const region = (countryCode || DEFAULT_REGION).toLowerCase()
|
|
21
|
+
|
|
22
|
+
if (!href.startsWith("/")) {
|
|
23
|
+
return (
|
|
24
|
+
<Link href={href} {...rest}>
|
|
25
|
+
{children}
|
|
26
|
+
</Link>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const localizedHref = href === "/" ? `/${region}` : `/${region}${href}`
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<Link href={localizedHref} {...rest}>
|
|
34
|
+
{children}
|
|
35
|
+
</Link>
|
|
36
|
+
)
|
|
37
|
+
}
|