|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { FormEventHandler } from "react"; |
| 4 | +import { ApiHelper } from "@churchapps/helpers"; |
| 5 | +import { Locale } from "../helpers"; |
| 6 | +import { TextField, Typography, Card, CardContent, Button } from "@mui/material"; |
| 7 | +import { VerificationCodeInput } from "./VerificationCodeInput"; |
| 8 | + |
| 9 | +interface Props { |
| 10 | + defaultEmail?: string; |
| 11 | + isSubmitting: boolean; |
| 12 | + submit: (email: string, code: string) => void; |
| 13 | + loginCallback: () => void; |
| 14 | +} |
| 15 | + |
| 16 | +const RESEND_COOLDOWN_SECONDS = 30; |
| 17 | + |
| 18 | +const buttonSx = { |
| 19 | + backgroundColor: "hsl(218, 85%, 55%)", |
| 20 | + color: "white", |
| 21 | + padding: "12px", |
| 22 | + textTransform: "none", |
| 23 | + fontSize: "16px", |
| 24 | + fontWeight: 500, |
| 25 | + borderRadius: "6px", |
| 26 | + "&:hover": { backgroundColor: "hsl(218, 85%, 50%)" }, |
| 27 | + "&:disabled": { backgroundColor: "#9ca3af" } |
| 28 | +} as const; |
| 29 | + |
| 30 | +const linkStyle: React.CSSProperties = { background: "none", border: "none", color: "#3b82f6", fontSize: "14px", cursor: "pointer", textDecoration: "none" }; |
| 31 | + |
| 32 | +export const EmailCode: React.FC<Props> = (props) => { |
| 33 | + const [errors, setErrors] = React.useState<string[]>([]); |
| 34 | + const [email, setEmail] = React.useState(props.defaultEmail || ""); |
| 35 | + const [isSending, setIsSending] = React.useState(false); |
| 36 | + const [codeSent, setCodeSent] = React.useState(false); |
| 37 | + const [code, setCode] = React.useState(""); |
| 38 | + const [resendCooldown, setResendCooldown] = React.useState(0); |
| 39 | + |
| 40 | + React.useEffect(() => { |
| 41 | + if (resendCooldown <= 0) return; |
| 42 | + const t = setTimeout(() => setResendCooldown(resendCooldown - 1), 1000); |
| 43 | + return () => clearTimeout(t); |
| 44 | + }, [resendCooldown]); |
| 45 | + |
| 46 | + const validEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); |
| 47 | + |
| 48 | + const requestCode = async () => { |
| 49 | + if (!validEmail) { setErrors([Locale.label("login.validate.email")]); return; } |
| 50 | + setIsSending(true); |
| 51 | + try { |
| 52 | + const resp: any = await ApiHelper.postAnonymous("/users/requestLoginCode", { email }, "MembershipApi"); |
| 53 | + if (resp?.errors?.length) setErrors(resp.errors); |
| 54 | + else { |
| 55 | + setErrors([]); |
| 56 | + setCode(""); |
| 57 | + setCodeSent(true); |
| 58 | + setResendCooldown(RESEND_COOLDOWN_SECONDS); |
| 59 | + } |
| 60 | + } catch (e: any) { |
| 61 | + setErrors([e?.message || Locale.label("login.validate.email")]); |
| 62 | + } finally { |
| 63 | + setIsSending(false); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + const verify = (submittedCode: string) => { |
| 68 | + if (props.isSubmitting) return; |
| 69 | + if (submittedCode.length !== 6) { setErrors([Locale.label("login.validate.code")]); return; } |
| 70 | + setErrors([]); |
| 71 | + props.submit(email, submittedCode); |
| 72 | + }; |
| 73 | + |
| 74 | + const sendForm: FormEventHandler = (e) => { e.preventDefault(); requestCode(); }; |
| 75 | + const codeForm: FormEventHandler = (e) => { e.preventDefault(); verify(code); }; |
| 76 | + const busy = isSending || props.isSubmitting; |
| 77 | + |
| 78 | + return ( |
| 79 | + <Card id="email-code-card" sx={{ |
| 80 | + width: "100%", |
| 81 | + maxWidth: { xs: "400px", sm: "500px" }, |
| 82 | + backgroundColor: "white", |
| 83 | + border: "1px solid #e5e7eb", |
| 84 | + boxShadow: "0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05)" |
| 85 | + }}> |
| 86 | + <CardContent sx={{ textAlign: "center", padding: "32px" }}> |
| 87 | + <Typography component="h1" sx={{ fontSize: "24px", fontWeight: "bold", color: "#111827", marginBottom: "8px" }}> |
| 88 | + {Locale.label("login.emailCodeTitle")} |
| 89 | + </Typography> |
| 90 | + <Typography sx={{ color: "#6b7280", marginBottom: "32px" }}> |
| 91 | + {codeSent ? Locale.label("login.enterCodeInstructions") : Locale.label("login.emailCodeInstructions")} |
| 92 | + </Typography> |
| 93 | + |
| 94 | + {errors.length > 0 && ( |
| 95 | + <div style={{ backgroundColor: "#fef2f2", border: "1px solid #fecaca", borderRadius: "6px", padding: "12px", textAlign: "left", marginBottom: "16px" }}> |
| 96 | + {errors.map((error) => <div key={error} style={{ color: "#dc2626", fontSize: "14px" }}>{error}</div>)} |
| 97 | + </div> |
| 98 | + )} |
| 99 | + |
| 100 | + {!codeSent ? ( |
| 101 | + <form id="email-code-request-form" onSubmit={sendForm} style={{ display: "flex", flexDirection: "column", gap: "16px" }}> |
| 102 | + <TextField |
| 103 | + id="email-code-email" |
| 104 | + name="email" |
| 105 | + type="email" |
| 106 | + label={Locale.label("login.email")} |
| 107 | + value={email} |
| 108 | + onChange={(e) => setEmail(e.target.value)} |
| 109 | + autoFocus |
| 110 | + required |
| 111 | + autoComplete="email" |
| 112 | + fullWidth |
| 113 | + /> |
| 114 | + <Button id="email-code-send" type="submit" variant="contained" fullWidth disabled={busy} sx={buttonSx}> |
| 115 | + {busy ? Locale.label("common.pleaseWait") : Locale.label("login.sendCode")} |
| 116 | + </Button> |
| 117 | + <button type="button" onClick={props.loginCallback} style={linkStyle}>{Locale.label("login.usePassword")}</button> |
| 118 | + </form> |
| 119 | + ) : ( |
| 120 | + <form id="email-code-verify-form" onSubmit={codeForm} style={{ display: "flex", flexDirection: "column", gap: "16px" }}> |
| 121 | + <Typography variant="body2" sx={{ color: "#6b7280", fontSize: "14px" }}> |
| 122 | + <b>{email}</b> |
| 123 | + </Typography> |
| 124 | + <VerificationCodeInput value={code} onChange={setCode} onComplete={verify} disabled={busy} autoFocus /> |
| 125 | + <Button id="email-code-submit" type="submit" variant="contained" fullWidth disabled={busy || code.length !== 6} sx={buttonSx}> |
| 126 | + {busy ? Locale.label("common.pleaseWait") : Locale.label("login.signIn")} |
| 127 | + </Button> |
| 128 | + <div style={{ display: "flex", justifyContent: "center", alignItems: "center", gap: "8px" }}> |
| 129 | + <button type="button" disabled={resendCooldown > 0 || busy} onClick={requestCode} style={{ ...linkStyle, color: resendCooldown > 0 ? "#9ca3af" : "#3b82f6", cursor: resendCooldown > 0 ? "default" : "pointer" }}> |
| 130 | + {resendCooldown > 0 ? `${Locale.label("login.resendCode")} (${resendCooldown}s)` : Locale.label("login.resendCode")} |
| 131 | + </button> |
| 132 | + <span style={{ fontSize: "14px", color: "#6b7280" }}>|</span> |
| 133 | + <button type="button" onClick={props.loginCallback} style={linkStyle}>{Locale.label("login.usePassword")}</button> |
| 134 | + </div> |
| 135 | + </form> |
| 136 | + )} |
| 137 | + </CardContent> |
| 138 | + </Card> |
| 139 | + ); |
| 140 | +}; |
0 commit comments