Clear "already subscribed" newsletter error on next interaction
Matches standard form-validation behavior: any further edit to the email or consent checkbox after the already-subscribed message appears now dismisses it, instead of leaving it stuck until submit. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -27,15 +27,34 @@ export function useNewsletterSignup(source: NewsletterOptInSource) {
|
||||
const [error, setError] = useState("");
|
||||
const emailRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
// Clears a previous submit-time error (real failure or "already
|
||||
// subscribed") the moment the customer interacts with the form again —
|
||||
// same "stale validation message shouldn't linger" behavior
|
||||
// emailError already had for itself, extended to the submit-result
|
||||
// error too, since it's otherwise easy to misread as still describing
|
||||
// the current (possibly already-corrected) input.
|
||||
function clearSubmitError() {
|
||||
if (status === "error") {
|
||||
setStatus("idle");
|
||||
setError("");
|
||||
}
|
||||
}
|
||||
|
||||
function handleEmailChange(value: string) {
|
||||
setEmail(value);
|
||||
if (emailError) setEmailError("");
|
||||
clearSubmitError();
|
||||
}
|
||||
|
||||
function handleEmailBlur(value: string) {
|
||||
setEmailError(validateEmailFormat(value));
|
||||
}
|
||||
|
||||
function handleConsentChange(checked: boolean) {
|
||||
setConsent(checked);
|
||||
clearSubmitError();
|
||||
}
|
||||
|
||||
async function handleSubmit(e: FormEvent<HTMLFormElement>) {
|
||||
e.preventDefault();
|
||||
const formatError = validateEmailFormat(email);
|
||||
@@ -70,5 +89,5 @@ export function useNewsletterSignup(source: NewsletterOptInSource) {
|
||||
}
|
||||
}
|
||||
|
||||
return { email, emailError, consent, setConsent, status, error, successMessage: SUCCESS_MESSAGE, emailRef, handleEmailChange, handleEmailBlur, handleSubmit };
|
||||
return { email, emailError, consent, handleConsentChange, status, error, successMessage: SUCCESS_MESSAGE, emailRef, handleEmailChange, handleEmailBlur, handleSubmit };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user