summaryrefslogtreecommitdiff
path: root/website/src/scripts/contact/resend-otp.ts
blob: 3f3a4f3402f8e54e98106acbdd1750f6065c15ba (plain)
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
import { actions } from "astro:actions";
import { resetResendButton } from "./reset-resend-button";
import { postErrorMessageOnOtpForm } from "./post-error-message";
import type { Selectors } from "./selectors";

const fallbackErrorMsg =
  "No can do. I'm afraid joeac.net is a bit broken right now - sorry about that.";

export async function resendOtp(
  selectors: Selectors,
  resetButtonInterval: NodeJS.Timeout | undefined,
): Promise<Result> {
  const result = resetResendButton(selectors, resetButtonInterval);
  const name = selectors.contactForm.nameElem()?.value;
  const email = selectors.contactForm.emailElem().value;

  const sendOtpResult = await actions.otp.send({ type: "email", name, email });
  if (sendOtpResult.error) {
    const errorMsg = sendOtpResult.error?.toString() ?? fallbackErrorMsg;
    postErrorMessageOnOtpForm(selectors, errorMsg);
    throw sendOtpResult.error;
  }

  return result;
}

type Result = {
  resendButtonInterval: NodeJS.Timeout | undefined;
};