@pradip1995/segment-login-template 0.5.10 → 0.5.11

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.10",
3
+ "version": "0.5.11",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,16 +1,29 @@
1
1
  "use client"
2
2
 
3
- import { useMemo, useState } from "react"
3
+ import { useMemo, useState, type MouseEvent } from "react"
4
+ import { usePathname, useRouter } from "next/navigation"
4
5
  import type { HttpTypes } from "@medusajs/types"
6
+ import { addToCart } from "@pradip1995/commerce-core/client/actions/cart"
5
7
  import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
6
8
  import { formatPrice } from "@pradip1995/segment-primitives/format-price"
7
9
  import { acct } from "./account-theme"
8
10
 
9
- function statusStyle(status?: string, fulfillment?: string) {
10
- const s = (fulfillment || status || "").toLowerCase()
11
- if (s.includes("cancel")) {
11
+ function isCancelled(order: HttpTypes.StoreOrder) {
12
+ const status = (order.status || "").toLowerCase()
13
+ return status === "canceled" || status === "cancelled"
14
+ }
15
+
16
+ function canReorder(order: HttpTypes.StoreOrder) {
17
+ if (isCancelled(order)) return false
18
+ const fulfillment = (order.fulfillment_status || "").toLowerCase()
19
+ return ["delivered", "partially_delivered"].includes(fulfillment)
20
+ }
21
+
22
+ function statusStyle(order: HttpTypes.StoreOrder) {
23
+ if (isCancelled(order)) {
12
24
  return "bg-[#FFF1F2] text-[#BE123C] border-[#BE123C]/20"
13
25
  }
26
+ const s = (order.fulfillment_status || order.status || "").toLowerCase()
14
27
  if (s === "delivered" || s === "complete" || s === "completed") {
15
28
  return "bg-[#E6F4F0] text-[#008A5D] border-[#008A5D]/20"
16
29
  }
@@ -23,16 +36,27 @@ function statusStyle(status?: string, fulfillment?: string) {
23
36
  return "bg-surface-muted text-muted border-cart-border"
24
37
  }
25
38
 
39
+ function statusLabel(order: HttpTypes.StoreOrder) {
40
+ if (isCancelled(order)) return "Cancelled"
41
+ return (order.fulfillment_status || order.status || "").replace(/_/g, " ")
42
+ }
43
+
26
44
  export default function AccountOrders({
27
45
  orders,
28
46
  }: {
29
47
  orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
30
48
  }) {
31
49
  const [query, setQuery] = useState("")
50
+ const [reorderingId, setReorderingId] = useState<string | null>(null)
51
+ const router = useRouter()
52
+ const pathname = usePathname() || ""
53
+ const countryCode = pathname.split("/").filter(Boolean)[0] || "in"
32
54
 
33
55
  const safeOrders: HttpTypes.StoreOrder[] = useMemo(() => {
34
56
  if (Array.isArray(orders)) return orders
35
- if (Array.isArray((orders as any)?.orders)) return (orders as any).orders
57
+ if (Array.isArray((orders as { orders?: HttpTypes.StoreOrder[] })?.orders)) {
58
+ return (orders as { orders: HttpTypes.StoreOrder[] }).orders
59
+ }
36
60
  return []
37
61
  }, [orders])
38
62
 
@@ -41,16 +65,31 @@ export default function AccountOrders({
41
65
  if (!q) return safeOrders
42
66
  return safeOrders.filter((order) => {
43
67
  const id = String(order.display_id ?? order.id)
44
- const status = String(order.status ?? "").replace(/_/g, " ")
45
- const fulfillment = String(order.fulfillment_status ?? "").replace(/_/g, " ")
46
- return (
47
- id.includes(q) ||
48
- status.toLowerCase().includes(q) ||
49
- fulfillment.toLowerCase().includes(q)
50
- )
68
+ const status = statusLabel(order)
69
+ return id.includes(q) || status.toLowerCase().includes(q)
51
70
  })
52
71
  }, [safeOrders, query])
53
72
 
73
+ async function handleReorder(event: MouseEvent, order: HttpTypes.StoreOrder) {
74
+ event.preventDefault()
75
+ event.stopPropagation()
76
+ if (!canReorder(order) || reorderingId) return
77
+ setReorderingId(order.id)
78
+ try {
79
+ for (const item of order.items || []) {
80
+ if (!item.variant_id) continue
81
+ await addToCart({
82
+ variantId: item.variant_id,
83
+ quantity: item.quantity || 1,
84
+ countryCode,
85
+ })
86
+ }
87
+ router.push(`/${countryCode}/cart`)
88
+ } catch {
89
+ setReorderingId(null)
90
+ }
91
+ }
92
+
54
93
  return (
55
94
  <div className="space-y-6">
56
95
  <div className="pb-6 border-b border-cart-border">
@@ -84,28 +123,22 @@ export default function AccountOrders({
84
123
  ) : (
85
124
  <ul className="space-y-4">
86
125
  {filtered.map((order) => {
87
- const label = (
88
- order.fulfillment_status ||
89
- order.status ||
90
- ""
91
- ).replace(/_/g, " ")
126
+ const label = statusLabel(order)
127
+ const showReorder = canReorder(order)
92
128
  return (
93
129
  <li key={order.id}>
94
- <LocalizedLink
95
- href={`/orders/${order.id}`}
96
- className={`${acct.card} p-5 flex flex-wrap items-center justify-between gap-4 hover:border-brand-accent transition-colors block`}
130
+ <div
131
+ className={`${acct.card} p-5 flex flex-wrap items-center justify-between gap-4`}
97
132
  >
98
- <div className="space-y-2 min-w-0">
133
+ <LocalizedLink
134
+ href={`/orders/${order.id}`}
135
+ className="space-y-2 min-w-0 flex-1 hover:opacity-90 transition-opacity"
136
+ >
99
137
  <div className="flex flex-wrap items-center gap-2">
100
138
  <p className={`font-bold ${acct.heading}`}>
101
139
  Order #{order.display_id}
102
140
  </p>
103
- <span
104
- className={`${acct.badge} capitalize ${statusStyle(
105
- order.status,
106
- order.fulfillment_status
107
- )}`}
108
- >
141
+ <span className={`${acct.badge} capitalize ${statusStyle(order)}`}>
109
142
  {label}
110
143
  </span>
111
144
  </div>
@@ -118,11 +151,21 @@ export default function AccountOrders({
118
151
  {order.items?.length ?? 0} item
119
152
  {(order.items?.length ?? 0) === 1 ? "" : "s"}
120
153
  </p>
121
- </div>
122
- <p className={`text-base font-bold ${acct.accent} shrink-0`}>
123
- {formatPrice(order.total, order.currency_code)}
124
- </p>
125
- </LocalizedLink>
154
+ <p className={`text-base font-bold ${acct.accent}`}>
155
+ {formatPrice(order.total, order.currency_code)}
156
+ </p>
157
+ </LocalizedLink>
158
+ {showReorder ? (
159
+ <button
160
+ type="button"
161
+ className={acct.btnPrimary}
162
+ disabled={reorderingId === order.id}
163
+ onClick={(event) => handleReorder(event, order)}
164
+ >
165
+ {reorderingId === order.id ? "Adding…" : "Reorder"}
166
+ </button>
167
+ ) : null}
168
+ </div>
126
169
  </li>
127
170
  )
128
171
  })}