@pradip1995/segment-login-template 0.5.27 → 0.5.28

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.
Files changed (42) hide show
  1. package/package.json +13 -9
  2. package/src/account-addresses.tsx +307 -287
  3. package/src/account-guest-orders.tsx +98 -98
  4. package/src/account-orders.tsx +260 -260
  5. package/src/account-overview.tsx +133 -133
  6. package/src/account-payment-methods.tsx +328 -328
  7. package/src/account-profile-logic.ts +282 -282
  8. package/src/account-profile.tsx +111 -111
  9. package/src/account-theme.ts +91 -91
  10. package/src/account-utils.ts +20 -20
  11. package/src/address-actions.ts +74 -0
  12. package/src/auth-server.ts +442 -442
  13. package/src/card-expiry.test.ts +71 -0
  14. package/src/card-expiry.ts +29 -0
  15. package/src/commerce-auth-otp-modal.css +125 -125
  16. package/src/commerce-auth-otp-modal.tsx +121 -121
  17. package/src/default-address.test.ts +143 -0
  18. package/src/default-address.ts +51 -0
  19. package/src/forgot-password-form.tsx +109 -109
  20. package/src/google-auth-section.tsx +22 -22
  21. package/src/google-sign-in-section.tsx +67 -67
  22. package/src/guest-order-modal.tsx +215 -215
  23. package/src/index.ts +7 -7
  24. package/src/is-next-redirect.ts +60 -60
  25. package/src/login-form.tsx +469 -469
  26. package/src/manifest.ts +11 -11
  27. package/src/order-history-status.test.ts +224 -224
  28. package/src/order-history-status.ts +244 -244
  29. package/src/otp-component.ts +14 -14
  30. package/src/otp-input.tsx +46 -46
  31. package/src/otp-unified-form.tsx +333 -333
  32. package/src/otp-verification-modal.tsx +112 -112
  33. package/src/payment-methods-actions.ts +243 -243
  34. package/src/profile-email-field.tsx +116 -116
  35. package/src/profile-email.test.ts +182 -182
  36. package/src/profile-email.ts +54 -54
  37. package/src/profile-phone-field.tsx +119 -119
  38. package/src/register-form.tsx +339 -339
  39. package/src/reorder-checkout-address.ts +103 -101
  40. package/src/reset-password-form.tsx +103 -103
  41. package/src/reset-password-page.tsx +12 -12
  42. package/src/segment.tsx +272 -272
@@ -1,101 +1,103 @@
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
- }
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
+ import { resolveDefaultAddressId } from "./default-address"
10
+
11
+ type AddressLike = {
12
+ id?: string | null
13
+ first_name?: string | null
14
+ last_name?: string | null
15
+ address_1?: string | null
16
+ address_2?: string | null
17
+ city?: string | null
18
+ country_code?: string | null
19
+ postal_code?: string | null
20
+ province?: string | null
21
+ phone?: string | null
22
+ company?: string | null
23
+ is_default_shipping?: boolean | null
24
+ is_default_billing?: boolean | null
25
+ metadata?: Record<string, unknown> | null
26
+ }
27
+
28
+ function pickDefaultCustomerAddress(
29
+ customer: HttpTypes.StoreCustomer | null | undefined
30
+ ): AddressLike | undefined {
31
+ const addresses = (customer?.addresses ?? []) as AddressLike[]
32
+ const defaultId = resolveDefaultAddressId(addresses)
33
+ if (defaultId) {
34
+ return addresses.find((address) => address.id === defaultId)
35
+ }
36
+ return addresses.find(
37
+ (address) =>
38
+ address.metadata?.is_default === true || address.metadata?.is_default === "true"
39
+ )
40
+ }
41
+
42
+ function hasUsableAddress(address?: AddressLike | null): address is AddressLike {
43
+ return !!(address && (address.address_1?.trim() || address.postal_code?.trim()))
44
+ }
45
+
46
+ function toCartAddress(source: AddressLike) {
47
+ return {
48
+ first_name: source.first_name || undefined,
49
+ last_name: source.last_name || undefined,
50
+ address_1: source.address_1 || undefined,
51
+ address_2: source.address_2 || undefined,
52
+ city: source.city || undefined,
53
+ country_code: source.country_code || undefined,
54
+ postal_code: source.postal_code || undefined,
55
+ province: source.province || undefined,
56
+ phone: source.phone || undefined,
57
+ company: source.company || undefined,
58
+ }
59
+ }
60
+
61
+ /** Clear existing bag so each reorder starts from the order qty, not stacked qty. */
62
+ export async function clearCartLineItems() {
63
+ const cart = await retrieveCart().catch(() => null)
64
+ const items = cart?.items || []
65
+ for (const item of items) {
66
+ if (!item.id) continue
67
+ await deleteLineItem(item.id)
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Prefill checkout cart address after reorder:
73
+ * 1) customer default address, else
74
+ * 2) shipping/billing from the reordered order.
75
+ */
76
+ export async function applyReorderCheckoutAddress(order: HttpTypes.StoreOrder) {
77
+ try {
78
+ const customer = await retrieveCustomer().catch(() => null)
79
+ if (customer?.id === "pending_deletion") return
80
+
81
+ let source = pickDefaultCustomerAddress(customer)
82
+
83
+ if (!hasUsableAddress(source)) {
84
+ source = order.shipping_address || order.billing_address
85
+ }
86
+
87
+ if (!hasUsableAddress(source) && order.id) {
88
+ const fullOrder = await retrieveOrder(order.id).catch(() => null)
89
+ source = fullOrder?.shipping_address || fullOrder?.billing_address
90
+ }
91
+
92
+ if (!hasUsableAddress(source)) return
93
+
94
+ await updateAddressSilently({
95
+ email: order.email || customer?.email || undefined,
96
+ shipping_address: toCartAddress(source),
97
+ same_as_billing: true,
98
+ assignShipping: true,
99
+ })
100
+ } catch {
101
+ // Checkout can still open; user can fill address manually.
102
+ }
103
+ }
@@ -1,103 +1,103 @@
1
- "use client"
2
-
3
- import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
4
- import { useState } from "react"
5
- import { useSearchParams } from "next/navigation"
6
- import { completePasswordResetAction } from "./auth-server"
7
-
8
- export default function ResetPasswordForm({ countryCode }: { countryCode: string }) {
9
- const searchParams = useSearchParams()
10
- const token = searchParams.get("token") || ""
11
- const email = searchParams.get("email") || ""
12
-
13
- const [password, setPassword] = useState("")
14
- const [confirmPassword, setConfirmPassword] = useState("")
15
- const [error, setError] = useState<string | null>(null)
16
- const [pending, setPending] = useState(false)
17
-
18
- async function handleSubmit(e: React.FormEvent) {
19
- e.preventDefault()
20
- setError(null)
21
-
22
- if (!token || !email) {
23
- setError("This reset link is invalid or expired. Request a new one from the sign-in page.")
24
- return
25
- }
26
-
27
- if (password.length < 8) {
28
- setError("Password must be at least 8 characters.")
29
- return
30
- }
31
-
32
- if (password !== confirmPassword) {
33
- setError("Passwords do not match.")
34
- return
35
- }
36
-
37
- setPending(true)
38
-
39
- try {
40
- const result = await completePasswordResetAction({
41
- email,
42
- password,
43
- token,
44
- countryCode,
45
- })
46
-
47
- if (result && !result.success) {
48
- setError(result.error || "Failed to reset password")
49
- setPending(false)
50
- }
51
- } catch (err) {
52
- if (isNextRedirect(err)) throw err
53
- setError(safeErrorMessage(err, "Failed to reset password"))
54
- setPending(false)
55
- }
56
- }
57
-
58
- return (
59
- <div className="w-full max-w-md mx-auto space-y-6">
60
- <div className="text-center space-y-2">
61
- <h1 className="section-heading text-xl">Set a new password</h1>
62
- <p className="text-sm text-muted">Choose a strong password for {email || "your account"}.</p>
63
- </div>
64
-
65
- <form onSubmit={handleSubmit} className="space-y-4">
66
- <label className="block">
67
- <span className="text-xs font-semibold uppercase tracking-[var(--letter-spacing-nav)] text-muted mb-1.5 block">
68
- New password
69
- </span>
70
- <input
71
- type="password"
72
- value={password}
73
- onChange={(e) => setPassword(e.target.value)}
74
- required
75
- autoComplete="new-password"
76
- className="w-full border border-cart-border rounded px-3 py-2.5 text-sm bg-surface focus:outline-none focus:border-brand-accent"
77
- />
78
- </label>
79
-
80
- <label className="block">
81
- <span className="text-xs font-semibold uppercase tracking-[var(--letter-spacing-nav)] text-muted mb-1.5 block">
82
- Confirm password
83
- </span>
84
- <input
85
- type="password"
86
- value={confirmPassword}
87
- onChange={(e) => setConfirmPassword(e.target.value)}
88
- required
89
- autoComplete="new-password"
90
- className="w-full border border-cart-border rounded px-3 py-2.5 text-sm bg-surface focus:outline-none focus:border-brand-accent"
91
- />
92
- </label>
93
-
94
- {error && <p className="text-sm text-brand-sale">{error}</p>}
95
-
96
- <button type="submit" disabled={pending} className="btn-primary w-full disabled:opacity-60">
97
- {pending ? "Updating…" : "Update password"}
98
- </button>
99
- </form>
100
- </div>
101
- )
102
- }
103
-
1
+ "use client"
2
+
3
+ import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
4
+ import { useState } from "react"
5
+ import { useSearchParams } from "next/navigation"
6
+ import { completePasswordResetAction } from "./auth-server"
7
+
8
+ export default function ResetPasswordForm({ countryCode }: { countryCode: string }) {
9
+ const searchParams = useSearchParams()
10
+ const token = searchParams.get("token") || ""
11
+ const email = searchParams.get("email") || ""
12
+
13
+ const [password, setPassword] = useState("")
14
+ const [confirmPassword, setConfirmPassword] = useState("")
15
+ const [error, setError] = useState<string | null>(null)
16
+ const [pending, setPending] = useState(false)
17
+
18
+ async function handleSubmit(e: React.FormEvent) {
19
+ e.preventDefault()
20
+ setError(null)
21
+
22
+ if (!token || !email) {
23
+ setError("This reset link is invalid or expired. Request a new one from the sign-in page.")
24
+ return
25
+ }
26
+
27
+ if (password.length < 8) {
28
+ setError("Password must be at least 8 characters.")
29
+ return
30
+ }
31
+
32
+ if (password !== confirmPassword) {
33
+ setError("Passwords do not match.")
34
+ return
35
+ }
36
+
37
+ setPending(true)
38
+
39
+ try {
40
+ const result = await completePasswordResetAction({
41
+ email,
42
+ password,
43
+ token,
44
+ countryCode,
45
+ })
46
+
47
+ if (result && !result.success) {
48
+ setError(result.error || "Failed to reset password")
49
+ setPending(false)
50
+ }
51
+ } catch (err) {
52
+ if (isNextRedirect(err)) throw err
53
+ setError(safeErrorMessage(err, "Failed to reset password"))
54
+ setPending(false)
55
+ }
56
+ }
57
+
58
+ return (
59
+ <div className="w-full max-w-md mx-auto space-y-6">
60
+ <div className="text-center space-y-2">
61
+ <h1 className="section-heading text-xl">Set a new password</h1>
62
+ <p className="text-sm text-muted">Choose a strong password for {email || "your account"}.</p>
63
+ </div>
64
+
65
+ <form onSubmit={handleSubmit} className="space-y-4">
66
+ <label className="block">
67
+ <span className="text-xs font-semibold uppercase tracking-[var(--letter-spacing-nav)] text-muted mb-1.5 block">
68
+ New password
69
+ </span>
70
+ <input
71
+ type="password"
72
+ value={password}
73
+ onChange={(e) => setPassword(e.target.value)}
74
+ required
75
+ autoComplete="new-password"
76
+ className="w-full border border-cart-border rounded px-3 py-2.5 text-sm bg-surface focus:outline-none focus:border-brand-accent"
77
+ />
78
+ </label>
79
+
80
+ <label className="block">
81
+ <span className="text-xs font-semibold uppercase tracking-[var(--letter-spacing-nav)] text-muted mb-1.5 block">
82
+ Confirm password
83
+ </span>
84
+ <input
85
+ type="password"
86
+ value={confirmPassword}
87
+ onChange={(e) => setConfirmPassword(e.target.value)}
88
+ required
89
+ autoComplete="new-password"
90
+ className="w-full border border-cart-border rounded px-3 py-2.5 text-sm bg-surface focus:outline-none focus:border-brand-accent"
91
+ />
92
+ </label>
93
+
94
+ {error && <p className="text-sm text-brand-sale">{error}</p>}
95
+
96
+ <button type="submit" disabled={pending} className="btn-primary w-full disabled:opacity-60">
97
+ {pending ? "Updating…" : "Update password"}
98
+ </button>
99
+ </form>
100
+ </div>
101
+ )
102
+ }
103
+
@@ -1,12 +1,12 @@
1
- "use client"
2
-
3
- import { Suspense } from "react"
4
- import ResetPasswordForm from "./reset-password-form"
5
-
6
- export default function ResetPasswordPage({ countryCode = "in" }: { countryCode?: string }) {
7
- return (
8
- <Suspense fallback={<p className="text-sm text-muted text-center py-8">Loading…</p>}>
9
- <ResetPasswordForm countryCode={countryCode} />
10
- </Suspense>
11
- )
12
- }
1
+ "use client"
2
+
3
+ import { Suspense } from "react"
4
+ import ResetPasswordForm from "./reset-password-form"
5
+
6
+ export default function ResetPasswordPage({ countryCode = "in" }: { countryCode?: string }) {
7
+ return (
8
+ <Suspense fallback={<p className="text-sm text-muted text-center py-8">Loading…</p>}>
9
+ <ResetPasswordForm countryCode={countryCode} />
10
+ </Suspense>
11
+ )
12
+ }