This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
116 lines (89 loc) · 2.54 KB
/
app.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
import { Backend } from "@staticbackend/js";
const bkn = new Backend("pub-key", "dev");
// current user's session token
let token = null;
// main div
let noAuth = document.getElementById("no-auth");
let todos = document.getElementById("todos");
// register/login buttons
let btnReg = document.getElementById("register");
let btnLogin = document.getElementById("login");
let authForm = document.forms["auth"];
// prevent page refresh
authForm.addEventListener("submit", (e) => e.preventDefault());
// register/login click event handlers
btnReg.addEventListener("click", async (e) => {
const result = await bkn.register(authForm.email.value, authForm.password.value);
if (!result.ok) {
console.error(result.content);
return;
}
token = result.content;
toggleAuth(true);
});
btnLogin.addEventListener("click", async (e) => {
const result = await bkn.login(authForm.email.value, authForm.password.value);
if (!result.ok) {
console.error(result.content);
return;
}
token = result.content;
await loadTodos();
toggleAuth(true);
});
const toggleAuth = (state) => {
if (state) {
noAuth.style.display = "none";
todos.style.display = "block";
} else {
noAuth.style.display = "block";
todos.style.display = "none";
}
}
// we show the register/login div by default
toggleAuth(false);
// add to-do form
const addForm = document.forms["todo"];
addForm.addEventListener("submit", async (e) => {
e.preventDefault();
const doc = {
name: addForm.name.value,
done: false
}
const result = await bkn.create(token, "todos", doc);
if (!result.ok) {
console.error(result.content);
return;
}
appendItem(result.content);
addForm.name.value = "";
addForm.focus();
});
// the to-do container
let items = document.getElementById("items");
const appendItem = (todo) => {
console.log(todo);
const li = document.createElement("li");
li.innerText = todo.name;
li.dataset["done"] = todo.done;
li.style.textDecoration = todo.done ? "line-through" : "none";
li.addEventListener("click", async (e) => {
const update = { name: todo.name, done: li.dataset["done"] == "false" };
const result = await bkn.update(token, "todos", todo.id, update);
if (!result.ok) {
console.error(result.content);
return;
}
li.style.textDecoration = update.done ? "line-through" : "none";
li.dataset["done"] = update.done ? "true" : "false";
});
items.appendChild(li);
}
const loadTodos = async () => {
const result = await bkn.list(token, "todos");
if (!result.ok) {
console.error(result.content);
return;
}
result.content.results.forEach(appendItem);
}