summaryrefslogtreecommitdiff
path: root/http/public/scripts/contact/actions.js
blob: 5a7e32a90d1670edbfe5a29cdc4ad1a8f10ef80d (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export const actions = {
  /**
   * @param {{type: 'email', name: string, email: string}}
   * @returns {Promise<void>}
   */
  sendOtp: async ({ type, name, email }) => {
    const url = "/do/send_otp.php";
    const req = new Request(url, {
      method: "POST",
      body: JSON.stringify({ type, name, email }),
    });
    const res = await fetch(req);
    if (!res.ok) {
      throw new Error(
        `Request to ${url} failed: ${res.status} ${res.statusText} ${await res.text()}`,
      );
    }
  },

  /**
   * @param {{guess: string, lenient?: bool, userId: string}}
   * @returns {Promise<string | false>}
   */
  verifyOtp: async ({ guess, lenient, userId }) => {
    const url = "/do/verify_otp.php";
    const req = new Request(url, {
      method: "POST",
      body: JSON.stringify({ guess, lenient, userId }),
    });
    const res = await fetch(req);

    if (res.status === 400) {
      return false;
    } else if (!res.ok) {
      throw new Error(
        `Request to ${url} failed: ${res.status} ${res.statusText} ${await res.text()}`,
      );
    }

    return res.text();
  },

  /**
   * @param {{email: string, message: string, name: string, userId: string, token: string}}
   * @returns {Promise<void>}
   */
  sendEmail: async ({ email, message, name, userId, token }) => {
    const url = "/do/send_email.php";
    const req = new Request(url, {
      method: "POST",
      body: JSON.stringify({ email, message, name, userId, token }),
    });
    const res = await fetch(req);
    if (!res.ok) {
      throw new Error(
        `Request to ${url} failed: ${res.status} ${res.statusText} ${await res.text()}`,
      );
    }
  },
};