-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount.ts
117 lines (95 loc) · 2.95 KB
/
account.ts
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
109
110
111
112
113
114
115
116
117
import emitter from "../socket";
interface AccountEvent {
account_id: string;
}
interface VerifiedEvent extends AccountEvent {
verified: boolean;
}
interface LoadingStateEvent extends AccountEvent {
loading_state: string;
}
interface LoadingProgressEvent extends AccountEvent {
loaded: number;
total: number;
}
const NO_FORM_SITES = new Set(["Bluesky", "DeviantArt", "Patreon", "Twitter"]);
function getAccountSection(accountID: string): HTMLElement | null {
return document.querySelector(`section[data-account-id="${accountID}"]`);
}
emitter.on("account_verified", (payload) => {
const event = payload as VerifiedEvent;
if (!getAccountSection(event.account_id)) return;
if (event.verified) {
alert("Account verified!");
} else {
alert("Account could not be verified, please try again.");
}
window.location.reload();
});
emitter.on("loading_state_change", (payload) => {
const event = payload as LoadingStateEvent;
const loadingStateElement = getAccountSection(
event.account_id
)?.querySelector("#account-loading-state");
if (!loadingStateElement) return;
if (event.loading_state === "Loading Complete") {
window.location.reload();
} else {
loadingStateElement.textContent = event.loading_state;
}
});
emitter.on("loading_progress", (payload) => {
const event = payload as LoadingProgressEvent;
const progressElement = <HTMLProgressElement | null>(
getAccountSection(event.account_id)?.querySelector(
"#account-import-progress"
)
);
if (!progressElement) return;
progressElement.value = event.loaded;
progressElement.max = event.total;
});
const siteSelectInput = <HTMLSelectElement | null>(
document.getElementById("site-select")
);
const usernameInput = <HTMLInputElement | null>(
document.getElementById("username-input")
);
if (siteSelectInput && usernameInput) {
const updateSelection = (value: string) => {
usernameInput.style.display = NO_FORM_SITES.has(value) ? "none" : "block";
};
updateSelection(siteSelectInput.value);
siteSelectInput.addEventListener("change", () => {
updateSelection(siteSelectInput.value);
});
}
const verifyAccountButton = document.getElementById("verify-account");
if (verifyAccountButton) {
const accountID = document.querySelector<HTMLElement>(
"section[data-account-id]"
)?.dataset.accountId;
if (accountID) {
verifyAccountButton.addEventListener("click", (ev) => {
verifyAccountButton.classList.add("is-loading");
fetch("/user/account/verify", {
method: "POST",
body: new URLSearchParams({
account_id: accountID,
}),
credentials: "same-origin",
headers: {
"content-type": "application/x-www-form-urlencoded",
},
});
});
}
}
document.getElementById("delete-account")?.addEventListener("click", (ev) => {
const wasIntentional = confirm(
"Are you sure you want to remove this account?"
);
if (!wasIntentional) {
ev.preventDefault();
}
});