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-contact-form.js | |
| parent | fac1199cd61e069b1a144d2548477a2c3b3b94b0 (diff) | |
http: adds scripts
Diffstat (limited to 'http/public/scripts/contact/submit-contact-form.js')
| -rw-r--r-- | http/public/scripts/contact/submit-contact-form.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/http/public/scripts/contact/submit-contact-form.js b/http/public/scripts/contact/submit-contact-form.js new file mode 100644 index 0000000..a90f420 --- /dev/null +++ b/http/public/scripts/contact/submit-contact-form.js @@ -0,0 +1,53 @@ +import { actions } from "./actions.js"; +import { postErrorMessageOnContactForm } from "./post-error-message.js"; +import { resetResendButton } from "./reset-resend-button.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 + * @param {NodeJS.Timeout | undefined} resetButtonInterval + * @returns {Promise<Result>} + */ +export async function submitContactForm(selectors, resendButtonInterval) { + const { contactForm, otpDialog } = selectors; + + const name = contactForm.nameElem()?.value; + const email = contactForm.emailElem().value; + + contactForm.submitButton()?.setAttribute("disabled", ""); + try { + await actions.sendOtp({ type: "email", name, email }); + } catch (sendOtpError) { + const errorMsg = sendOtpError?.toString() ?? fallbackErrorMsg; + postErrorMessageOnContactForm(selectors, errorMsg); + throw sendOtpError; + } + + const otpRecipient = otpDialog.otpRecipient(); + email && otpRecipient && (otpRecipient.textContent = `<${email}>`); + const otpValidUntil = otpDialog.otpValidUntil(); + const validUntil = new Date(Date.now() + 1000 * 60 * 5); + otpValidUntil && + (otpValidUntil.textContent = `until ${validUntil.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}`); + + const dialog = otpDialog.self(); + dialog.showModal(); + contactForm.submitButton()?.removeAttribute("disabled"); + dialog.addEventListener("click", function (event) { + const rect = dialog.getBoundingClientRect(); + const isInDialog = + rect.top <= event.clientY && + event.clientY <= rect.top + rect.height && + rect.left <= event.clientX && + event.clientX <= rect.left + rect.width; + if (!isInDialog) { + dialog.close(); + } + }); + + return resetResendButton(selectors, resendButtonInterval); +} |
