diff options
| author | Joe Carstairs <me@joeac.net> | 2026-06-04 15:15:02 +0100 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2026-06-04 15:16:34 +0100 |
| commit | 813836fbba884388e6013e9fd377430ef33b6c8d (patch) | |
| tree | 056149f5c51eabaa89960a7d8d2eb68fe9b158e8 /http/public/scripts/contact/submit-otp-form.js | |
| parent | fac1199cd61e069b1a144d2548477a2c3b3b94b0 (diff) | |
http: adds scripts
Diffstat (limited to 'http/public/scripts/contact/submit-otp-form.js')
| -rw-r--r-- | http/public/scripts/contact/submit-otp-form.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/http/public/scripts/contact/submit-otp-form.js b/http/public/scripts/contact/submit-otp-form.js new file mode 100644 index 0000000..8c09314 --- /dev/null +++ b/http/public/scripts/contact/submit-otp-form.js @@ -0,0 +1,77 @@ +import { actions } from "./actions.js"; +import { postErrorMessageOnOtpForm } from "./post-error-message.js"; + +/** @typedef {import("./selectors.js").Selectors} Selectors */ + +const fallbackErrorMsg = + "No can do. I'm afraid joeac.net is a bit broken right now - sorry about that."; + +/** + * @param {Selectors} selectors + */ +export async function submitOtpForm(selectors) { + const { contactForm, otpDialog, successSection } = selectors; + const otpForm = otpDialog.otpForm(); + otpDialog.submitButton()?.setAttribute("disabled", ""); + + const otpFormData = new FormData(otpForm ?? undefined); + const guess = [ + otpFormData.get("1"), + otpFormData.get("2"), + otpFormData.get("3"), + otpFormData.get("4"), + otpFormData.get("5"), + otpFormData.get("6"), + ].join(""); + + const name = contactForm.nameElem()?.value; + const email = contactForm.emailElem().value; + const message = contactForm.messageElem()?.value; + + let verifyResult; + try { + verifyResult = await actions.verifyOtp({ guess, userId: email }); + } catch (verifyError) { + otpDialog.submitButton()?.removeAttribute("disabled"); + postErrorMessageOnOtpForm( + selectors, + verifyError?.toString() ?? fallbackErrorMsg, + ); + return; + } + + if (verifyResult === false) { + otpDialog.submitButton()?.removeAttribute("disabled"); + postErrorMessageOnOtpForm(selectors, "Incorrect OTP. Check your email?"); + otpDialog.firstOtpInput()?.focus(); + return; + } + const sendmailToken = verifyResult; + + try { + await actions.sendEmail({ + email, + message: message, + name: name, + userId: email, + token: sendmailToken, + }); + } catch (sendEmailError) { + const errorMsg = sendEmailError?.toString() ?? fallbackErrorMsg; + postErrorMessageOnOtpForm(selectors, errorMsg); + otpDialog.submitButton()?.removeAttribute("disabled"); + return; + } + + const sentName = successSection.name(); + const sentEmail = successSection.email(); + const sentMessage = successSection.message(); + sentName && (sentName.textContent = name ?? "???"); + sentEmail && (sentEmail.textContent = email ?? "???"); + sentMessage && (sentMessage.textContent = message ?? "???"); + + contactForm.self()?.remove(); + successSection.self()?.removeAttribute("hidden"); + otpDialog.submitButton()?.removeAttribute("disabled"); + otpDialog.self().close(); +} |
