@pradip1995/segment-animation-order 0.2.1
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 +44 -0
- package/src/animation-order.css +823 -0
- package/src/confirmation-back-handler.tsx +32 -0
- package/src/index.ts +2 -0
- package/src/manifest.ts +11 -0
- package/src/order-actions-server.ts +52 -0
- package/src/order-actions.tsx +341 -0
- package/src/order-details.tsx +253 -0
- package/src/order-status.tsx +252 -0
- package/src/order-tracking.tsx +412 -0
- package/src/product-details-path.ts +22 -0
- package/src/segment.tsx +79 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import Image from "next/image"
|
|
4
|
+
import { motion } from "framer-motion"
|
|
5
|
+
import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
|
|
6
|
+
import { formatPrice } from "@pradip1995/segment-primitives/format-price"
|
|
7
|
+
import type { HttpTypes } from "@medusajs/types"
|
|
8
|
+
import ConfirmationBackHandler from "./confirmation-back-handler"
|
|
9
|
+
import { productDetailsPathFromOrder } from "./product-details-path"
|
|
10
|
+
|
|
11
|
+
export type AnimationOrderStatusProps = {
|
|
12
|
+
order?: HttpTypes.StoreOrder | null
|
|
13
|
+
view?: "confirmed" | "failed" | string
|
|
14
|
+
isLoggedIn?: boolean
|
|
15
|
+
shopName?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function formatAddress(order: HttpTypes.StoreOrder) {
|
|
19
|
+
const shipping = order.shipping_address
|
|
20
|
+
const fullName = [shipping?.first_name, shipping?.last_name].filter(Boolean).join(" ")
|
|
21
|
+
const address = [
|
|
22
|
+
shipping?.address_1,
|
|
23
|
+
shipping?.address_2,
|
|
24
|
+
shipping?.city,
|
|
25
|
+
shipping?.province,
|
|
26
|
+
shipping?.postal_code,
|
|
27
|
+
]
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
.join(", ")
|
|
30
|
+
return { fullName, address }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function ConfirmedView({
|
|
34
|
+
order,
|
|
35
|
+
isLoggedIn,
|
|
36
|
+
}: {
|
|
37
|
+
order: HttpTypes.StoreOrder
|
|
38
|
+
isLoggedIn: boolean
|
|
39
|
+
}) {
|
|
40
|
+
const currency = order.currency_code || "inr"
|
|
41
|
+
const items = order.items || []
|
|
42
|
+
const { fullName, address } = formatAddress(order)
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<section className="anim-order anim-order--confirmed" data-testid="segment-animation-order">
|
|
46
|
+
<ConfirmationBackHandler href={productDetailsPathFromOrder(order)} />
|
|
47
|
+
<div className="anim-order__atmosphere" aria-hidden />
|
|
48
|
+
|
|
49
|
+
<div className="anim-order__inner">
|
|
50
|
+
<motion.header
|
|
51
|
+
className="anim-order__hero"
|
|
52
|
+
initial={{ opacity: 0, y: 18 }}
|
|
53
|
+
animate={{ opacity: 1, y: 0 }}
|
|
54
|
+
transition={{ duration: 0.55, ease: [0.22, 1, 0.36, 1] }}
|
|
55
|
+
>
|
|
56
|
+
<p className="anim-order__eyebrow">Thank you</p>
|
|
57
|
+
<h1 className="anim-order__title">Order placed successfully</h1>
|
|
58
|
+
<p className="anim-order__subtitle">
|
|
59
|
+
Your order is confirmed. You will receive a confirmation email or SMS
|
|
60
|
+
shortly with the expected delivery date.
|
|
61
|
+
</p>
|
|
62
|
+
{order.display_id != null ? (
|
|
63
|
+
<p className="anim-order__order-id">Order #{order.display_id}</p>
|
|
64
|
+
) : null}
|
|
65
|
+
<span className="anim-order__accent-line" aria-hidden />
|
|
66
|
+
</motion.header>
|
|
67
|
+
|
|
68
|
+
<motion.div
|
|
69
|
+
className="anim-order__panel"
|
|
70
|
+
initial={{ opacity: 0, y: 16 }}
|
|
71
|
+
animate={{ opacity: 1, y: 0 }}
|
|
72
|
+
transition={{ duration: 0.5, delay: 0.12, ease: [0.22, 1, 0.36, 1] }}
|
|
73
|
+
>
|
|
74
|
+
<p className="anim-order__panel-label">Delivering to</p>
|
|
75
|
+
<p className="anim-order__panel-name">{fullName || "Customer"}</p>
|
|
76
|
+
{order.email ? <p className="anim-order__panel-meta">{order.email}</p> : null}
|
|
77
|
+
<p className="anim-order__panel-meta">{address || "Address not available"}</p>
|
|
78
|
+
</motion.div>
|
|
79
|
+
|
|
80
|
+
<motion.div
|
|
81
|
+
className="anim-order__panel"
|
|
82
|
+
initial={{ opacity: 0, y: 16 }}
|
|
83
|
+
animate={{ opacity: 1, y: 0 }}
|
|
84
|
+
transition={{ duration: 0.5, delay: 0.2, ease: [0.22, 1, 0.36, 1] }}
|
|
85
|
+
>
|
|
86
|
+
<p className="anim-order__panel-label">Order items</p>
|
|
87
|
+
<ul className="anim-order__items">
|
|
88
|
+
{items.map((item) => (
|
|
89
|
+
<li key={item.id} className="anim-order__item">
|
|
90
|
+
<div className="anim-order__thumb">
|
|
91
|
+
{item.thumbnail ? (
|
|
92
|
+
<Image
|
|
93
|
+
src={item.thumbnail}
|
|
94
|
+
alt={item.title || "Product"}
|
|
95
|
+
fill
|
|
96
|
+
className="object-cover"
|
|
97
|
+
unoptimized
|
|
98
|
+
/>
|
|
99
|
+
) : null}
|
|
100
|
+
</div>
|
|
101
|
+
<div className="anim-order__item-meta">
|
|
102
|
+
<p className="anim-order__item-title">
|
|
103
|
+
{item.product_title || item.title}
|
|
104
|
+
</p>
|
|
105
|
+
<p className="anim-order__item-qty">Qty: {item.quantity}</p>
|
|
106
|
+
</div>
|
|
107
|
+
<p className="anim-order__item-price">
|
|
108
|
+
{formatPrice(
|
|
109
|
+
item.total ?? (item.unit_price || 0) * (item.quantity || 0),
|
|
110
|
+
currency
|
|
111
|
+
)}
|
|
112
|
+
</p>
|
|
113
|
+
</li>
|
|
114
|
+
))}
|
|
115
|
+
</ul>
|
|
116
|
+
<div className="anim-order__total">
|
|
117
|
+
<span>Total</span>
|
|
118
|
+
<strong>{formatPrice(order.total, currency)}</strong>
|
|
119
|
+
</div>
|
|
120
|
+
</motion.div>
|
|
121
|
+
|
|
122
|
+
<motion.div
|
|
123
|
+
className="anim-order__actions"
|
|
124
|
+
initial={{ opacity: 0, y: 12 }}
|
|
125
|
+
animate={{ opacity: 1, y: 0 }}
|
|
126
|
+
transition={{ duration: 0.45, delay: 0.28 }}
|
|
127
|
+
>
|
|
128
|
+
<LocalizedLink href="/store" className="anim-order__btn anim-order__btn--outline">
|
|
129
|
+
Continue shopping
|
|
130
|
+
</LocalizedLink>
|
|
131
|
+
<LocalizedLink
|
|
132
|
+
href={isLoggedIn ? "/account/orders" : "/account"}
|
|
133
|
+
className="anim-order__btn anim-order__btn--primary"
|
|
134
|
+
>
|
|
135
|
+
Track order
|
|
136
|
+
</LocalizedLink>
|
|
137
|
+
</motion.div>
|
|
138
|
+
</div>
|
|
139
|
+
</section>
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function FailedView({
|
|
144
|
+
order,
|
|
145
|
+
shopName,
|
|
146
|
+
}: {
|
|
147
|
+
order?: HttpTypes.StoreOrder | null
|
|
148
|
+
shopName?: string
|
|
149
|
+
}) {
|
|
150
|
+
const brand = shopName || "Lumière"
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<section className="anim-order anim-order--failed" data-testid="segment-animation-order-failed">
|
|
154
|
+
<div className="anim-order__atmosphere anim-order__atmosphere--failed" aria-hidden />
|
|
155
|
+
|
|
156
|
+
<div className="anim-order__inner">
|
|
157
|
+
<motion.header
|
|
158
|
+
className="anim-order__hero"
|
|
159
|
+
initial={{ opacity: 0, y: 18 }}
|
|
160
|
+
animate={{ opacity: 1, y: 0 }}
|
|
161
|
+
transition={{ duration: 0.55, ease: [0.22, 1, 0.36, 1] }}
|
|
162
|
+
>
|
|
163
|
+
<p className="anim-order__eyebrow anim-order__eyebrow--failed">Payment issue</p>
|
|
164
|
+
<h1 className="anim-order__title">Order could not be completed</h1>
|
|
165
|
+
<p className="anim-order__subtitle">
|
|
166
|
+
Something went wrong while confirming your payment. No charge was completed for this
|
|
167
|
+
attempt — you can try again from checkout, or continue browsing {brand}.
|
|
168
|
+
</p>
|
|
169
|
+
{order?.display_id != null ? (
|
|
170
|
+
<p className="anim-order__order-id">Reference #{order.display_id}</p>
|
|
171
|
+
) : null}
|
|
172
|
+
<span className="anim-order__accent-line anim-order__accent-line--failed" aria-hidden />
|
|
173
|
+
</motion.header>
|
|
174
|
+
|
|
175
|
+
<motion.div
|
|
176
|
+
className="anim-order__panel"
|
|
177
|
+
initial={{ opacity: 0, y: 16 }}
|
|
178
|
+
animate={{ opacity: 1, y: 0 }}
|
|
179
|
+
transition={{ duration: 0.5, delay: 0.12 }}
|
|
180
|
+
>
|
|
181
|
+
<p className="anim-order__panel-label">What you can do</p>
|
|
182
|
+
<ul className="anim-order__tips">
|
|
183
|
+
<li>Return to checkout and try Pay Online or Cash on Delivery again.</li>
|
|
184
|
+
<li>Check your card / UPI balance, then retry with the same cart.</li>
|
|
185
|
+
<li>If the amount was held, it usually releases within a few business days.</li>
|
|
186
|
+
</ul>
|
|
187
|
+
</motion.div>
|
|
188
|
+
|
|
189
|
+
<motion.div
|
|
190
|
+
className="anim-order__actions"
|
|
191
|
+
initial={{ opacity: 0, y: 12 }}
|
|
192
|
+
animate={{ opacity: 1, y: 0 }}
|
|
193
|
+
transition={{ duration: 0.45, delay: 0.22 }}
|
|
194
|
+
>
|
|
195
|
+
<LocalizedLink href="/checkout" className="anim-order__btn anim-order__btn--primary">
|
|
196
|
+
Try checkout again
|
|
197
|
+
</LocalizedLink>
|
|
198
|
+
<LocalizedLink href="/cart" className="anim-order__btn anim-order__btn--outline">
|
|
199
|
+
Back to cart
|
|
200
|
+
</LocalizedLink>
|
|
201
|
+
</motion.div>
|
|
202
|
+
</div>
|
|
203
|
+
</section>
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function EmptyState({ view }: { view: string }) {
|
|
208
|
+
const isFailed = view === "failed"
|
|
209
|
+
return (
|
|
210
|
+
<section
|
|
211
|
+
className={`anim-order ${isFailed ? "anim-order--failed" : "anim-order--confirmed"}`}
|
|
212
|
+
data-testid="segment-animation-order-empty"
|
|
213
|
+
>
|
|
214
|
+
<div className="anim-order__atmosphere" aria-hidden />
|
|
215
|
+
<div className="anim-order__inner">
|
|
216
|
+
<header className="anim-order__hero">
|
|
217
|
+
<p className="anim-order__eyebrow">{isFailed ? "Payment issue" : "Order"}</p>
|
|
218
|
+
<h1 className="anim-order__title">
|
|
219
|
+
{isFailed ? "Order could not be completed" : "Order not found"}
|
|
220
|
+
</h1>
|
|
221
|
+
<p className="anim-order__subtitle">
|
|
222
|
+
{isFailed
|
|
223
|
+
? "We couldn’t load this payment attempt. Return to checkout to try again."
|
|
224
|
+
: "We couldn’t load your order confirmation. Check the link or continue shopping."}
|
|
225
|
+
</p>
|
|
226
|
+
</header>
|
|
227
|
+
<div className="anim-order__actions">
|
|
228
|
+
<LocalizedLink
|
|
229
|
+
href={isFailed ? "/checkout" : "/store"}
|
|
230
|
+
className="anim-order__btn anim-order__btn--primary"
|
|
231
|
+
>
|
|
232
|
+
{isFailed ? "Try checkout again" : "Continue shopping"}
|
|
233
|
+
</LocalizedLink>
|
|
234
|
+
</div>
|
|
235
|
+
</div>
|
|
236
|
+
</section>
|
|
237
|
+
)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export default function AnimationOrderStatus({
|
|
241
|
+
order,
|
|
242
|
+
view = "confirmed",
|
|
243
|
+
isLoggedIn = false,
|
|
244
|
+
shopName,
|
|
245
|
+
}: AnimationOrderStatusProps) {
|
|
246
|
+
if (view === "failed") {
|
|
247
|
+
return <FailedView order={order} shopName={shopName} />
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (!order) return <EmptyState view="confirmed" />
|
|
251
|
+
return <ConfirmedView order={order} isLoggedIn={isLoggedIn} />
|
|
252
|
+
}
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import type { HttpTypes } from "@medusajs/types"
|
|
2
|
+
|
|
3
|
+
type TrackingStep = {
|
|
4
|
+
title: string
|
|
5
|
+
status: string
|
|
6
|
+
date: string | Date | null
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type OrderReturn = {
|
|
10
|
+
id: string
|
|
11
|
+
status?: string | null
|
|
12
|
+
exchange_id?: string | null
|
|
13
|
+
claim_id?: string | null
|
|
14
|
+
created_at?: string | Date | null
|
|
15
|
+
received_at?: string | Date | null
|
|
16
|
+
canceled_at?: string | Date | null
|
|
17
|
+
updated_at?: string | Date | null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type OrderFulfillmentLite = {
|
|
21
|
+
packed_at?: string | Date | null
|
|
22
|
+
shipped_at?: string | Date | null
|
|
23
|
+
delivered_at?: string | Date | null
|
|
24
|
+
canceled_at?: string | Date | null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isStandaloneReturn(ret: OrderReturn) {
|
|
28
|
+
return !ret.exchange_id && !ret.claim_id
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getActiveReturn(order: HttpTypes.StoreOrder): OrderReturn | null {
|
|
32
|
+
const returns =
|
|
33
|
+
(order as HttpTypes.StoreOrder & { returns?: OrderReturn[] }).returns || []
|
|
34
|
+
const standalone = returns.filter(isStandaloneReturn)
|
|
35
|
+
|
|
36
|
+
const active = standalone.find(
|
|
37
|
+
(r: OrderReturn) =>
|
|
38
|
+
r.status &&
|
|
39
|
+
!["canceled", "cancelled", "rejected"].includes(r.status.toLowerCase())
|
|
40
|
+
)
|
|
41
|
+
if (active) return active
|
|
42
|
+
|
|
43
|
+
const canceled = [...standalone]
|
|
44
|
+
.reverse()
|
|
45
|
+
.find((r) =>
|
|
46
|
+
["canceled", "cancelled"].includes((r.status || "").toLowerCase())
|
|
47
|
+
)
|
|
48
|
+
return canceled || null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function formatStepDate(date: string | Date) {
|
|
52
|
+
try {
|
|
53
|
+
const d = new Date(date)
|
|
54
|
+
if (isNaN(d.getTime())) return ""
|
|
55
|
+
return new Intl.DateTimeFormat("en-IN", {
|
|
56
|
+
weekday: "short",
|
|
57
|
+
day: "numeric",
|
|
58
|
+
month: "short",
|
|
59
|
+
}).format(d)
|
|
60
|
+
} catch {
|
|
61
|
+
return ""
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function formatFullDate(date: string | Date) {
|
|
66
|
+
try {
|
|
67
|
+
const d = new Date(date)
|
|
68
|
+
if (isNaN(d.getTime())) return "N/A"
|
|
69
|
+
return new Intl.DateTimeFormat("en-IN", {
|
|
70
|
+
day: "numeric",
|
|
71
|
+
month: "short",
|
|
72
|
+
year: "numeric",
|
|
73
|
+
}).format(d)
|
|
74
|
+
} catch {
|
|
75
|
+
return "N/A"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getFulfillments(order: HttpTypes.StoreOrder): OrderFulfillmentLite[] {
|
|
80
|
+
return (
|
|
81
|
+
(order as HttpTypes.StoreOrder & { fulfillments?: OrderFulfillmentLite[] })
|
|
82
|
+
.fulfillments ?? []
|
|
83
|
+
).filter((fulfillment: OrderFulfillmentLite) => !fulfillment.canceled_at)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function firstDate(
|
|
87
|
+
fulfillments: OrderFulfillmentLite[],
|
|
88
|
+
key: keyof Pick<OrderFulfillmentLite, "packed_at" | "shipped_at" | "delivered_at">
|
|
89
|
+
): string | Date | null {
|
|
90
|
+
for (const fulfillment of fulfillments) {
|
|
91
|
+
if (fulfillment[key]) return fulfillment[key] ?? null
|
|
92
|
+
}
|
|
93
|
+
return null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function getDeliveryStatusIndex(order: HttpTypes.StoreOrder) {
|
|
97
|
+
const status = (order.fulfillment_status || "").toLowerCase().trim()
|
|
98
|
+
const fulfillments = getFulfillments(order)
|
|
99
|
+
const anyDelivered = fulfillments.some((f) => f.delivered_at)
|
|
100
|
+
const anyShipped = fulfillments.some((f) => f.shipped_at)
|
|
101
|
+
const anyPacked = fulfillments.some((f) => f.packed_at)
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
anyDelivered ||
|
|
105
|
+
status === "delivered" ||
|
|
106
|
+
status === "partially_delivered" ||
|
|
107
|
+
status === "partially_returned" ||
|
|
108
|
+
status === "returned"
|
|
109
|
+
) {
|
|
110
|
+
return 3
|
|
111
|
+
}
|
|
112
|
+
if (anyShipped || status === "shipped" || status === "partially_shipped") {
|
|
113
|
+
return 2
|
|
114
|
+
}
|
|
115
|
+
if (anyPacked || status === "fulfilled" || status === "partially_fulfilled") {
|
|
116
|
+
return 1
|
|
117
|
+
}
|
|
118
|
+
return 0
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function getReturnStatusIndex(
|
|
122
|
+
returnStatus?: string | null,
|
|
123
|
+
paymentStatus?: string | null
|
|
124
|
+
) {
|
|
125
|
+
const payment = (paymentStatus || "").toLowerCase()
|
|
126
|
+
if (payment === "refunded" || payment === "partially_refunded") return 3
|
|
127
|
+
switch ((returnStatus || "").toLowerCase()) {
|
|
128
|
+
case "open":
|
|
129
|
+
case "requested":
|
|
130
|
+
return 0
|
|
131
|
+
case "partially_received":
|
|
132
|
+
return 1
|
|
133
|
+
case "received":
|
|
134
|
+
return 2
|
|
135
|
+
default:
|
|
136
|
+
return 0
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function getRefundLabel(paymentStatus?: string | null) {
|
|
141
|
+
const payment = (paymentStatus || "").toLowerCase()
|
|
142
|
+
if (payment === "refunded") return "Refunded"
|
|
143
|
+
if (payment === "partially_refunded") return "Partially refunded"
|
|
144
|
+
return null
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function getLatestRefundDate(order: HttpTypes.StoreOrder): string | Date | null {
|
|
148
|
+
const collections =
|
|
149
|
+
(
|
|
150
|
+
order as HttpTypes.StoreOrder & {
|
|
151
|
+
payment_collections?: Array<{
|
|
152
|
+
payments?: Array<{
|
|
153
|
+
refunds?: Array<{ created_at?: string | Date | null }>
|
|
154
|
+
}>
|
|
155
|
+
}>
|
|
156
|
+
}
|
|
157
|
+
).payment_collections || []
|
|
158
|
+
|
|
159
|
+
let latest: string | Date | null = null
|
|
160
|
+
for (const collection of collections) {
|
|
161
|
+
for (const payment of collection.payments || []) {
|
|
162
|
+
for (const refund of payment.refunds || []) {
|
|
163
|
+
if (!refund.created_at) continue
|
|
164
|
+
if (!latest || new Date(refund.created_at) > new Date(latest)) {
|
|
165
|
+
latest = refund.created_at
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return latest
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function TrackMarker({
|
|
174
|
+
done,
|
|
175
|
+
current,
|
|
176
|
+
cancelled,
|
|
177
|
+
}: {
|
|
178
|
+
done: boolean
|
|
179
|
+
current: boolean
|
|
180
|
+
cancelled: boolean
|
|
181
|
+
}) {
|
|
182
|
+
if (cancelled) {
|
|
183
|
+
return (
|
|
184
|
+
<span className="anim-order__track-dot anim-order__track-dot--icon" aria-hidden>
|
|
185
|
+
<svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round">
|
|
186
|
+
<line x1="18" y1="6" x2="6" y2="18" />
|
|
187
|
+
<line x1="6" y1="6" x2="18" y2="18" />
|
|
188
|
+
</svg>
|
|
189
|
+
</span>
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (done) {
|
|
194
|
+
return (
|
|
195
|
+
<span className="anim-order__track-dot anim-order__track-dot--icon" aria-hidden>
|
|
196
|
+
<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
|
|
197
|
+
<polyline points="20 6 9 17 4 12" />
|
|
198
|
+
</svg>
|
|
199
|
+
</span>
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (current) {
|
|
204
|
+
return <span className="anim-order__track-dot anim-order__track-dot--current" aria-hidden />
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return <span className="anim-order__track-dot" aria-hidden />
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function TrackingTimeline({
|
|
211
|
+
title,
|
|
212
|
+
subtitle,
|
|
213
|
+
steps,
|
|
214
|
+
currentStatusIndex,
|
|
215
|
+
isCancelled = false,
|
|
216
|
+
}: {
|
|
217
|
+
title: string
|
|
218
|
+
subtitle?: string
|
|
219
|
+
steps: TrackingStep[]
|
|
220
|
+
currentStatusIndex: number
|
|
221
|
+
isCancelled?: boolean
|
|
222
|
+
}) {
|
|
223
|
+
return (
|
|
224
|
+
<div className="anim-order__track">
|
|
225
|
+
<div className="anim-order__track-head">
|
|
226
|
+
<h2 className="anim-order__track-heading">{title}</h2>
|
|
227
|
+
{subtitle ? <p className="anim-order__track-subtitle">{subtitle}</p> : null}
|
|
228
|
+
</div>
|
|
229
|
+
|
|
230
|
+
<ol className="anim-order__track-list">
|
|
231
|
+
{steps.map((step, index) => {
|
|
232
|
+
const isActive = isCancelled ? true : index <= currentStatusIndex
|
|
233
|
+
const isCurrent = isCancelled
|
|
234
|
+
? index === steps.length - 1
|
|
235
|
+
: index === currentStatusIndex
|
|
236
|
+
const isCancelledStep = isCancelled && index === steps.length - 1
|
|
237
|
+
const isDone = isActive && !isCurrent && !isCancelledStep
|
|
238
|
+
const dateLabel = step.date ? formatStepDate(step.date) : ""
|
|
239
|
+
|
|
240
|
+
return (
|
|
241
|
+
<li
|
|
242
|
+
key={`${step.status}-${index}`}
|
|
243
|
+
className={[
|
|
244
|
+
"anim-order__track-step",
|
|
245
|
+
isActive ? "is-active" : "",
|
|
246
|
+
isDone ? "is-done" : "",
|
|
247
|
+
isCurrent ? "is-current" : "",
|
|
248
|
+
isCancelledStep ? "is-cancelled" : "",
|
|
249
|
+
]
|
|
250
|
+
.filter(Boolean)
|
|
251
|
+
.join(" ")}
|
|
252
|
+
>
|
|
253
|
+
<TrackMarker
|
|
254
|
+
done={isDone}
|
|
255
|
+
current={isCurrent && !isCancelledStep}
|
|
256
|
+
cancelled={isCancelledStep}
|
|
257
|
+
/>
|
|
258
|
+
<div className="anim-order__track-body">
|
|
259
|
+
<p className="anim-order__track-title">
|
|
260
|
+
{step.title}
|
|
261
|
+
{isCurrent && !isCancelledStep ? (
|
|
262
|
+
<span className="anim-order__track-now">Now</span>
|
|
263
|
+
) : null}
|
|
264
|
+
</p>
|
|
265
|
+
{dateLabel ? (
|
|
266
|
+
<p className="anim-order__track-date">{dateLabel}</p>
|
|
267
|
+
) : null}
|
|
268
|
+
</div>
|
|
269
|
+
</li>
|
|
270
|
+
)
|
|
271
|
+
})}
|
|
272
|
+
</ol>
|
|
273
|
+
</div>
|
|
274
|
+
)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export default function OrderTracking({ order }: { order: HttpTypes.StoreOrder }) {
|
|
278
|
+
const isCancelled =
|
|
279
|
+
(order.status || "").toLowerCase() === "canceled" ||
|
|
280
|
+
(order.status || "").toLowerCase() === "cancelled"
|
|
281
|
+
|
|
282
|
+
const activeReturn = getActiveReturn(order)
|
|
283
|
+
const returnStatus = (activeReturn?.status || "").toLowerCase()
|
|
284
|
+
const paymentStatus = (order.payment_status || "").toLowerCase()
|
|
285
|
+
const isReturnCancelled =
|
|
286
|
+
returnStatus === "canceled" || returnStatus === "cancelled"
|
|
287
|
+
const hasReturn = Boolean(activeReturn)
|
|
288
|
+
const refundLabel = getRefundLabel(paymentStatus)
|
|
289
|
+
const refundDate = getLatestRefundDate(order)
|
|
290
|
+
|
|
291
|
+
const fromMetadata = !!(order.metadata as { estimated_delivery?: string } | null)
|
|
292
|
+
?.estimated_delivery
|
|
293
|
+
const estimatedDelivery = fromMetadata
|
|
294
|
+
? new Date(
|
|
295
|
+
(order.metadata as { estimated_delivery: string }).estimated_delivery
|
|
296
|
+
)
|
|
297
|
+
: new Date(new Date(order.created_at).getTime() + 7 * 24 * 60 * 60 * 1000)
|
|
298
|
+
|
|
299
|
+
const fulfillments = getFulfillments(order)
|
|
300
|
+
const packedAt = firstDate(fulfillments, "packed_at")
|
|
301
|
+
const shippedAt = firstDate(fulfillments, "shipped_at")
|
|
302
|
+
const deliveredAt = firstDate(fulfillments, "delivered_at")
|
|
303
|
+
const statusIndex = getDeliveryStatusIndex(order)
|
|
304
|
+
|
|
305
|
+
const deliverySteps: TrackingStep[] = [
|
|
306
|
+
{ title: "Order Confirmed", status: "confirmed", date: order.created_at },
|
|
307
|
+
{ title: "Shipped", status: "shipped", date: packedAt || shippedAt },
|
|
308
|
+
{ title: "Out For Delivery", status: "out_for_delivery", date: shippedAt },
|
|
309
|
+
{ title: "Delivered", status: "delivered", date: deliveredAt },
|
|
310
|
+
]
|
|
311
|
+
|
|
312
|
+
const cancelledSteps: TrackingStep[] = [
|
|
313
|
+
{ title: "Order Confirmed", status: "confirmed", date: order.created_at },
|
|
314
|
+
{
|
|
315
|
+
title: "Order Cancelled",
|
|
316
|
+
status: "canceled",
|
|
317
|
+
date: (order as { canceled_at?: string }).canceled_at || new Date(),
|
|
318
|
+
},
|
|
319
|
+
]
|
|
320
|
+
|
|
321
|
+
const returnSteps: TrackingStep[] = isReturnCancelled
|
|
322
|
+
? [
|
|
323
|
+
{
|
|
324
|
+
title: "Return Requested",
|
|
325
|
+
status: "return_requested",
|
|
326
|
+
date: activeReturn?.created_at || null,
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
title: "Return Cancelled",
|
|
330
|
+
status: "canceled",
|
|
331
|
+
date: activeReturn?.canceled_at || activeReturn?.updated_at || new Date(),
|
|
332
|
+
},
|
|
333
|
+
]
|
|
334
|
+
: [
|
|
335
|
+
{
|
|
336
|
+
title: "Return Requested",
|
|
337
|
+
status: "return_requested",
|
|
338
|
+
date: activeReturn?.created_at || null,
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
title: "Partially Received",
|
|
342
|
+
status: "return_partial",
|
|
343
|
+
date:
|
|
344
|
+
returnStatus === "partially_received" ||
|
|
345
|
+
returnStatus === "received" ||
|
|
346
|
+
Boolean(refundLabel)
|
|
347
|
+
? activeReturn?.updated_at || null
|
|
348
|
+
: null,
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
title: "Return Received",
|
|
352
|
+
status: "return_received",
|
|
353
|
+
date:
|
|
354
|
+
returnStatus === "received" || Boolean(refundLabel)
|
|
355
|
+
? activeReturn?.received_at || activeReturn?.updated_at || null
|
|
356
|
+
: null,
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
title:
|
|
360
|
+
refundLabel === "Partially refunded" ? "Partially Refunded" : "Refunded",
|
|
361
|
+
status: "refunded",
|
|
362
|
+
date: refundLabel ? refundDate || activeReturn?.updated_at || null : null,
|
|
363
|
+
},
|
|
364
|
+
]
|
|
365
|
+
|
|
366
|
+
if (isCancelled) {
|
|
367
|
+
return (
|
|
368
|
+
<TrackingTimeline
|
|
369
|
+
title="Order tracking"
|
|
370
|
+
steps={cancelledSteps}
|
|
371
|
+
currentStatusIndex={1}
|
|
372
|
+
isCancelled
|
|
373
|
+
/>
|
|
374
|
+
)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const returnSubtitle = [
|
|
378
|
+
returnStatus ? `Return: ${returnStatus.replace(/_/g, " ")}` : null,
|
|
379
|
+
refundLabel ? `Payment: ${refundLabel.toLowerCase()}` : null,
|
|
380
|
+
]
|
|
381
|
+
.filter(Boolean)
|
|
382
|
+
.join(" · ")
|
|
383
|
+
|
|
384
|
+
return (
|
|
385
|
+
<div className="anim-order__track-stack">
|
|
386
|
+
<TrackingTimeline
|
|
387
|
+
title="Order tracking"
|
|
388
|
+
subtitle={
|
|
389
|
+
statusIndex >= 3
|
|
390
|
+
? `Delivered${deliveredAt ? `: ${formatFullDate(deliveredAt)}` : ""}`
|
|
391
|
+
: `${fromMetadata ? "Shiprocket delivery: " : "Estimated delivery: "}${formatFullDate(estimatedDelivery)}`
|
|
392
|
+
}
|
|
393
|
+
steps={deliverySteps}
|
|
394
|
+
currentStatusIndex={statusIndex}
|
|
395
|
+
/>
|
|
396
|
+
|
|
397
|
+
{hasReturn ? (
|
|
398
|
+
<TrackingTimeline
|
|
399
|
+
title="Return tracking"
|
|
400
|
+
subtitle={returnSubtitle}
|
|
401
|
+
steps={returnSteps}
|
|
402
|
+
currentStatusIndex={
|
|
403
|
+
isReturnCancelled
|
|
404
|
+
? 1
|
|
405
|
+
: getReturnStatusIndex(returnStatus, paymentStatus)
|
|
406
|
+
}
|
|
407
|
+
isCancelled={isReturnCancelled}
|
|
408
|
+
/>
|
|
409
|
+
) : null}
|
|
410
|
+
</div>
|
|
411
|
+
)
|
|
412
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { HttpTypes } from "@medusajs/types"
|
|
2
|
+
|
|
3
|
+
type OrderItemLike = {
|
|
4
|
+
product_handle?: string | null
|
|
5
|
+
product?: { handle?: string | null } | null
|
|
6
|
+
variant?: { product?: { handle?: string | null } | null } | null
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** PDP path for the first line item, or `/store` when no handle is available. */
|
|
10
|
+
export function productDetailsPathFromOrder(
|
|
11
|
+
order?: HttpTypes.StoreOrder | null
|
|
12
|
+
): string {
|
|
13
|
+
const item = (order?.items?.[0] ?? null) as OrderItemLike | null
|
|
14
|
+
const handle =
|
|
15
|
+
item?.product_handle?.trim() ||
|
|
16
|
+
item?.product?.handle?.trim() ||
|
|
17
|
+
item?.variant?.product?.handle?.trim() ||
|
|
18
|
+
""
|
|
19
|
+
|
|
20
|
+
if (!handle) return "/store"
|
|
21
|
+
return `/products/${encodeURIComponent(handle)}`
|
|
22
|
+
}
|