@pradip1995/segment-login-template 0.5.18 → 0.5.20

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-login-template",
3
- "version": "0.5.18",
3
+ "version": "0.5.20",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -34,18 +34,13 @@ export default function AccountGuestOrders({
34
34
  }
35
35
 
36
36
  return (
37
- <div className="space-y-6">
37
+ <div className="sr-guest-orders space-y-6">
38
38
  <div className="flex flex-wrap items-start justify-between gap-4">
39
39
  <div>
40
- <div className="flex items-center gap-2 mb-2" aria-hidden>
41
- <span className="h-px w-6 bg-brand-accent/70" />
42
- <span className="text-brand-accent text-[7px]">◆</span>
43
- <span className="h-px w-6 bg-brand-accent/70" />
44
- </div>
45
- <h2 className="font-heading text-2xl font-bold text-heading uppercase tracking-[0.08em] mb-1">
40
+ <h2 className="sr-guest-orders__title font-heading text-2xl font-bold uppercase tracking-[0.08em] mb-1">
46
41
  Guest orders
47
42
  </h2>
48
- <p className="text-sm text-muted">
43
+ <p className="sr-guest-orders__copy text-sm">
49
44
  Track orders linked to your verified email.
50
45
  </p>
51
46
  </div>
@@ -53,20 +48,20 @@ export default function AccountGuestOrders({
53
48
  <button
54
49
  type="button"
55
50
  onClick={handleCheckDifferent}
56
- className="btn-outline text-xs"
51
+ className="sr-guest-orders__btn"
57
52
  >
58
53
  Check different email
59
54
  </button>
60
- <button type="button" onClick={handleSignOut} className="btn-outline text-xs">
55
+ <button type="button" onClick={handleSignOut} className="sr-guest-orders__btn">
61
56
  End guest session
62
57
  </button>
63
58
  </div>
64
59
  </div>
65
60
 
66
61
  {safeOrders.length === 0 ? (
67
- <div className="border border-cart-border bg-surface p-8 text-center">
68
- <p className="text-sm text-muted mb-4">No guest orders found for this session.</p>
69
- <LocalizedLink href="/store" className={acct.btnPrimary}>
62
+ <div className="sr-guest-orders__empty p-8 text-center">
63
+ <p className="sr-guest-orders__copy text-sm mb-4">No guest orders found for this session.</p>
64
+ <LocalizedLink href="/store" className={`${acct.btnPrimary} sr-guest-orders__shop`}>
70
65
  Continue shopping
71
66
  </LocalizedLink>
72
67
  </div>
@@ -76,17 +71,17 @@ export default function AccountGuestOrders({
76
71
  <li key={order.id}>
77
72
  <LocalizedLink
78
73
  href={`/orders/${order.id}`}
79
- className="border border-cart-border bg-surface p-5 flex items-center justify-between gap-4 hover:border-brand-accent transition-colors duration-200 block"
74
+ className="sr-guest-orders__card p-5 flex items-center justify-between gap-4 block"
80
75
  >
81
76
  <div>
82
- <p className="font-semibold text-heading text-sm">
77
+ <p className="sr-guest-orders__order font-semibold text-sm">
83
78
  Order #{order.display_id}
84
79
  </p>
85
- <p className="text-xs text-muted mt-1 capitalize">
80
+ <p className="sr-guest-orders__status text-xs mt-1 capitalize">
86
81
  {resolveOrderHistoryStatus(order as OrderHistoryStatusInput).label}
87
82
  </p>
88
83
  </div>
89
- <p className="text-sm font-semibold text-brand-accent shrink-0">
84
+ <p className="sr-guest-orders__total text-sm font-semibold shrink-0">
90
85
  {formatPrice(order.total, order.currency_code)}
91
86
  </p>
92
87
  </LocalizedLink>
@@ -1,12 +1,13 @@
1
1
  "use client"
2
2
 
3
- import { useMemo, useState, type MouseEvent } from "react"
3
+ import { useEffect, useMemo, useState, type MouseEvent } from "react"
4
4
  import { usePathname, useRouter } from "next/navigation"
5
5
  import type { HttpTypes } from "@medusajs/types"
6
6
  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,
@@ -45,12 +46,29 @@ function statusStyle(tone: OrderHistoryStatusTone) {
45
46
  return "bg-surface-muted text-muted border-cart-border"
46
47
  }
47
48
 
49
+ const DEFAULT_PAGE_SIZE = 3
50
+
51
+ function paginationItems(current: number, total: number) {
52
+ if (total <= 7) return Array.from({ length: total }, (_, index) => index + 1)
53
+ const items: (number | "ellipsis")[] = [1]
54
+ const start = Math.max(2, current - 1)
55
+ const end = Math.min(total - 1, current + 1)
56
+ if (start > 2) items.push("ellipsis")
57
+ for (let page = start; page <= end; page += 1) items.push(page)
58
+ if (end < total - 1) items.push("ellipsis")
59
+ items.push(total)
60
+ return items
61
+ }
62
+
48
63
  export default function AccountOrders({
49
64
  orders,
65
+ pageSize = DEFAULT_PAGE_SIZE,
50
66
  }: {
51
67
  orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
68
+ pageSize?: number
52
69
  }) {
53
70
  const [query, setQuery] = useState("")
71
+ const [page, setPage] = useState(1)
54
72
  const [reorderingId, setReorderingId] = useState<string | null>(null)
55
73
  const router = useRouter()
56
74
  const pathname = usePathname() || ""
@@ -74,6 +92,22 @@ export default function AccountOrders({
74
92
  })
75
93
  }, [safeOrders, query])
76
94
 
95
+ const perPage = Math.max(1, pageSize)
96
+ const totalPages = Math.max(1, Math.ceil(filtered.length / perPage))
97
+ const currentPage = Math.min(page, totalPages)
98
+ const paged = useMemo(
99
+ () => filtered.slice((currentPage - 1) * perPage, currentPage * perPage),
100
+ [filtered, currentPage, perPage]
101
+ )
102
+
103
+ useEffect(() => {
104
+ setPage(1)
105
+ }, [query, perPage])
106
+
107
+ useEffect(() => {
108
+ if (page > totalPages) setPage(totalPages)
109
+ }, [page, totalPages])
110
+
77
111
  async function handleReorder(event: MouseEvent, order: HttpTypes.StoreOrder) {
78
112
  event.preventDefault()
79
113
  event.stopPropagation()
@@ -81,6 +115,7 @@ export default function AccountOrders({
81
115
  setReorderingId(order.id)
82
116
  try {
83
117
  const reorderItems = presentOrderLines(order).reorderItems
118
+ await clearCartLineItems()
84
119
  for (const item of reorderItems) {
85
120
  if (!item.variant_id) continue
86
121
  await addToCart({
@@ -89,7 +124,8 @@ export default function AccountOrders({
89
124
  countryCode,
90
125
  })
91
126
  }
92
- router.push(`/${countryCode}/cart`)
127
+ await applyReorderCheckoutAddress(order)
128
+ router.push(`/${countryCode}/checkout`)
93
129
  } catch {
94
130
  setReorderingId(null)
95
131
  }
@@ -127,7 +163,7 @@ export default function AccountOrders({
127
163
  </div>
128
164
  ) : (
129
165
  <ul className="space-y-4">
130
- {filtered.map((order) => {
166
+ {paged.map((order) => {
131
167
  const resolved = historyStatus(order)
132
168
  const showReorder = canReorder(order)
133
169
  const presentation = presentOrderLines(order)
@@ -177,6 +213,48 @@ export default function AccountOrders({
177
213
  })}
178
214
  </ul>
179
215
  )}
216
+
217
+ {filtered.length > perPage ? (
218
+ <nav className="account-orders__pager" aria-label="Order pagination">
219
+ <button
220
+ type="button"
221
+ className="account-orders__page"
222
+ disabled={currentPage <= 1}
223
+ onClick={() => setPage((p) => Math.max(1, p - 1))}
224
+ >
225
+ Prev
226
+ </button>
227
+ {paginationItems(currentPage, totalPages).map((item, index) =>
228
+ item === "ellipsis" ? (
229
+ <span key={`ellipsis-${index}`} className="account-orders__ellipsis">
230
+ …
231
+ </span>
232
+ ) : (
233
+ <button
234
+ key={item}
235
+ type="button"
236
+ className={
237
+ item === currentPage
238
+ ? "account-orders__page is-active"
239
+ : "account-orders__page"
240
+ }
241
+ aria-current={item === currentPage ? "page" : undefined}
242
+ onClick={() => setPage(item)}
243
+ >
244
+ {item}
245
+ </button>
246
+ )
247
+ )}
248
+ <button
249
+ type="button"
250
+ className="account-orders__page"
251
+ disabled={currentPage >= totalPages}
252
+ onClick={() => setPage((p) => Math.min(totalPages, p + 1))}
253
+ >
254
+ Next
255
+ </button>
256
+ </nav>
257
+ ) : null}
180
258
  </div>
181
259
  )
182
260
  }
@@ -33,21 +33,21 @@ export default function CommerceAuthOtpModal({
33
33
  }
34
34
 
35
35
  return (
36
- <div className="fixed inset-0 z-50 flex items-center justify-center p-4">
36
+ <div className="sr-otp-modal fixed inset-0 z-50 flex items-center justify-center p-4">
37
37
  <button
38
38
  type="button"
39
- className="absolute inset-0 bg-black/40"
39
+ className="sr-otp-modal__backdrop absolute inset-0"
40
40
  aria-label="Close verification dialog"
41
41
  onClick={handleBackdropClick}
42
42
  />
43
- <div className="relative w-full max-w-md rounded-2xl bg-page-bg shadow-xl p-6">
43
+ <div className="sr-otp-modal__panel relative w-full max-w-md p-6">
44
44
  <div className="flex flex-col gap-6 py-4 px-2">
45
45
  <div className="text-center">
46
- <h2 className="text-2xl font-bold text-heading mb-2">
46
+ <h2 className="sr-otp-modal__title text-2xl font-bold mb-2">
47
47
  {success ? successTitle : title}
48
48
  </h2>
49
49
  {!success && (
50
- <p className="text-sm text-gray-500 leading-relaxed max-w-[320px] mx-auto">
50
+ <p className="sr-otp-modal__copy text-sm leading-relaxed max-w-[320px] mx-auto">
51
51
  {description}
52
52
  </p>
53
53
  )}
@@ -55,16 +55,23 @@ export default function CommerceAuthOtpModal({
55
55
 
56
56
  {success ? (
57
57
  <div className="text-center py-6">
58
- <p className="text-brand-accent font-bold animate-pulse">{successMessage}</p>
58
+ <p className="sr-otp-modal__success font-bold">{successMessage}</p>
59
59
  </div>
60
60
  ) : (
61
61
  <>
62
62
  {extraContent}
63
63
 
64
- <OtpInput value={otp} onChange={onOtpChange} autoFocus />
64
+ <OtpInput
65
+ value={otp}
66
+ onChange={onOtpChange}
67
+ autoFocus
68
+ activeClassName="sr-otp-modal__cell is-active"
69
+ filledClassName="sr-otp-modal__cell is-filled"
70
+ emptyClassName="sr-otp-modal__cell"
71
+ />
65
72
 
66
73
  {error && (
67
- <p className="text-red-500 text-xs text-center" role="alert">
74
+ <p className="sr-otp-modal__error text-xs text-center" role="alert">
68
75
  {error}
69
76
  </p>
70
77
  )}
@@ -74,7 +81,7 @@ export default function CommerceAuthOtpModal({
74
81
  type="button"
75
82
  onClick={onVerify}
76
83
  disabled={pending || otp.length < 6}
77
- className="w-full py-3 bg-brand-accent text-inverse font-bold hover:opacity-90 rounded-[30px] transition-colors disabled:opacity-60"
84
+ className="sr-otp-modal__verify w-full"
78
85
  >
79
86
  {pending ? "Verifying…" : verifyLabel}
80
87
  </button>
@@ -83,14 +90,14 @@ export default function CommerceAuthOtpModal({
83
90
  type="button"
84
91
  onClick={onResend}
85
92
  disabled={pending}
86
- className="text-xs text-gray-500 hover:text-brand-accent w-full text-center underline font-medium disabled:opacity-60"
93
+ className="sr-otp-modal__resend w-full text-center disabled:opacity-60"
87
94
  >
88
95
  Resend code
89
96
  </button>
90
97
  </div>
91
98
 
92
99
  {preventClose && (
93
- <p className="text-xs text-muted text-center">
100
+ <p className="sr-otp-modal__hint text-xs text-center">
94
101
  Verification is required to complete{" "}
95
102
  {verifyLabel.toLowerCase().includes("sign in") ? "sign in" : "registration"}.
96
103
  </p>
@@ -5,7 +5,6 @@ import { useEffect, useRef, useState } from "react"
5
5
  import { sendOTP, verifyOTP } from "./auth-server"
6
6
  import OtpVerificationModal from "./otp-verification-modal"
7
7
  import type { OtpVerificationComponent } from "./otp-component"
8
- import { auth } from "./account-theme"
9
8
 
10
9
  type GuestOrderModalProps = {
11
10
  open: boolean
@@ -152,14 +151,14 @@ export default function GuestOrderModal({
152
151
  }
153
152
 
154
153
  return (
155
- <div className="fixed inset-0 z-50 flex items-center justify-center p-4">
154
+ <div className="sr-otp-modal fixed inset-0 z-50 flex items-center justify-center p-4">
156
155
  <button
157
156
  type="button"
158
- className="absolute inset-0 bg-black/40"
157
+ className="sr-otp-modal__backdrop absolute inset-0"
159
158
  aria-label="Close guest order dialog"
160
159
  onClick={handleClose}
161
160
  />
162
- <div className="relative w-full max-w-md border border-cart-border/60 bg-surface shadow-[0_10px_40px_-16px_rgba(0,0,0,0.2)] p-7 sm:p-8">
161
+ <div className="sr-otp-modal__panel relative w-full max-w-md p-7 sm:p-8">
163
162
  <form
164
163
  onSubmit={(e) => {
165
164
  e.preventDefault()
@@ -168,16 +167,15 @@ export default function GuestOrderModal({
168
167
  className="space-y-5"
169
168
  >
170
169
  <div className="text-center">
171
- <Ornament />
172
- <h2 className={`${auth.title} text-xl xl:text-2xl`}>Track your order</h2>
173
- <p className="mt-2 text-sm text-muted leading-relaxed">
170
+ <h2 className="sr-otp-modal__title text-xl xl:text-2xl">Track your order</h2>
171
+ <p className="sr-otp-modal__copy mt-2 text-sm leading-relaxed">
174
172
  Enter the email used at checkout. We&apos;ll send a one-time code to view guest
175
173
  orders.
176
174
  </p>
177
175
  </div>
178
176
 
179
177
  <label className="block">
180
- <span className={`${auth.fieldLabel} mb-1.5 block`}>Email</span>
178
+ <span className="sr-otp-modal__label mb-1.5 block">Email</span>
181
179
  <input
182
180
  type="email"
183
181
  value={email}
@@ -185,12 +183,12 @@ export default function GuestOrderModal({
185
183
  required
186
184
  autoComplete="email"
187
185
  placeholder="you@example.com"
188
- className={auth.input}
186
+ className="sr-otp-modal__input"
189
187
  />
190
188
  </label>
191
189
 
192
190
  {error && (
193
- <p className="text-sm text-brand-sale" role="alert">
191
+ <p className="sr-otp-modal__error text-sm" role="alert">
194
192
  {error}
195
193
  </p>
196
194
  )}
@@ -198,15 +196,15 @@ export default function GuestOrderModal({
198
196
  <button
199
197
  type="submit"
200
198
  disabled={pending || !email.trim()}
201
- className={`${auth.btnPrimary} ${auth.btnShine}`}
199
+ className="sr-otp-modal__verify w-full"
202
200
  >
203
- <span className="relative z-[1]">{pending ? "Sending…" : "Send verification code"}</span>
201
+ {pending ? "Sending…" : "Send verification code"}
204
202
  </button>
205
203
 
206
204
  <button
207
205
  type="button"
208
206
  onClick={handleClose}
209
- className="text-sm text-muted hover:text-brand-accent w-full text-center transition-colors"
207
+ className="sr-otp-modal__resend w-full text-center"
210
208
  >
211
209
  Cancel
212
210
  </button>
@@ -215,13 +213,3 @@ export default function GuestOrderModal({
215
213
  </div>
216
214
  )
217
215
  }
218
-
219
- function Ornament() {
220
- return (
221
- <div className="flex items-center justify-center gap-2 mb-4" aria-hidden>
222
- <span className="h-px w-8 bg-gradient-to-r from-transparent to-brand-accent" />
223
- <span className="text-brand-accent text-[8px] leading-none">◆</span>
224
- <span className="h-px w-8 bg-gradient-to-l from-transparent to-brand-accent" />
225
- </div>
226
- )
227
- }
@@ -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
+ }