summaryrefslogtreecommitdiff
path: root/website/src/scripts/contact/reset-resend-button.ts
blob: 21b0adc53e3b4a0bdead1d260fa5fe2c676b4c5b (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
30
31
32
import type { Selectors } from "./selectors";

export function resetResendButton(
  { otpDialog }: Selectors,
  resendButtonInterval: NodeJS.Timeout | undefined,
): Result {
  clearInterval(resendButtonInterval);

  const resendButton = otpDialog.resendButton();
  if (resendButton) {
    resendButton.setAttribute("data-countdown", "60");
    resendButton.setAttribute("disabled", "");

    resendButtonInterval = setInterval(() => {
      const countdown = +(resendButton.getAttribute("data-countdown") ?? 1) - 1;
      resendButton.setAttribute("data-countdown", countdown.toString());
      resendButton.textContent = `Resend (${countdown}s)`;
    }, 1000);

    setTimeout(() => {
      clearInterval(resendButtonInterval);
      resendButton.textContent = "Resend";
      resendButton.removeAttribute("disabled");
    }, 1000 * 60);
  }

  return { resendButtonInterval };
}

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