-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
72 lines (60 loc) · 2.61 KB
/
Copy pathapp.ts
File metadata and controls
72 lines (60 loc) · 2.61 KB
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
import { login, logout, getDataUrl } from "./example14Api.ts";
const usernameInput = document.getElementById("username") as HTMLInputElement;
const passwordInput = document.getElementById("password") as HTMLInputElement;
const loginBtn = document.getElementById("login-btn")!;
const logoutBtn = document.getElementById("logout-btn")!;
const result = document.getElementById("result")!;
const excelLink = document.getElementById("excel-link") as HTMLAnchorElement;
const loginSection = document.getElementById("login-section")!;
const uploadSection = document.getElementById("upload-section")!;
const authStatus = document.getElementById("auth-status")!;
function showLoggedIn(username: string) {
authStatus.innerHTML = "<p style=\"color: green;\">Logged in as <strong>" + username + "</strong></p>";
loginSection.classList.add("hidden");
uploadSection.classList.remove("hidden");
}
function showLoggedOut() {
authStatus.innerHTML = `<p style="color: gray;">Not authenticated</p>`;
loginSection.classList.remove("hidden");
uploadSection.classList.add("hidden");
}
// Check initial auth state
const user = (window as any).user as { userId: number | null; username: string } | undefined;
if (user?.userId != null && !isNaN(user.userId)) {
showLoggedIn(user.username);
} else {
showLoggedOut();
}
loginBtn.addEventListener("click", async () => {
const username = usernameInput.value;
const password = passwordInput.value;
const response = await login({ username, password });
if (response.status === 200) {
result.innerHTML = `<p style="color: green;">Login successful!</p><pre>${response.response}</pre>`;
showLoggedIn(username);
} else {
result.innerHTML = `<p style="color: red;">Status: ${response.status}</p>
<p style="color: red;">Error: ${JSON.stringify(response.error)}</p>`;
}
});
logoutBtn.addEventListener("click", async () => {
const response = await logout();
if (response.status === 200) {
result.innerHTML = `<p style="color: green;">Logged out successfully!</p>`;
showLoggedOut();
} else {
result.innerHTML = `<p style="color: red;">Status: ${response.status}</p>
<p style="color: red;">Error: ${JSON.stringify(response.error)}</p>`;
}
});
excelLink.addEventListener("click", (e) => {
e.preventDefault();
const dateStr = new Date().toISOString().slice(0, 19).replace(/[-:]/g, "");
const fileName = `data-${dateStr}.xlsx`;
const sheetName = `data-${dateStr}`;
document.location.href = getDataUrl({
format: "excel",
excelFileName: fileName,
excelSheet: sheetName
});
});