Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 1x 148x 148x 148x 148x 148x 148x 148x 148x 4x 4x 148x 148x 148x 148x 148x 148x 40x 1195x 31x 148x 44x 44x 44x 148x 7x 6x 6x 6x 6x 3x 3x 6x 3x 3x 3x 3x 148x 6x 6x 6x 6x 6x 5x 5x 5x 1x 148x | import { useEffect, useState } from "react";
import NiceModal from "@ebay/nice-modal-react";
import GiveButton from "@shared/Button/GiveButton";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import { OTPVerification } from "@features/common/OTP";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { VERIFY_PURCHASE } from "@sections/PayBuilder/constants";
export type VerifyPurchaseFailure = {
message: string;
needsNewCode: boolean;
};
type VerifyPurchaseModalProps = {
email: string;
/**
* Submits the code. Resolves null once the purchase is confirmed — the modal
* closes itself — or with the refusal to show, staying open so the buyer can
* correct it. needsNewCode means this code is dead however many times it is
* retyped, so the resend is offered at once.
*/
onVerify: (code: string) => Promise<VerifyPurchaseFailure | null>;
/** Rejects when no mail went out, so the cooldown is not restarted for it. */
/** Resolves null once mailed, or with the refusal to show beside the link. */
onResend: () => Promise<string | null>;
/** Called whenever the modal leaves, so the caller can drop what it staged. */
onDismiss?: () => void;
};
export const VerifyPurchaseModal = NiceModal.create(
({ email, onVerify, onResend, onDismiss }: VerifyPurchaseModalProps) => {
const [otp, setOtp] = useState("");
const [timer, setTimer] = useState(VERIFY_PURCHASE.RESEND_SECONDS);
const [error, setError] = useState<string | null>(null);
const [isVerifying, setIsVerifying] = useState(false);
const [isResending, setIsResending] = useState(false);
const [resendError, setResendError] = useState<string | null>(null);
const { open, onClose: hide, TransitionProps } = useNiceModal();
// Escape and the header X reach this too, not just Cancel.
const onClose = () => {
Iif (isVerifying) return;
hide();
};
// Every removal path, so what the caller staged is not left behind.
useEffect(() => () => onDismiss?.(), [onDismiss]);
const { palette } = useAppTheme();
const { isMobileView } = useCustomThemeV2();
// Boolean(error): resubmitting the refused code spends another attempt.
const [descriptionBefore, descriptionAfter] =
VERIFY_PURCHASE.DESCRIPTION_TEMPLATE.split(
VERIFY_PURCHASE.EMAIL_PLACEHOLDER,
);
const isSubmitDisabled =
otp.length < VERIFY_PURCHASE.CODE_LENGTH || isVerifying || Boolean(error);
useEffect(() => {
if (timer === 0) return;
const interval = setInterval(() => setTimer((prev) => prev - 1), 1000);
return () => clearInterval(interval);
}, [timer]);
// Clearing on edit: the buyer is correcting the code the message is about,
// so leaving it up would describe what they are no longer looking at.
const handleChange = (value: string) => {
setOtp(value);
if (error) setError(null);
if (resendError) setResendError(null);
};
const handleResend = async () => {
// The link stays up while the send is in flight; a second click spends
// another send and earns a cooldown refusal.
if (isResending) return;
setIsResending(true);
try {
const message = await onResend();
if (message) {
// Not setError: that slot reddens the code boxes.
setResendError(message);
return;
}
} catch {
setResendError(VERIFY_PURCHASE.RESEND_FAILED);
return;
} finally {
setIsResending(false);
}
setTimer(VERIFY_PURCHASE.RESEND_SECONDS);
setOtp("");
setError(null);
setResendError(null);
};
const handleConfirm = async () => {
setIsVerifying(true);
let failure: VerifyPurchaseFailure | null;
try {
failure = await onVerify(otp);
} catch {
// Without this the modal is wedged open with every control disabled.
setError(VERIFY_PURCHASE.GENERIC_INVALID_CODE);
return;
} finally {
setIsVerifying(false);
}
if (failure) {
setError(failure.message);
// The API's cooldown counts spent sends too, so it is not shortened.
if (failure.needsNewCode) setOtp("");
return;
}
onClose();
};
return (
<GiveBaseModal
open={open}
onClose={onClose}
// The code is single-use, so a stray click beside the modal costs
// the buyer a resend to get back to where they were.
disableBackdropClick
// Hiding leaves the entry and its callbacks in the modal store.
TransitionProps={TransitionProps}
title={VERIFY_PURCHASE.TITLE}
width={isMobileView ? "100%" : VERIFY_PURCHASE.MODAL_WIDTH}
height="fit-content"
buttons={
<>
<GiveButton
variant="ghost"
size="large"
label={VERIFY_PURCHASE.CANCEL_LABEL}
onClick={onClose}
disabled={isVerifying}
/>
<GiveButton
size="large"
variant="filled"
label={VERIFY_PURCHASE.VERIFY_LABEL}
onClick={handleConfirm}
data-testid="submit-action-button"
disabled={isSubmitDisabled}
sx={{ border: "none" }}
/>
</>
}
>
<OTPVerification
email={email}
otp={otp}
setOtp={handleChange}
error={error}
timer={timer}
onResend={handleResend}
showLogo
numInputs={VERIFY_PURCHASE.CODE_LENGTH}
timerFormat="clock"
resendPrompt={VERIFY_PURCHASE.RESEND_PROMPT}
countdownPrefix={VERIFY_PURCHASE.COUNTDOWN_PREFIX}
resendLabel={VERIFY_PURCHASE.RESEND_LABEL}
timerSx={{ paddingBottom: "60px" }}
description={
<GiveText variant="bodyS" color="secondary" textAlign="center">
{descriptionBefore}
<span style={{ color: palette.text.primary }}>{email}</span>
{descriptionAfter}
</GiveText>
}
/>
{resendError && (
<GiveText
variant="bodyS"
color="error"
textAlign="center"
role="alert"
data-testid="resend-error"
>
{resendError}
</GiveText>
)}
</GiveBaseModal>
);
},
);
|