@pradip1995/segment-jewelry-account-pages 0.1.2 → 0.1.4

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-jewelry-account-pages",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -27,9 +27,9 @@
27
27
  "@medusajs/types": "^2.8.0",
28
28
  "@pradip1995/commerce-auth": "^4.0.0",
29
29
  "@pradip1995/commerce-core": "^4.0.0",
30
- "@pradip1995/segment-login-template": "^0.5.7",
31
- "@pradip1995/segment-primitives": "^0.4.3",
32
- "@pradip1995/segment-tokens": "^0.3.7"
30
+ "@pradip1995/segment-login-template": "^0.5.14",
31
+ "@pradip1995/segment-primitives": "^0.4.11",
32
+ "@pradip1995/segment-tokens": "^0.3.14"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@pradip1995/plugin-sdk": "^0.2.0",
@@ -41,6 +41,6 @@
41
41
  "scripts": {
42
42
  "typecheck": "tsc --noEmit",
43
43
  "lint": "tsc --noEmit",
44
- "test": "node --experimental-strip-types --test src/default-address.test.ts"
44
+ "test": "node --experimental-strip-types --test src/*.test.ts"
45
45
  }
46
46
  }
@@ -4,59 +4,63 @@ import Image from "next/image"
4
4
  import type { HttpTypes } from "@medusajs/types"
5
5
  import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
6
6
  import { formatPrice } from "@pradip1995/segment-primitives/format-price"
7
+ import {
8
+ resolveOrderHistoryStatus,
9
+ type OrderHistoryStatusInput,
10
+ type OrderHistoryStatusTone,
11
+ } from "./order-history-status"
12
+
13
+ const STATUS_ICON_CLASS: Record<OrderHistoryStatusTone, string> = {
14
+ cancel: "modern-order-row__status-icon--cancel",
15
+ shipped: "modern-order-row__status-icon--shipped",
16
+ done: "modern-order-row__status-icon--done",
17
+ return: "modern-order-row__status-icon--return",
18
+ processing: "modern-order-row__status-icon--done",
19
+ }
7
20
 
8
- function getStatusDetails(status?: string, fulfillment?: string) {
9
- const s = (fulfillment || status || "").toLowerCase()
10
- if (s.includes("cancel")) {
11
- return {
12
- label: "Cancelled",
13
- iconClass: "modern-order-row__status-icon--cancel",
14
- Icon: function CancelIcon() {
15
- return (
16
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" width="14" height="14">
17
- <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
18
- </svg>
19
- )
20
- },
21
- }
21
+ function StatusIcon({ tone }: { tone: OrderHistoryStatusTone }) {
22
+ if (tone === "cancel") {
23
+ return (
24
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" width="14" height="14">
25
+ <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
26
+ </svg>
27
+ )
22
28
  }
23
- if (s.includes("ship")) {
24
- return {
25
- label: "Shipped",
26
- iconClass: "modern-order-row__status-icon--shipped",
27
- Icon: function ShippedIcon() {
28
- return (
29
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" width="16" height="16">
30
- <path strokeLinecap="round" strokeLinejoin="round" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" />
31
- </svg>
32
- )
33
- },
34
- }
29
+ if (tone === "shipped") {
30
+ return (
31
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" width="16" height="16">
32
+ <path strokeLinecap="round" strokeLinejoin="round" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" />
33
+ </svg>
34
+ )
35
35
  }
36
- if (s === "delivered" || s === "complete" || s === "completed" || s.includes("fulfill")) {
37
- return {
38
- label: s === "delivered" ? "Delivered" : "Order Confirmed",
39
- iconClass: "modern-order-row__status-icon--done",
40
- Icon: function CheckIcon() {
41
- return (
42
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" width="15" height="15">
43
- <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
44
- </svg>
45
- )
46
- },
47
- }
36
+ if (tone === "return") {
37
+ return (
38
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="15" height="15">
39
+ <path strokeLinecap="round" strokeLinejoin="round" d="M9 15L3 9m0 0l6-6M3 9h12a6 6 0 010 12h-3" />
40
+ </svg>
41
+ )
42
+ }
43
+ if (tone === "processing") {
44
+ return (
45
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="15" height="15">
46
+ <circle cx="12" cy="12" r="9" />
47
+ <path strokeLinecap="round" strokeLinejoin="round" d="M12 7v5l3 3" />
48
+ </svg>
49
+ )
48
50
  }
51
+ return (
52
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" width="15" height="15">
53
+ <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
54
+ </svg>
55
+ )
56
+ }
57
+
58
+ function getStatusDetails(order: HttpTypes.StoreOrder) {
59
+ const resolved = resolveOrderHistoryStatus(order as OrderHistoryStatusInput)
49
60
  return {
50
- label: "Processing",
51
- iconClass: "modern-order-row__status-icon--done",
52
- Icon: function ProcessingIcon() {
53
- return (
54
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="15" height="15">
55
- <circle cx="12" cy="12" r="9" />
56
- <path strokeLinecap="round" strokeLinejoin="round" d="M12 7v5l3 3" />
57
- </svg>
58
- )
59
- },
61
+ label: resolved.label,
62
+ iconClass: STATUS_ICON_CLASS[resolved.tone],
63
+ tone: resolved.tone,
60
64
  }
61
65
  }
62
66
 
@@ -86,7 +90,7 @@ export default function ModernAccountOrderCard({
86
90
  compact?: boolean
87
91
  }) {
88
92
  const currency = order.currency_code
89
- const statusInfo = getStatusDetails(order.status, order.fulfillment_status)
93
+ const statusInfo = getStatusDetails(order)
90
94
  const items = order.items || []
91
95
 
92
96
  const dateStr = order.created_at
@@ -124,7 +128,7 @@ export default function ModernAccountOrderCard({
124
128
  <p className="modern-order-row__label">Status</p>
125
129
  <div className="modern-order-row__status">
126
130
  <div className={`modern-order-row__status-icon ${statusInfo.iconClass}`}>
127
- <statusInfo.Icon />
131
+ <StatusIcon tone={statusInfo.tone} />
128
132
  </div>
129
133
  <span className="modern-order-row__status-text">{statusInfo.label}</span>
130
134
  </div>
@@ -155,7 +159,7 @@ export default function ModernAccountOrderCard({
155
159
 
156
160
  <div className="modern-order-row__status">
157
161
  <div className={`modern-order-row__status-icon ${statusInfo.iconClass}`}>
158
- <statusInfo.Icon />
162
+ <StatusIcon tone={statusInfo.tone} />
159
163
  </div>
160
164
  <span className="modern-order-row__status-text">{statusInfo.label}</span>
161
165
  </div>
@@ -4,6 +4,10 @@ import { useMemo, useState } from "react"
4
4
  import type { HttpTypes } from "@medusajs/types"
5
5
  import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
6
6
  import ModernAccountOrderCard from "./modern-account-order-card"
7
+ import {
8
+ resolveOrderHistoryStatus,
9
+ type OrderHistoryStatusInput,
10
+ } from "./order-history-status"
7
11
 
8
12
  export default function ModernAccountOrders({
9
13
  orders,
@@ -25,7 +29,9 @@ export default function ModernAccountOrders({
25
29
  if (!q) return safeOrders
26
30
  return safeOrders.filter((order) => {
27
31
  const id = String(order.display_id ?? order.id)
28
- const status = String(order.status ?? "").replace(/_/g, " ")
32
+ const status = resolveOrderHistoryStatus(
33
+ order as OrderHistoryStatusInput
34
+ ).label
29
35
  const fulfillment = String(order.fulfillment_status ?? "").replace(/_/g, " ")
30
36
  return (
31
37
  id.includes(q) ||
@@ -504,6 +504,11 @@
504
504
  color: #b91c1c;
505
505
  }
506
506
 
507
+ .modern-order-row__status-icon--return {
508
+ background: #f5ede6;
509
+ color: #6b432a;
510
+ }
511
+
507
512
  .modern-order-row__status-text {
508
513
  font-size: 0.875rem;
509
514
  font-weight: 700;
@@ -0,0 +1,156 @@
1
+ import assert from "node:assert/strict"
2
+ import { describe, it } from "node:test"
3
+ import {
4
+ resolveOrderHistoryStatus,
5
+ type OrderHistoryStatus,
6
+ type OrderHistoryStatusInput,
7
+ } from "./order-history-status.ts"
8
+
9
+ const CASES: Array<{
10
+ name: string
11
+ order: OrderHistoryStatusInput
12
+ want: OrderHistoryStatus
13
+ }> = [
14
+ {
15
+ name: "stays delivered when there is no return",
16
+ order: { status: "completed", fulfillment_status: "delivered" },
17
+ want: { label: "Delivered", tone: "done" },
18
+ },
19
+ {
20
+ name: "shows return requested after a return is submitted on a delivered order",
21
+ order: {
22
+ status: "completed",
23
+ fulfillment_status: "delivered",
24
+ returns: [{ status: "requested" }],
25
+ },
26
+ want: { label: "Return Requested", tone: "return" },
27
+ },
28
+ {
29
+ name: "treats a return with no status as requested",
30
+ order: {
31
+ fulfillment_status: "delivered",
32
+ returns: [{ status: null }],
33
+ },
34
+ want: { label: "Return Requested", tone: "return" },
35
+ },
36
+ {
37
+ name: "maps open returns to return approved",
38
+ order: {
39
+ fulfillment_status: "delivered",
40
+ returns: [{ status: "open" }],
41
+ },
42
+ want: { label: "Return Approved", tone: "return" },
43
+ },
44
+ {
45
+ name: "maps approved returns to return approved",
46
+ order: {
47
+ fulfillment_status: "delivered",
48
+ returns: [{ status: "approved" }],
49
+ },
50
+ want: { label: "Return Approved", tone: "return" },
51
+ },
52
+ {
53
+ name: "maps partially received returns to return in progress",
54
+ order: {
55
+ fulfillment_status: "delivered",
56
+ returns: [{ status: "partially_received" }],
57
+ },
58
+ want: { label: "Return In Progress", tone: "return" },
59
+ },
60
+ {
61
+ name: "maps received returns to returned",
62
+ order: {
63
+ status: "completed",
64
+ fulfillment_status: "delivered",
65
+ returns: [{ status: "received" }],
66
+ },
67
+ want: { label: "Returned", tone: "return" },
68
+ },
69
+ {
70
+ name: "maps fulfillment returned when returns are missing from the payload",
71
+ order: { fulfillment_status: "returned" },
72
+ want: { label: "Returned", tone: "return" },
73
+ },
74
+ {
75
+ name: "maps fulfillment partially returned to return in progress",
76
+ order: { fulfillment_status: "partially_returned" },
77
+ want: { label: "Return In Progress", tone: "return" },
78
+ },
79
+ {
80
+ name: "shows refunded when payment is refunded after a return",
81
+ order: {
82
+ fulfillment_status: "returned",
83
+ payment_status: "refunded",
84
+ returns: [{ status: "received" }],
85
+ },
86
+ want: { label: "Refunded", tone: "return" },
87
+ },
88
+ {
89
+ name: "shows refunded for a partial refund",
90
+ order: {
91
+ fulfillment_status: "delivered",
92
+ payment_status: "partially_refunded",
93
+ returns: [{ status: "requested" }],
94
+ },
95
+ want: { label: "Refunded", tone: "return" },
96
+ },
97
+ {
98
+ name: "ignores canceled returns and keeps delivered",
99
+ order: {
100
+ fulfillment_status: "delivered",
101
+ returns: [{ status: "canceled" }, { status: "cancelled" }],
102
+ },
103
+ want: { label: "Delivered", tone: "done" },
104
+ },
105
+ {
106
+ name: "ignores exchange and claim returns",
107
+ order: {
108
+ fulfillment_status: "delivered",
109
+ returns: [
110
+ { status: "requested", exchange_id: "exch_1" },
111
+ { status: "requested", claim_id: "claim_1" },
112
+ ],
113
+ },
114
+ want: { label: "Delivered", tone: "done" },
115
+ },
116
+ {
117
+ name: "uses the furthest return lifecycle when several returns exist",
118
+ order: {
119
+ fulfillment_status: "delivered",
120
+ returns: [{ status: "requested" }, { status: "received" }],
121
+ },
122
+ want: { label: "Returned", tone: "return" },
123
+ },
124
+ {
125
+ name: "cancelled order wins over a return",
126
+ order: {
127
+ status: "canceled",
128
+ fulfillment_status: "delivered",
129
+ returns: [{ status: "requested" }],
130
+ },
131
+ want: { label: "Cancelled", tone: "cancel" },
132
+ },
133
+ {
134
+ name: "shows shipped when not delivered and no return",
135
+ order: { fulfillment_status: "shipped" },
136
+ want: { label: "Shipped", tone: "shipped" },
137
+ },
138
+ {
139
+ name: "shows order confirmed for fulfilled orders",
140
+ order: { fulfillment_status: "fulfilled" },
141
+ want: { label: "Order Confirmed", tone: "done" },
142
+ },
143
+ {
144
+ name: "shows processing as the default",
145
+ order: { status: "pending" },
146
+ want: { label: "Processing", tone: "processing" },
147
+ },
148
+ ]
149
+
150
+ describe("resolveOrderHistoryStatus", () => {
151
+ for (const row of CASES) {
152
+ it(row.name, () => {
153
+ assert.deepEqual(resolveOrderHistoryStatus(row.order), row.want)
154
+ })
155
+ }
156
+ })
@@ -0,0 +1,150 @@
1
+ export type OrderHistoryStatusTone =
2
+ | "cancel"
3
+ | "shipped"
4
+ | "done"
5
+ | "return"
6
+ | "processing"
7
+
8
+ export type OrderHistoryStatus = {
9
+ label: string
10
+ tone: OrderHistoryStatusTone
11
+ }
12
+
13
+ export type OrderHistoryReturnLike = {
14
+ status?: string | null
15
+ exchange_id?: string | null
16
+ claim_id?: string | null
17
+ }
18
+
19
+ export type OrderHistoryStatusInput = {
20
+ status?: string | null
21
+ fulfillment_status?: string | null
22
+ payment_status?: string | null
23
+ returns?: OrderHistoryReturnLike[] | null
24
+ }
25
+
26
+ type ReturnLifecycle =
27
+ | "requested"
28
+ | "approved"
29
+ | "in_progress"
30
+ | "returned"
31
+ | "refunded"
32
+
33
+ const CLOSED_RETURN = new Set(["canceled", "cancelled", "rejected"])
34
+
35
+ const RETURN_RANK: Record<ReturnLifecycle, number> = {
36
+ requested: 1,
37
+ approved: 2,
38
+ in_progress: 3,
39
+ returned: 4,
40
+ refunded: 5,
41
+ }
42
+
43
+ const RETURN_LABEL: Record<ReturnLifecycle, string> = {
44
+ requested: "Return Requested",
45
+ approved: "Return Approved",
46
+ in_progress: "Return In Progress",
47
+ returned: "Returned",
48
+ refunded: "Refunded",
49
+ }
50
+
51
+ function normalize(value?: string | null) {
52
+ return (value || "").toLowerCase().trim()
53
+ }
54
+
55
+ function isStandaloneReturn(ret: OrderHistoryReturnLike) {
56
+ return !ret.exchange_id && !ret.claim_id
57
+ }
58
+
59
+ function lifecycleFromReturnStatus(status: string): ReturnLifecycle | null {
60
+ switch (status) {
61
+ case "requested":
62
+ return "requested"
63
+ case "open":
64
+ case "approved":
65
+ return "approved"
66
+ case "partially_received":
67
+ case "in_progress":
68
+ return "in_progress"
69
+ case "received":
70
+ case "returned":
71
+ case "complete":
72
+ case "completed":
73
+ return "returned"
74
+ default:
75
+ return null
76
+ }
77
+ }
78
+
79
+ function pickHigher(
80
+ current: ReturnLifecycle | null,
81
+ next: ReturnLifecycle
82
+ ): ReturnLifecycle {
83
+ if (!current) return next
84
+ return RETURN_RANK[next] > RETURN_RANK[current] ? next : current
85
+ }
86
+
87
+ function returnLifecycle(order: OrderHistoryStatusInput): ReturnLifecycle | null {
88
+ const payment = normalize(order.payment_status)
89
+ let best: ReturnLifecycle | null = null
90
+
91
+ if (payment === "refunded" || payment === "partially_refunded") {
92
+ best = "refunded"
93
+ }
94
+
95
+ for (const ret of order.returns || []) {
96
+ if (!isStandaloneReturn(ret)) continue
97
+ const status = normalize(ret.status)
98
+ if (CLOSED_RETURN.has(status)) continue
99
+ const life = status
100
+ ? lifecycleFromReturnStatus(status) ?? "requested"
101
+ : "requested"
102
+ best = pickHigher(best, life)
103
+ }
104
+
105
+ const fulfillment = normalize(order.fulfillment_status)
106
+ if (fulfillment === "returned") {
107
+ best = pickHigher(best, "returned")
108
+ } else if (fulfillment === "partially_returned") {
109
+ best = pickHigher(best, "in_progress")
110
+ }
111
+
112
+ return best
113
+ }
114
+
115
+ export function resolveOrderHistoryStatus(
116
+ order: OrderHistoryStatusInput
117
+ ): OrderHistoryStatus {
118
+ const orderStatus = normalize(order.status)
119
+ if (orderStatus === "canceled" || orderStatus === "cancelled") {
120
+ return { label: "Cancelled", tone: "cancel" }
121
+ }
122
+
123
+ const life = returnLifecycle(order)
124
+ if (life) {
125
+ return { label: RETURN_LABEL[life], tone: "return" }
126
+ }
127
+
128
+ const fulfillment = normalize(order.fulfillment_status)
129
+ const combined = fulfillment || orderStatus
130
+
131
+ if (combined.includes("cancel")) {
132
+ return { label: "Cancelled", tone: "cancel" }
133
+ }
134
+ if (combined.includes("ship")) {
135
+ return { label: "Shipped", tone: "shipped" }
136
+ }
137
+ if (
138
+ combined === "delivered" ||
139
+ combined === "complete" ||
140
+ combined === "completed" ||
141
+ combined.includes("fulfill")
142
+ ) {
143
+ return {
144
+ label: combined === "delivered" ? "Delivered" : "Order Confirmed",
145
+ tone: "done",
146
+ }
147
+ }
148
+
149
+ return { label: "Processing", tone: "processing" }
150
+ }