@pradip1995/segment-animation-login 0.1.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.
@@ -0,0 +1,261 @@
1
+ "use client"
2
+
3
+ import { useMemo, useState, type MouseEvent } from "react"
4
+ import Image from "next/image"
5
+ import { usePathname, useRouter } from "next/navigation"
6
+ import type { HttpTypes } from "@medusajs/types"
7
+ import { addToCart } from "@pradip1995/commerce-core/client/actions/cart"
8
+ import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
9
+ import { formatPrice } from "@pradip1995/segment-primitives/format-price"
10
+
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 prettyStatus(value?: string | null) {
23
+ if (!value) return "—"
24
+ return value.replace(/_/g, " ")
25
+ }
26
+
27
+ function fulfillmentTone(order: HttpTypes.StoreOrder): "ok" | "warn" | "bad" | "info" {
28
+ if (isCancelled(order)) return "bad"
29
+ const s = (order.fulfillment_status || order.status || "").toLowerCase()
30
+ if (s === "delivered" || s === "complete" || s === "completed" || s.includes("fulfill")) {
31
+ return "ok"
32
+ }
33
+ if (s.includes("ship")) return "info"
34
+ if (s.includes("not") || s.includes("pending") || s.includes("unfulfill")) return "warn"
35
+ return "info"
36
+ }
37
+
38
+ function paymentTone(status?: string | null): "ok" | "warn" | "bad" | "info" {
39
+ const s = (status || "").toLowerCase()
40
+ if (s === "captured" || s === "paid") return "ok"
41
+ if (s.includes("refund")) return "info"
42
+ if (s.includes("cancel") || s.includes("fail")) return "bad"
43
+ return "warn"
44
+ }
45
+
46
+ function itemImage(item: HttpTypes.StoreOrderLineItem): string {
47
+ if (item.thumbnail?.trim()) return item.thumbnail.trim()
48
+ const product = (item as { product?: { thumbnail?: string | null } }).product
49
+ if (product?.thumbnail?.trim()) return product.thumbnail.trim()
50
+ const variantProduct = item.variant?.product as { thumbnail?: string | null } | undefined
51
+ return variantProduct?.thumbnail?.trim() || ""
52
+ }
53
+
54
+ function itemTitle(item: HttpTypes.StoreOrderLineItem): string {
55
+ return item.product_title || item.title || "Product"
56
+ }
57
+
58
+ function itemTotal(item: HttpTypes.StoreOrderLineItem): number {
59
+ if (typeof item.total === "number") return item.total
60
+ return (item.unit_price || 0) * (item.quantity || 0)
61
+ }
62
+
63
+ export default function AnimationAccountOrders({
64
+ orders,
65
+ }: {
66
+ orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
67
+ }) {
68
+ const [query, setQuery] = useState("")
69
+ const [reorderingId, setReorderingId] = useState<string | null>(null)
70
+ const router = useRouter()
71
+ const pathname = usePathname() || ""
72
+ const countryCode = pathname.split("/").filter(Boolean)[0] || "in"
73
+
74
+ const safeOrders: HttpTypes.StoreOrder[] = useMemo(() => {
75
+ if (Array.isArray(orders)) return orders
76
+ if (Array.isArray((orders as { orders?: HttpTypes.StoreOrder[] })?.orders)) {
77
+ return (orders as { orders: HttpTypes.StoreOrder[] }).orders
78
+ }
79
+ return []
80
+ }, [orders])
81
+
82
+ const filtered = useMemo(() => {
83
+ const q = query.trim().toLowerCase()
84
+ if (!q) return safeOrders
85
+ return safeOrders.filter((order) => {
86
+ const id = String(order.display_id ?? order.id)
87
+ const fulfillment = prettyStatus(order.fulfillment_status)
88
+ const payment = prettyStatus(order.payment_status)
89
+ const itemNames = (order.items || [])
90
+ .map((item) => itemTitle(item))
91
+ .join(" ")
92
+ .toLowerCase()
93
+ return (
94
+ id.toLowerCase().includes(q) ||
95
+ fulfillment.toLowerCase().includes(q) ||
96
+ payment.toLowerCase().includes(q) ||
97
+ itemNames.includes(q)
98
+ )
99
+ })
100
+ }, [safeOrders, query])
101
+
102
+ async function handleReorder(event: MouseEvent, order: HttpTypes.StoreOrder) {
103
+ event.preventDefault()
104
+ event.stopPropagation()
105
+ if (!canReorder(order) || reorderingId) return
106
+ setReorderingId(order.id)
107
+ try {
108
+ for (const item of order.items || []) {
109
+ if (!item.variant_id) continue
110
+ await addToCart({
111
+ variantId: item.variant_id,
112
+ quantity: item.quantity || 1,
113
+ countryCode,
114
+ })
115
+ }
116
+ router.push(`/${countryCode}/cart`)
117
+ } catch {
118
+ setReorderingId(null)
119
+ }
120
+ }
121
+
122
+ return (
123
+ <div className="anim-acct anim-acct--orders">
124
+ <header className="anim-acct__welcome">
125
+ <div>
126
+ <p className="anim-acct__kicker">Purchases</p>
127
+ <h2 className="anim-acct__hello">Order history</h2>
128
+ <p className="anim-acct__signed-in">
129
+ Track status, items, payment, and delivery for every order.
130
+ </p>
131
+ </div>
132
+ <LocalizedLink href="/store" className="anim-acct__btn anim-acct__btn--outline">
133
+ Continue shopping
134
+ </LocalizedLink>
135
+ </header>
136
+
137
+ <label className="anim-acct__search">
138
+ <span className="sr-only">Search orders</span>
139
+ <input
140
+ type="search"
141
+ value={query}
142
+ onChange={(e) => setQuery(e.target.value)}
143
+ placeholder="Search by order #, status, or product"
144
+ />
145
+ </label>
146
+
147
+ {filtered.length === 0 ? (
148
+ <div className="anim-acct__empty">
149
+ <p>
150
+ {safeOrders.length === 0
151
+ ? "No orders yet."
152
+ : "No orders match your search."}
153
+ </p>
154
+ {safeOrders.length === 0 ? (
155
+ <LocalizedLink href="/store" className="anim-acct__btn anim-acct__btn--primary">
156
+ Start shopping
157
+ </LocalizedLink>
158
+ ) : null}
159
+ </div>
160
+ ) : (
161
+ <ul className="anim-acct__order-list">
162
+ {filtered.map((order) => {
163
+ const items = order.items || []
164
+ const currency = order.currency_code
165
+ const fulfillment = prettyStatus(order.fulfillment_status || order.status)
166
+ const payment = prettyStatus(order.payment_status)
167
+ const showReorder = canReorder(order)
168
+
169
+ return (
170
+ <li key={order.id} className="anim-acct__order-card">
171
+ <div className="anim-acct__order-card-head">
172
+ <div>
173
+ <div className="anim-acct__order-card-id-row">
174
+ <h3>Order #{order.display_id ?? order.id.slice(-6)}</h3>
175
+ <span
176
+ className={`anim-acct__status anim-acct__status--${fulfillmentTone(order)}`}
177
+ >
178
+ {fulfillment}
179
+ </span>
180
+ <span
181
+ className={`anim-acct__status anim-acct__status--${paymentTone(order.payment_status)}`}
182
+ >
183
+ Payment: {payment}
184
+ </span>
185
+ </div>
186
+ <p className="anim-acct__order-card-meta">
187
+ {order.created_at
188
+ ? new Date(order.created_at).toLocaleDateString("en-IN", {
189
+ weekday: "short",
190
+ day: "numeric",
191
+ month: "short",
192
+ year: "numeric",
193
+ })
194
+ : "—"}
195
+ {" · "}
196
+ {items.length} item{items.length === 1 ? "" : "s"}
197
+ </p>
198
+ </div>
199
+ <div className="anim-acct__order-card-total">
200
+ <span>Total</span>
201
+ <strong>{formatPrice(order.total, currency)}</strong>
202
+ </div>
203
+ </div>
204
+
205
+ <ul className="anim-acct__order-items">
206
+ {items.map((item) => {
207
+ const img = itemImage(item)
208
+ return (
209
+ <li key={item.id} className="anim-acct__order-item">
210
+ <div className="anim-acct__order-item-thumb">
211
+ {img ? (
212
+ <Image
213
+ src={img}
214
+ alt={itemTitle(item)}
215
+ fill
216
+ className="object-cover"
217
+ unoptimized
218
+ />
219
+ ) : null}
220
+ </div>
221
+ <div className="anim-acct__order-item-meta">
222
+ <p className="anim-acct__order-item-title">{itemTitle(item)}</p>
223
+ <p className="anim-acct__order-item-sub">
224
+ {item.variant?.title ? `${item.variant.title} · ` : ""}
225
+ Qty {item.quantity}
226
+ </p>
227
+ </div>
228
+ <p className="anim-acct__order-item-price">
229
+ {formatPrice(itemTotal(item), currency)}
230
+ </p>
231
+ </li>
232
+ )
233
+ })}
234
+ </ul>
235
+
236
+ <div className="anim-acct__order-card-actions">
237
+ <LocalizedLink
238
+ href={`/orders/${order.id}`}
239
+ className="anim-acct__btn anim-acct__btn--primary anim-acct__btn--sm"
240
+ >
241
+ View details
242
+ </LocalizedLink>
243
+ {showReorder ? (
244
+ <button
245
+ type="button"
246
+ className="anim-acct__btn anim-acct__btn--outline anim-acct__btn--sm"
247
+ disabled={reorderingId === order.id}
248
+ onClick={(event) => handleReorder(event, order)}
249
+ >
250
+ {reorderingId === order.id ? "Adding…" : "Reorder"}
251
+ </button>
252
+ ) : null}
253
+ </div>
254
+ </li>
255
+ )
256
+ })}
257
+ </ul>
258
+ )}
259
+ </div>
260
+ )
261
+ }
@@ -0,0 +1,117 @@
1
+ "use client"
2
+
3
+ import type { HttpTypes } from "@medusajs/types"
4
+ import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
5
+ import { formatPrice } from "@pradip1995/segment-primitives/format-price"
6
+
7
+ function getProfileCompletion(customer: HttpTypes.StoreCustomer): number {
8
+ let score = 0
9
+ if (customer.first_name?.trim()) score += 25
10
+ if (customer.last_name?.trim()) score += 25
11
+ if (customer.email?.trim()) score += 25
12
+ if (customer.phone?.trim()) score += 25
13
+ return score
14
+ }
15
+
16
+ function safeOrdersList(
17
+ orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
18
+ ): HttpTypes.StoreOrder[] {
19
+ if (Array.isArray(orders)) return orders
20
+ if (Array.isArray((orders as { orders?: HttpTypes.StoreOrder[] })?.orders)) {
21
+ return (orders as { orders: HttpTypes.StoreOrder[] }).orders
22
+ }
23
+ return []
24
+ }
25
+
26
+ export default function AnimationAccountOverview({
27
+ customer,
28
+ orders,
29
+ }: {
30
+ customer: HttpTypes.StoreCustomer
31
+ orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
32
+ countryCode: string
33
+ }) {
34
+ const completion = getProfileCompletion(customer)
35
+ const addressCount = customer.addresses?.length ?? 0
36
+ const ordersList = safeOrdersList(orders)
37
+
38
+ return (
39
+ <div className="anim-acct" data-testid="animation-account-overview">
40
+ <header className="anim-acct__welcome">
41
+ <div>
42
+ <p className="anim-acct__kicker">Welcome back</p>
43
+ <h2 className="anim-acct__hello">
44
+ {customer.first_name || "Member"}
45
+ </h2>
46
+ <p className="anim-acct__signed-in">
47
+ Signed in as <strong>{customer.email}</strong>
48
+ </p>
49
+ </div>
50
+ <LocalizedLink href="/account/profile" className="anim-acct__btn anim-acct__btn--outline">
51
+ Edit profile
52
+ </LocalizedLink>
53
+ </header>
54
+
55
+ <div className="anim-acct__stats">
56
+ <LocalizedLink href="/account/profile" className="anim-acct__stat">
57
+ <span className="anim-acct__stat-label">Profile</span>
58
+ <span className="anim-acct__stat-value">{completion}%</span>
59
+ <span className="anim-acct__stat-bar" aria-hidden>
60
+ <span style={{ width: `${completion}%` }} />
61
+ </span>
62
+ <span className="anim-acct__stat-hint">Completion</span>
63
+ </LocalizedLink>
64
+
65
+ <LocalizedLink href="/account/addresses" className="anim-acct__stat">
66
+ <span className="anim-acct__stat-label">Addresses</span>
67
+ <span className="anim-acct__stat-value">{addressCount}</span>
68
+ <span className="anim-acct__stat-hint">Saved for checkout</span>
69
+ </LocalizedLink>
70
+
71
+ <LocalizedLink href="/account/orders" className="anim-acct__stat">
72
+ <span className="anim-acct__stat-label">Orders</span>
73
+ <span className="anim-acct__stat-value">{ordersList.length}</span>
74
+ <span className="anim-acct__stat-hint">Order history</span>
75
+ </LocalizedLink>
76
+ </div>
77
+
78
+ <section className="anim-acct__section">
79
+ <div className="anim-acct__section-head">
80
+ <h3>Recent orders</h3>
81
+ {ordersList.length > 0 ? (
82
+ <LocalizedLink href="/account/orders">View all →</LocalizedLink>
83
+ ) : null}
84
+ </div>
85
+
86
+ {ordersList.length === 0 ? (
87
+ <div className="anim-acct__empty">
88
+ <p>No orders yet.</p>
89
+ <LocalizedLink href="/store" className="anim-acct__btn anim-acct__btn--primary">
90
+ Continue shopping
91
+ </LocalizedLink>
92
+ </div>
93
+ ) : (
94
+ <ul className="anim-acct__orders">
95
+ {ordersList.slice(0, 5).map((order) => (
96
+ <li key={order.id}>
97
+ <LocalizedLink href={`/orders/${order.id}`} className="anim-acct__order">
98
+ <div>
99
+ <span className="anim-acct__order-id">#{order.display_id}</span>
100
+ <span className="anim-acct__order-date">
101
+ {order.created_at
102
+ ? new Date(order.created_at).toLocaleDateString("en-IN", {
103
+ dateStyle: "medium",
104
+ })
105
+ : "—"}
106
+ </span>
107
+ </div>
108
+ <strong>{formatPrice(order.total, order.currency_code)}</strong>
109
+ </LocalizedLink>
110
+ </li>
111
+ ))}
112
+ </ul>
113
+ )}
114
+ </section>
115
+ </div>
116
+ )
117
+ }
@@ -0,0 +1,81 @@
1
+ "use client"
2
+
3
+ import LoginTemplate from "@pradip1995/segment-login-template"
4
+ import type { HttpTypes } from "@medusajs/types"
5
+ import type { OtpVerificationComponent } from "@pradip1995/segment-login-template/otp-component"
6
+ import AnimationAccountOverview from "./account-overview"
7
+ import AnimationAccountAddresses from "./account-addresses"
8
+ import AnimationAccountOrders from "./account-orders"
9
+ import "./animation-account.css"
10
+
11
+ type AnimationAccountPagesProps = {
12
+ countryCode?: string
13
+ path?: string
14
+ customer?: HttpTypes.StoreCustomer | null
15
+ orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
16
+ paymentDetails?: Record<string, unknown>[]
17
+ guestOrders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
18
+ otpComponent?: OtpVerificationComponent
19
+ title?: string
20
+ subtitle?: string
21
+ shopName?: string
22
+ privacyHref?: string
23
+ termsHref?: string
24
+ brandPanel?: unknown
25
+ guestTrackLabel?: string
26
+ authMode?: "otp" | "full"
27
+ }
28
+
29
+ function accountSectionPath(path?: string): string {
30
+ if (!path || path === "login") return "overview"
31
+ const base = path.split("/")[0]
32
+ return base || "overview"
33
+ }
34
+
35
+ export default function AnimationAccountPages(props: AnimationAccountPagesProps) {
36
+ const {
37
+ countryCode = "in",
38
+ path = "",
39
+ customer,
40
+ orders = [],
41
+ otpComponent,
42
+ } = props
43
+
44
+ const section = accountSectionPath(path)
45
+
46
+ if (section === "guest-orders") {
47
+ return (
48
+ <div className="anim-acct anim-acct--panel" data-anim-account>
49
+ <LoginTemplate {...props} otpComponent={otpComponent} />
50
+ </div>
51
+ )
52
+ }
53
+
54
+ if (!customer || customer.id === "pending_deletion") return null
55
+
56
+ if (section === "overview") {
57
+ return (
58
+ <AnimationAccountOverview
59
+ customer={customer}
60
+ orders={orders}
61
+ countryCode={countryCode}
62
+ />
63
+ )
64
+ }
65
+
66
+ if (section === "addresses") {
67
+ return (
68
+ <AnimationAccountAddresses customer={customer} countryCode={countryCode} />
69
+ )
70
+ }
71
+
72
+ if (section === "orders") {
73
+ return <AnimationAccountOrders orders={orders} />
74
+ }
75
+
76
+ return (
77
+ <div className="anim-acct anim-acct--panel" data-anim-account>
78
+ <LoginTemplate {...props} otpComponent={otpComponent} />
79
+ </div>
80
+ )
81
+ }