@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.
- package/package.json +13 -9
- package/src/account-addresses.tsx +307 -287
- package/src/account-guest-orders.tsx +98 -98
- package/src/account-orders.tsx +260 -260
- package/src/account-overview.tsx +133 -133
- package/src/account-payment-methods.tsx +328 -328
- package/src/account-profile-logic.ts +282 -282
- package/src/account-profile.tsx +111 -111
- package/src/account-theme.ts +91 -91
- package/src/account-utils.ts +20 -20
- package/src/address-actions.ts +74 -0
- package/src/auth-server.ts +442 -442
- package/src/card-expiry.test.ts +71 -0
- package/src/card-expiry.ts +29 -0
- package/src/commerce-auth-otp-modal.css +125 -125
- package/src/commerce-auth-otp-modal.tsx +121 -121
- package/src/default-address.test.ts +143 -0
- package/src/default-address.ts +51 -0
- package/src/forgot-password-form.tsx +109 -109
- package/src/google-auth-section.tsx +22 -22
- package/src/google-sign-in-section.tsx +67 -67
- package/src/guest-order-modal.tsx +215 -215
- package/src/index.ts +7 -7
- package/src/is-next-redirect.ts +60 -60
- package/src/login-form.tsx +469 -469
- package/src/manifest.ts +11 -11
- package/src/order-history-status.test.ts +224 -224
- package/src/order-history-status.ts +244 -244
- package/src/otp-component.ts +14 -14
- package/src/otp-input.tsx +46 -46
- package/src/otp-unified-form.tsx +333 -333
- package/src/otp-verification-modal.tsx +112 -112
- package/src/payment-methods-actions.ts +243 -243
- package/src/profile-email-field.tsx +116 -116
- package/src/profile-email.test.ts +182 -182
- package/src/profile-email.ts +54 -54
- package/src/profile-phone-field.tsx +119 -119
- package/src/register-form.tsx +339 -339
- package/src/reorder-checkout-address.ts +103 -101
- package/src/reset-password-form.tsx +103 -103
- package/src/reset-password-page.tsx +12 -12
- package/src/segment.tsx +272 -272
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, it } from "node:test"
|
|
2
|
+
import assert from "node:assert/strict"
|
|
3
|
+
import { getCardExpiryError } from "./card-expiry.ts"
|
|
4
|
+
|
|
5
|
+
const now = new Date("2026-12-04T12:00:00.000Z")
|
|
6
|
+
|
|
7
|
+
const CASES: {
|
|
8
|
+
name: string
|
|
9
|
+
expiry: string
|
|
10
|
+
want: string | undefined
|
|
11
|
+
}[] = [
|
|
12
|
+
{
|
|
13
|
+
name: "rejects empty expiry",
|
|
14
|
+
expiry: "",
|
|
15
|
+
want: "Expiry date is required",
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
name: "rejects whitespace-only expiry",
|
|
19
|
+
expiry: " ",
|
|
20
|
+
want: "Expiry date is required",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "rejects single-digit month format",
|
|
24
|
+
expiry: "8/25",
|
|
25
|
+
want: "Use MM/YY format (e.g. 08/28)",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: "rejects month 00",
|
|
29
|
+
expiry: "00/28",
|
|
30
|
+
want: "Use MM/YY format (e.g. 08/28)",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "rejects month 13",
|
|
34
|
+
expiry: "13/28",
|
|
35
|
+
want: "Use MM/YY format (e.g. 08/28)",
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: "rejects expired August 2025 when current is Dec 2026",
|
|
39
|
+
expiry: "08/25",
|
|
40
|
+
want: "Your card has expired. Please enter a valid expiry date.",
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "rejects expired previous month in the same year",
|
|
44
|
+
expiry: "11/26",
|
|
45
|
+
want: "Your card has expired. Please enter a valid expiry date.",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "accepts the current month",
|
|
49
|
+
expiry: "12/26",
|
|
50
|
+
want: undefined,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "accepts a future month in the same year",
|
|
54
|
+
// Dec is month 12; use next year for a clear future case
|
|
55
|
+
expiry: "01/27",
|
|
56
|
+
want: undefined,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "accepts a future year",
|
|
60
|
+
expiry: "08/28",
|
|
61
|
+
want: undefined,
|
|
62
|
+
},
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
describe("getCardExpiryError", () => {
|
|
66
|
+
for (const tc of CASES) {
|
|
67
|
+
it(tc.name, () => {
|
|
68
|
+
assert.equal(getCardExpiryError(tc.expiry, now), tc.want)
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a card expiry in MM/YY format against the current month/year.
|
|
3
|
+
* Cards remain valid through the end of the expiry month.
|
|
4
|
+
*/
|
|
5
|
+
export function getCardExpiryError(
|
|
6
|
+
expiry: string,
|
|
7
|
+
now: Date = new Date()
|
|
8
|
+
): string | undefined {
|
|
9
|
+
const value = expiry.trim()
|
|
10
|
+
if (!value) {
|
|
11
|
+
return "Expiry date is required"
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const match = /^(0[1-9]|1[0-2])\/(\d{2})$/.exec(value)
|
|
15
|
+
if (!match) {
|
|
16
|
+
return "Use MM/YY format (e.g. 08/28)"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const month = Number(match[1])
|
|
20
|
+
const year = 2000 + Number(match[2])
|
|
21
|
+
const currentYear = now.getFullYear()
|
|
22
|
+
const currentMonth = now.getMonth() + 1
|
|
23
|
+
|
|
24
|
+
if (year < currentYear || (year === currentYear && month < currentMonth)) {
|
|
25
|
+
return "Your card has expired. Please enter a valid expiry date."
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return undefined
|
|
29
|
+
}
|
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
.sr-otp-modal {
|
|
2
|
-
position: fixed;
|
|
3
|
-
inset: 0;
|
|
4
|
-
z-index: 90;
|
|
5
|
-
display: flex;
|
|
6
|
-
align-items: center;
|
|
7
|
-
justify-content: center;
|
|
8
|
-
padding: 1rem;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
.sr-otp-modal__backdrop {
|
|
12
|
-
position: absolute;
|
|
13
|
-
inset: 0;
|
|
14
|
-
border: 0;
|
|
15
|
-
background: rgba(10, 16, 28, 0.48);
|
|
16
|
-
cursor: pointer;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
.sr-otp-modal__panel {
|
|
20
|
-
position: relative;
|
|
21
|
-
z-index: 1;
|
|
22
|
-
width: min(100%, 22rem);
|
|
23
|
-
max-height: min(92vh, 36rem);
|
|
24
|
-
overflow: auto;
|
|
25
|
-
padding: 1.35rem 1.15rem 1.2rem;
|
|
26
|
-
border-radius: 1rem;
|
|
27
|
-
background: #ffffff;
|
|
28
|
-
box-shadow: 0 18px 44px rgba(19, 38, 77, 0.22);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
.sr-otp-modal__panel > div {
|
|
32
|
-
display: flex;
|
|
33
|
-
flex-direction: column;
|
|
34
|
-
gap: 1.15rem;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
.sr-otp-modal__title {
|
|
38
|
-
margin: 0;
|
|
39
|
-
font-family: var(--font-heading, Georgia, serif);
|
|
40
|
-
font-size: 1.65rem;
|
|
41
|
-
font-weight: 500;
|
|
42
|
-
line-height: 1.2;
|
|
43
|
-
color: #0a0a0a;
|
|
44
|
-
text-align: center;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
.sr-otp-modal__copy,
|
|
48
|
-
.sr-otp-modal__hint,
|
|
49
|
-
.sr-otp-modal__success {
|
|
50
|
-
margin: 0.4rem 0 0;
|
|
51
|
-
font-size: 0.875rem;
|
|
52
|
-
line-height: 1.5;
|
|
53
|
-
color: #5c6570;
|
|
54
|
-
text-align: center;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
.sr-otp-modal__panel [class*="justify-center"] {
|
|
58
|
-
display: flex;
|
|
59
|
-
justify-content: center;
|
|
60
|
-
gap: 0.45rem;
|
|
61
|
-
width: 100%;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
.sr-otp-modal__cell {
|
|
65
|
-
display: flex;
|
|
66
|
-
align-items: center;
|
|
67
|
-
justify-content: center;
|
|
68
|
-
width: 2.35rem;
|
|
69
|
-
height: 2.85rem;
|
|
70
|
-
border: 1.5px solid #d5dde6;
|
|
71
|
-
border-radius: 0.55rem;
|
|
72
|
-
background: #f7fafc;
|
|
73
|
-
font-size: 1.15rem;
|
|
74
|
-
font-weight: 700;
|
|
75
|
-
color: #13264d;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
.sr-otp-modal__cell.is-active,
|
|
79
|
-
.sr-otp-modal__cell.is-filled {
|
|
80
|
-
border-color: #13264d;
|
|
81
|
-
background: #ffffff;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
.sr-otp-modal__error {
|
|
85
|
-
margin: 0;
|
|
86
|
-
color: #b42318;
|
|
87
|
-
text-align: center;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
.sr-otp-modal__verify,
|
|
91
|
-
.sr-otp-modal__resend {
|
|
92
|
-
display: block;
|
|
93
|
-
width: 100%;
|
|
94
|
-
min-height: 2.75rem;
|
|
95
|
-
border-radius: 999px;
|
|
96
|
-
font-size: 0.75rem;
|
|
97
|
-
font-weight: 700;
|
|
98
|
-
letter-spacing: 0.12em;
|
|
99
|
-
text-transform: uppercase;
|
|
100
|
-
cursor: pointer;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
.sr-otp-modal__verify {
|
|
104
|
-
border: 1px solid #13264d;
|
|
105
|
-
background: #13264d;
|
|
106
|
-
color: #ffffff;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
.sr-otp-modal__verify:disabled {
|
|
110
|
-
opacity: 0.55;
|
|
111
|
-
cursor: not-allowed;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
.sr-otp-modal__resend {
|
|
115
|
-
border: 0;
|
|
116
|
-
background: transparent;
|
|
117
|
-
color: #13264d;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
@media (max-width: 380px) {
|
|
121
|
-
.sr-otp-modal__cell {
|
|
122
|
-
width: 2.05rem;
|
|
123
|
-
height: 2.55rem;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
1
|
+
.sr-otp-modal {
|
|
2
|
+
position: fixed;
|
|
3
|
+
inset: 0;
|
|
4
|
+
z-index: 90;
|
|
5
|
+
display: flex;
|
|
6
|
+
align-items: center;
|
|
7
|
+
justify-content: center;
|
|
8
|
+
padding: 1rem;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.sr-otp-modal__backdrop {
|
|
12
|
+
position: absolute;
|
|
13
|
+
inset: 0;
|
|
14
|
+
border: 0;
|
|
15
|
+
background: rgba(10, 16, 28, 0.48);
|
|
16
|
+
cursor: pointer;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.sr-otp-modal__panel {
|
|
20
|
+
position: relative;
|
|
21
|
+
z-index: 1;
|
|
22
|
+
width: min(100%, 22rem);
|
|
23
|
+
max-height: min(92vh, 36rem);
|
|
24
|
+
overflow: auto;
|
|
25
|
+
padding: 1.35rem 1.15rem 1.2rem;
|
|
26
|
+
border-radius: 1rem;
|
|
27
|
+
background: #ffffff;
|
|
28
|
+
box-shadow: 0 18px 44px rgba(19, 38, 77, 0.22);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.sr-otp-modal__panel > div {
|
|
32
|
+
display: flex;
|
|
33
|
+
flex-direction: column;
|
|
34
|
+
gap: 1.15rem;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.sr-otp-modal__title {
|
|
38
|
+
margin: 0;
|
|
39
|
+
font-family: var(--font-heading, Georgia, serif);
|
|
40
|
+
font-size: 1.65rem;
|
|
41
|
+
font-weight: 500;
|
|
42
|
+
line-height: 1.2;
|
|
43
|
+
color: #0a0a0a;
|
|
44
|
+
text-align: center;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.sr-otp-modal__copy,
|
|
48
|
+
.sr-otp-modal__hint,
|
|
49
|
+
.sr-otp-modal__success {
|
|
50
|
+
margin: 0.4rem 0 0;
|
|
51
|
+
font-size: 0.875rem;
|
|
52
|
+
line-height: 1.5;
|
|
53
|
+
color: #5c6570;
|
|
54
|
+
text-align: center;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.sr-otp-modal__panel [class*="justify-center"] {
|
|
58
|
+
display: flex;
|
|
59
|
+
justify-content: center;
|
|
60
|
+
gap: 0.45rem;
|
|
61
|
+
width: 100%;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.sr-otp-modal__cell {
|
|
65
|
+
display: flex;
|
|
66
|
+
align-items: center;
|
|
67
|
+
justify-content: center;
|
|
68
|
+
width: 2.35rem;
|
|
69
|
+
height: 2.85rem;
|
|
70
|
+
border: 1.5px solid #d5dde6;
|
|
71
|
+
border-radius: 0.55rem;
|
|
72
|
+
background: #f7fafc;
|
|
73
|
+
font-size: 1.15rem;
|
|
74
|
+
font-weight: 700;
|
|
75
|
+
color: #13264d;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.sr-otp-modal__cell.is-active,
|
|
79
|
+
.sr-otp-modal__cell.is-filled {
|
|
80
|
+
border-color: #13264d;
|
|
81
|
+
background: #ffffff;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.sr-otp-modal__error {
|
|
85
|
+
margin: 0;
|
|
86
|
+
color: #b42318;
|
|
87
|
+
text-align: center;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
.sr-otp-modal__verify,
|
|
91
|
+
.sr-otp-modal__resend {
|
|
92
|
+
display: block;
|
|
93
|
+
width: 100%;
|
|
94
|
+
min-height: 2.75rem;
|
|
95
|
+
border-radius: 999px;
|
|
96
|
+
font-size: 0.75rem;
|
|
97
|
+
font-weight: 700;
|
|
98
|
+
letter-spacing: 0.12em;
|
|
99
|
+
text-transform: uppercase;
|
|
100
|
+
cursor: pointer;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.sr-otp-modal__verify {
|
|
104
|
+
border: 1px solid #13264d;
|
|
105
|
+
background: #13264d;
|
|
106
|
+
color: #ffffff;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.sr-otp-modal__verify:disabled {
|
|
110
|
+
opacity: 0.55;
|
|
111
|
+
cursor: not-allowed;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.sr-otp-modal__resend {
|
|
115
|
+
border: 0;
|
|
116
|
+
background: transparent;
|
|
117
|
+
color: #13264d;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@media (max-width: 380px) {
|
|
121
|
+
.sr-otp-modal__cell {
|
|
122
|
+
width: 2.05rem;
|
|
123
|
+
height: 2.55rem;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import { useEffect, useState } from "react"
|
|
4
|
-
import { createPortal } from "react-dom"
|
|
5
|
-
import OtpInput from "@pradip1995/commerce-auth/components/otp-input"
|
|
6
|
-
import type { OtpVerificationModalProps } from "./otp-verification-modal"
|
|
7
|
-
import "./commerce-auth-otp-modal.css"
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* OTP modal styled like commerce-auth/chocomelon, compatible with segment-login-template props.
|
|
11
|
-
* Use in pages.config: `"otpComponent": "@pradip1995/segment-login-template/commerce-auth-otp-modal"`
|
|
12
|
-
*/
|
|
13
|
-
export default function CommerceAuthOtpModal({
|
|
14
|
-
open,
|
|
15
|
-
title,
|
|
16
|
-
description,
|
|
17
|
-
otp,
|
|
18
|
-
onOtpChange,
|
|
19
|
-
error,
|
|
20
|
-
pending,
|
|
21
|
-
success = false,
|
|
22
|
-
successTitle = "Verified",
|
|
23
|
-
successMessage = "Verification successful!",
|
|
24
|
-
verifyLabel = "Verify",
|
|
25
|
-
onVerify,
|
|
26
|
-
onResend,
|
|
27
|
-
onClose,
|
|
28
|
-
preventClose = false,
|
|
29
|
-
extraContent,
|
|
30
|
-
}: OtpVerificationModalProps) {
|
|
31
|
-
const [mounted, setMounted] = useState(false)
|
|
32
|
-
|
|
33
|
-
useEffect(() => {
|
|
34
|
-
setMounted(true)
|
|
35
|
-
}, [])
|
|
36
|
-
|
|
37
|
-
if (!open || !mounted) return null
|
|
38
|
-
|
|
39
|
-
function handleBackdropClick() {
|
|
40
|
-
if (preventClose || pending || success) return
|
|
41
|
-
onClose?.()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return createPortal(
|
|
45
|
-
<div className="sr-otp-modal" role="presentation">
|
|
46
|
-
<button
|
|
47
|
-
type="button"
|
|
48
|
-
className="sr-otp-modal__backdrop absolute inset-0"
|
|
49
|
-
aria-label="Close verification dialog"
|
|
50
|
-
onClick={handleBackdropClick}
|
|
51
|
-
/>
|
|
52
|
-
<div className="sr-otp-modal__panel relative w-full max-w-md p-6">
|
|
53
|
-
<div className="flex flex-col gap-6 py-4 px-2">
|
|
54
|
-
<div className="text-center">
|
|
55
|
-
<h2 className="sr-otp-modal__title text-2xl font-bold mb-2">
|
|
56
|
-
{success ? successTitle : title}
|
|
57
|
-
</h2>
|
|
58
|
-
{!success && (
|
|
59
|
-
<p className="sr-otp-modal__copy text-sm leading-relaxed max-w-[320px] mx-auto">
|
|
60
|
-
{description}
|
|
61
|
-
</p>
|
|
62
|
-
)}
|
|
63
|
-
</div>
|
|
64
|
-
|
|
65
|
-
{success ? (
|
|
66
|
-
<div className="text-center py-6">
|
|
67
|
-
<p className="sr-otp-modal__success font-bold">{successMessage}</p>
|
|
68
|
-
</div>
|
|
69
|
-
) : (
|
|
70
|
-
<>
|
|
71
|
-
{extraContent}
|
|
72
|
-
|
|
73
|
-
<OtpInput
|
|
74
|
-
value={otp}
|
|
75
|
-
onChange={onOtpChange}
|
|
76
|
-
autoFocus
|
|
77
|
-
activeClassName="sr-otp-modal__cell is-active"
|
|
78
|
-
filledClassName="sr-otp-modal__cell is-filled"
|
|
79
|
-
emptyClassName="sr-otp-modal__cell"
|
|
80
|
-
/>
|
|
81
|
-
|
|
82
|
-
{error && (
|
|
83
|
-
<p className="sr-otp-modal__error text-xs text-center" role="alert">
|
|
84
|
-
{error}
|
|
85
|
-
</p>
|
|
86
|
-
)}
|
|
87
|
-
|
|
88
|
-
<div className="space-y-3">
|
|
89
|
-
<button
|
|
90
|
-
type="button"
|
|
91
|
-
onClick={onVerify}
|
|
92
|
-
disabled={pending || otp.length < 6}
|
|
93
|
-
className="sr-otp-modal__verify w-full"
|
|
94
|
-
>
|
|
95
|
-
{pending ? "Verifying…" : verifyLabel}
|
|
96
|
-
</button>
|
|
97
|
-
|
|
98
|
-
<button
|
|
99
|
-
type="button"
|
|
100
|
-
onClick={onResend}
|
|
101
|
-
disabled={pending}
|
|
102
|
-
className="sr-otp-modal__resend w-full text-center disabled:opacity-60"
|
|
103
|
-
>
|
|
104
|
-
Resend code
|
|
105
|
-
</button>
|
|
106
|
-
</div>
|
|
107
|
-
|
|
108
|
-
{preventClose && (
|
|
109
|
-
<p className="sr-otp-modal__hint text-xs text-center">
|
|
110
|
-
Verification is required to complete{" "}
|
|
111
|
-
{verifyLabel.toLowerCase().includes("sign in") ? "sign in" : "registration"}.
|
|
112
|
-
</p>
|
|
113
|
-
)}
|
|
114
|
-
</>
|
|
115
|
-
)}
|
|
116
|
-
</div>
|
|
117
|
-
</div>
|
|
118
|
-
</div>,
|
|
119
|
-
document.body
|
|
120
|
-
)
|
|
121
|
-
}
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from "react"
|
|
4
|
+
import { createPortal } from "react-dom"
|
|
5
|
+
import OtpInput from "@pradip1995/commerce-auth/components/otp-input"
|
|
6
|
+
import type { OtpVerificationModalProps } from "./otp-verification-modal"
|
|
7
|
+
import "./commerce-auth-otp-modal.css"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* OTP modal styled like commerce-auth/chocomelon, compatible with segment-login-template props.
|
|
11
|
+
* Use in pages.config: `"otpComponent": "@pradip1995/segment-login-template/commerce-auth-otp-modal"`
|
|
12
|
+
*/
|
|
13
|
+
export default function CommerceAuthOtpModal({
|
|
14
|
+
open,
|
|
15
|
+
title,
|
|
16
|
+
description,
|
|
17
|
+
otp,
|
|
18
|
+
onOtpChange,
|
|
19
|
+
error,
|
|
20
|
+
pending,
|
|
21
|
+
success = false,
|
|
22
|
+
successTitle = "Verified",
|
|
23
|
+
successMessage = "Verification successful!",
|
|
24
|
+
verifyLabel = "Verify",
|
|
25
|
+
onVerify,
|
|
26
|
+
onResend,
|
|
27
|
+
onClose,
|
|
28
|
+
preventClose = false,
|
|
29
|
+
extraContent,
|
|
30
|
+
}: OtpVerificationModalProps) {
|
|
31
|
+
const [mounted, setMounted] = useState(false)
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
setMounted(true)
|
|
35
|
+
}, [])
|
|
36
|
+
|
|
37
|
+
if (!open || !mounted) return null
|
|
38
|
+
|
|
39
|
+
function handleBackdropClick() {
|
|
40
|
+
if (preventClose || pending || success) return
|
|
41
|
+
onClose?.()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return createPortal(
|
|
45
|
+
<div className="sr-otp-modal" role="presentation">
|
|
46
|
+
<button
|
|
47
|
+
type="button"
|
|
48
|
+
className="sr-otp-modal__backdrop absolute inset-0"
|
|
49
|
+
aria-label="Close verification dialog"
|
|
50
|
+
onClick={handleBackdropClick}
|
|
51
|
+
/>
|
|
52
|
+
<div className="sr-otp-modal__panel relative w-full max-w-md p-6">
|
|
53
|
+
<div className="flex flex-col gap-6 py-4 px-2">
|
|
54
|
+
<div className="text-center">
|
|
55
|
+
<h2 className="sr-otp-modal__title text-2xl font-bold mb-2">
|
|
56
|
+
{success ? successTitle : title}
|
|
57
|
+
</h2>
|
|
58
|
+
{!success && (
|
|
59
|
+
<p className="sr-otp-modal__copy text-sm leading-relaxed max-w-[320px] mx-auto">
|
|
60
|
+
{description}
|
|
61
|
+
</p>
|
|
62
|
+
)}
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
{success ? (
|
|
66
|
+
<div className="text-center py-6">
|
|
67
|
+
<p className="sr-otp-modal__success font-bold">{successMessage}</p>
|
|
68
|
+
</div>
|
|
69
|
+
) : (
|
|
70
|
+
<>
|
|
71
|
+
{extraContent}
|
|
72
|
+
|
|
73
|
+
<OtpInput
|
|
74
|
+
value={otp}
|
|
75
|
+
onChange={onOtpChange}
|
|
76
|
+
autoFocus
|
|
77
|
+
activeClassName="sr-otp-modal__cell is-active"
|
|
78
|
+
filledClassName="sr-otp-modal__cell is-filled"
|
|
79
|
+
emptyClassName="sr-otp-modal__cell"
|
|
80
|
+
/>
|
|
81
|
+
|
|
82
|
+
{error && (
|
|
83
|
+
<p className="sr-otp-modal__error text-xs text-center" role="alert">
|
|
84
|
+
{error}
|
|
85
|
+
</p>
|
|
86
|
+
)}
|
|
87
|
+
|
|
88
|
+
<div className="space-y-3">
|
|
89
|
+
<button
|
|
90
|
+
type="button"
|
|
91
|
+
onClick={onVerify}
|
|
92
|
+
disabled={pending || otp.length < 6}
|
|
93
|
+
className="sr-otp-modal__verify w-full"
|
|
94
|
+
>
|
|
95
|
+
{pending ? "Verifying…" : verifyLabel}
|
|
96
|
+
</button>
|
|
97
|
+
|
|
98
|
+
<button
|
|
99
|
+
type="button"
|
|
100
|
+
onClick={onResend}
|
|
101
|
+
disabled={pending}
|
|
102
|
+
className="sr-otp-modal__resend w-full text-center disabled:opacity-60"
|
|
103
|
+
>
|
|
104
|
+
Resend code
|
|
105
|
+
</button>
|
|
106
|
+
</div>
|
|
107
|
+
|
|
108
|
+
{preventClose && (
|
|
109
|
+
<p className="sr-otp-modal__hint text-xs text-center">
|
|
110
|
+
Verification is required to complete{" "}
|
|
111
|
+
{verifyLabel.toLowerCase().includes("sign in") ? "sign in" : "registration"}.
|
|
112
|
+
</p>
|
|
113
|
+
)}
|
|
114
|
+
</>
|
|
115
|
+
)}
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
</div>,
|
|
119
|
+
document.body
|
|
120
|
+
)
|
|
121
|
+
}
|