-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
108 lines (94 loc) · 3.03 KB
/
app.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(function () {
"use strict";
//@ts-ignore
document
.querySelector("[name=signup]")
.addEventListener("submit", function (event) {
/** @type {HTMLFormElement} */
//@ts-ignore
let target = event.target;
event.preventDefault();
document.querySelector("button")?.setAttribute("disabled", "disabled");
document.querySelector(".signup-msg")?.remove();
/** @type {HTMLFormElement} */ //@ts-expect-error
let $email = document.querySelector("[type='email']");
let email = $email.value || "";
email = email.trim();
email = email.toLowerCase();
/** @type {HTMLFormElement} */ //@ts-expect-error
let $csrfToken = document.querySelector("[name='csrf-token']");
let csrfToken = $csrfToken.value || "";
let host = location.host || "localhost";
if ("localhost" !== host) {
host = `api.${host}`;
}
let url = `https://${host}/api/request-invite`;
let request = { email: email, "csrf-token": csrfToken };
let body = JSON.stringify(request, null, 2);
let p;
let yes = window.confirm(
`Thanks, we'll send an automated message to confirm your email address.`,
);
if (yes) {
p = window
.fetch(url, {
method: "POST",
mode: "cors",
headers: { "Content-Type": "application/json" },
body,
})
.then(mustOk);
} else {
p = Promise.resolve();
}
p.then(showSuccess).catch(showFailure);
/**
* @param {string} msg
* @param {string} [cls]
*/
function msgHtml(msg, cls = "") {
return `<h3 class="${["signup-msg", cls].join(" ")}">${msg}</h3>`;
}
async function showSuccess() {
//let msg = `You're good to go and should receive a confirmation email shortly.`;
let msg = `You're good to go. We'll reach out shortly.`;
target.querySelector("fieldset")?.setAttribute("disabled", "disabled");
target.insertAdjacentHTML("beforeend", msgHtml(msg, "good"));
}
/** @param {Error} err */
async function showFailure(err) {
console.error(`Failed to send request for invite`);
console.error(err);
let email = "bnnadotnet@therootcompany.com";
let msg = `Whoops, looks like something went wrong.<br/>
Email <a href="mailto:${email}">${email}</a> instead.`;
target.insertAdjacentHTML("beforeend", msgHtml(msg, "warn"));
}
});
/** @param {Response} resp */
async function mustOk(resp) {
let body = await resp.text();
let result;
try {
result = JSON.parse(body);
} catch (e) {
//@ts-ignore
e.code = "E_PARSE";
//@ts-ignore
e.body = body;
//@ts-ignore
e.response = resp;
throw e;
}
if (!resp.ok) {
let err = new Error(`did not get 200 OK`);
//@ts-ignore
err.code = "E_NOT_OK";
//@ts-ignore
err.result = result;
//@ts-ignore
err.response = resp;
throw err;
}
}
})();