diff --git a/static/js/NewsletterSignUpForm.jsx b/static/js/NewsletterSignUpForm.jsx index a067b97a9d..322f83b8c4 100644 --- a/static/js/NewsletterSignUpForm.jsx +++ b/static/js/NewsletterSignUpForm.jsx @@ -15,15 +15,26 @@ export function NewsletterSignUpForm({ const [educatorCheck, setEducatorCheck] = useState(false); const [subscribeMessage, setSubscribeMessage] = useState(null); const [showNameInputs, setShowNameInputs] = useState(false); + const [isSubscribed, setIsSubscribed] = useState(false); + const [isSubmitting, setIsSubmitting] = useState(false); + + const isFormDisabled = isSubscribed || isSubmitting; function handleSubscribe() { + if (isFormDisabled) { + return; + } if (showNameInputs === true) { // submit if (firstName.length > 0 && lastName.length > 0) { setSubscribeMessage("Subscribing..."); + setIsSubmitting(true); subscribe(firstName, lastName, email, educatorCheck, additionalNewsletterMailingLists).then(res => { + setIsSubscribed(true); + setIsSubmitting(false); setSubscribeMessage("Subscribed! Welcome to our list."); Sefaria.track.event("Newsletter", "Subscribe from " + contextName, ""); }).catch(error => { + setIsSubmitting(false); setSubscribeMessage(error?.message || "Sorry, there was an error."); setShowNameInputs(false); }); @@ -47,6 +58,7 @@ export function NewsletterSignUpForm({ aria-label={Sefaria._("Email address")} type="email" value={email} + disabled={isFormDisabled} onChange={e => setEmail(e.target.value)} onKeyUp={(e) => Util.handleEnterKey(e, handleSubscribe)}/> @@ -57,10 +69,11 @@ export function NewsletterSignUpForm({ aria-label="כתובת אימייל" type="email" value={email} + disabled={isFormDisabled} onChange={e => setEmail(e.target.value)} onKeyUp={(e) => Util.handleEnterKey(e, handleSubscribe)}/> - {!showNameInputs ? {Sefaria._("Submit")} : null} + {!showNameInputs && !isFormDisabled ? {Sefaria._("Submit")} : null} {showNameInputs ? <> setFirstName(e.target.value)} onKeyUp={(e) => Util.handleEnterKey(e, handleSubscribe)}/> @@ -80,6 +94,7 @@ export function NewsletterSignUpForm({ aria-label="שם פרטי" type="text" value={firstName} + disabled={isFormDisabled} onChange={e => setFirstName(e.target.value)} onKeyUp={(e) => Util.handleEnterKey(e, handleSubscribe)}/> @@ -90,6 +105,7 @@ export function NewsletterSignUpForm({ aria-label={Sefaria._("Last Name")} type="text" value={lastName} + disabled={isFormDisabled} onChange={e => setLastName(e.target.value)} onKeyUp={(e) => Util.handleEnterKey(e, handleSubscribe)}/> @@ -100,12 +116,13 @@ export function NewsletterSignUpForm({ aria-label="שם משפחה" type="text" value={lastName} + disabled={isFormDisabled} onChange={e => setLastName(e.target.value)} onKeyUp={(e) => Util.handleEnterKey(e, handleSubscribe)}/> {includeEducatorOption ? - : null} - {Sefaria._("Submit")} + : null} + {!isFormDisabled && {Sefaria._("Submit")}} : null} {subscribeMessage ? @@ -116,7 +133,7 @@ export function NewsletterSignUpForm({ } -const EducatorCheckbox = ({educatorCheck, setEducatorCheck}) => { +const EducatorCheckbox = ({educatorCheck, setEducatorCheck, disabled}) => { return (
@@ -125,6 +142,7 @@ const EducatorCheckbox = ({educatorCheck, setEducatorCheck}) => { className="educatorNewsletterInput" id="educator-check-en" checked={educatorCheck} + disabled={disabled} onChange={e => setEducatorCheck(!!e.target.checked)}/> @@ -134,6 +152,7 @@ const EducatorCheckbox = ({educatorCheck, setEducatorCheck}) => { className="educatorNewsletterInput" id="educator-check-he" checked={educatorCheck} + disabled={disabled} onChange={e => setEducatorCheck(!!e.target.checked)}/>