-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.js
More file actions
101 lines (72 loc) · 2.44 KB
/
main.js
File metadata and controls
101 lines (72 loc) · 2.44 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
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
async function main() {
const user = await restoreSession();
document.getElementById('loading').setAttribute('hidden', '');
if (!user) {
document.getElementById('auth-guest').removeAttribute('hidden');
return;
}
document.getElementById('username').innerHTML = `<a href="${user.url}" target="_blank">${user.name}</a>`;
document.getElementById('auth-user').removeAttribute('hidden');
}
function login() {
const loginUrl = getLoginUrl();
if (!loginUrl)
return;
performLogin(loginUrl);
}
async function logout() {
document.getElementById('logout-button').setAttribute('disabled', '');
await performLogout();
document.getElementById('auth-guest').removeAttribute('hidden');
document.getElementById('auth-user').setAttribute('hidden', '');
document.getElementById('logout-button').removeAttribute('disabled');
}
async function createTask() {
const description = prompt('Task description');
if (!description)
return;
const task = await performTaskCreation(description);
if (!task) {
return;
}
appendTaskItem(task);
}
async function updateTask(taskUrl, button) {
const completed = button.innerText === 'Complete';
button.setAttribute('disabled', '');
await performTaskUpdate(taskUrl, completed);
button.removeAttribute('disabled');
button.innerText = completed ? 'Undo' : 'Complete';
}
async function deleteTask(taskUrl, taskElement, button) {
button.setAttribute('disabled', '');
await performTaskDeletion(taskUrl);
taskElement.remove();
}
function removeTaskObject(task) {
document.querySelector(`li[data-id="${ task.id }"]`).remove();
}
function appendTaskItem(task) {
const taskItem = document.createElement('li');
taskItem.dataset.id = task.id;
taskItem.innerHTML = `
<button
type="button"
onclick="deleteTask('${task.id}', this.parentElement, this)"
>
Delete
</button>
<button
type="button"
onclick="updateTask('${task.id}', this)"
style="width:100px"
>
${task.completed ? 'Undo' : 'Complete'}
</button>
<span>${task.description}</span>
`;
document.getElementById('tasks').appendChild(taskItem);
}
// ------------------------------------------------------------------
main();
window.onunhandledrejection = (error) => alert(`Error: ${error.reason?.message}`);