summaryrefslogtreecommitdiff
path: root/http/public/scripts/contact/reset-resend-button.js
blob: 47bc9ac9b700b4176a020a45865efe8571d08353 (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
/** @typedef {import("./selectors.js").Selectors} Selectors */

/** @typedef {{ resendButtonInterval: NodeJS.Timeout | undefined }} Result */

/**
 * @param {Selectors} selectors
 * @param {NodeJS.Timeout | undefined} resetButtonInterval
 * @returns {Result}
 */
export function resetResendButton({ otpDialog }, resendButtonInterval) {
  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 };
}