-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
208 lines (192 loc) · 5.81 KB
/
script.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const apiUrl = "http://localhost:8000"
function showRegister() {
document.getElementById("auth").style.display = "none"
document.getElementById("register").style.display = "block"
}
function showLogin() {
document.getElementById("auth").style.display = "block"
document.getElementById("register").style.display = "none"
}
function login() {
const username = document.getElementById("username").value
const password = document.getElementById("password").value
fetch(`${apiUrl}/login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username, password }),
})
.then((response) => {
if (!response.ok) {
return response.json().then((err) => {
throw err
})
}
return response.json()
})
.then((data) => {
if (data.access_token) {
localStorage.setItem("token", data.access_token)
document.getElementById("auth").style.display = "none"
document.getElementById("content").style.display = "block"
getTarefas()
} else {
alert("Login failed")
}
})
.catch((error) => console.error("Error:", error))
}
function register() {
const username = document.getElementById("reg_username").value
const password = document.getElementById("reg_password").value
fetch(`${apiUrl}/register`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username, password }),
})
.then((response) => {
if (!response.ok) {
return response.json().then((err) => {
throw err
})
}
return response.json()
})
.then((data) => {
if (data.message === "User created") {
// Limpar os campos de registro
document.getElementById("reg_username").value = ""
document.getElementById("reg_password").value = ""
alert("Usuário registrado com sucesso! Agora você pode fazer login.")
showLogin()
} else {
alert("Registration failed")
}
})
.catch((error) => console.error("Error:", error))
}
function getTarefas() {
fetch(`${apiUrl}/tarefas`, {
method: "GET",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
})
.then((response) => response.json())
.then((data) => {
const tarefasList = document.getElementById("tarefas-list")
const completedList = document.getElementById("completed-list")
tarefasList.innerHTML = ""
completedList.innerHTML = ""
data.forEach((tarefa) => {
const li = document.createElement("li")
li.textContent = `${tarefa.title} - ${tarefa.description} - ${tarefa.due_date}`
const deleteButton = document.createElement("button")
deleteButton.textContent = "Excluir"
deleteButton.onclick = () => deleteTarefa(tarefa.id)
if (tarefa.completed) {
li.style.textDecoration = "line-through"
li.style.color = "gray"
li.appendChild(deleteButton)
completedList.appendChild(li)
} else {
const completeButton = document.createElement("button")
completeButton.textContent = "Concluir"
completeButton.onclick = () => completeTarefa(tarefa.id)
li.appendChild(completeButton)
li.appendChild(deleteButton)
tarefasList.appendChild(li)
}
})
})
.catch((error) => console.error("Error:", error))
}
function addTarefa() {
const title = document.getElementById("tarefa_title").value
const description = document.getElementById("tarefa_description").value
const due_date = document.getElementById("tarefa_due_date").value
fetch(`${apiUrl}/tarefas`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ title, description, due_date }),
})
.then((response) => {
if (!response.ok) {
return response.json().then((err) => {
throw err
})
}
return response.json()
})
.then((data) => {
if (data.message === "Tarefa created") {
getTarefas()
// Limpar os campos de entrada
document.getElementById("tarefa_title").value = ""
document.getElementById("tarefa_description").value = ""
document.getElementById("tarefa_due_date").value = ""
} else {
alert("Failed to add tarefa")
}
})
.catch((error) => console.error("Error:", error))
}
function completeTarefa(tarefaId) {
fetch(`${apiUrl}/tarefas/${tarefaId}/complete`, {
method: "POST",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
})
.then((response) => {
if (!response.ok) {
return response.json().then((err) => {
throw err
})
}
return response.json()
})
.then((data) => {
if (data.message === "Tarefa completed") {
getTarefas()
} else {
alert("Failed to complete tarefa")
}
})
.catch((error) => console.error("Error:", error))
}
function deleteTarefa(tarefaId) {
fetch(`${apiUrl}/tarefas/${tarefaId}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
})
.then((response) => {
if (!response.ok) {
return response.json().then((err) => {
throw err
})
}
return response.json()
})
.then((data) => {
if (data.message === "Tarefa deleted") {
getTarefas()
} else {
alert("Failed to delete tarefa")
}
})
.catch((error) => console.error("Error:", error))
}
function logout() {
localStorage.removeItem("token")
document.getElementById("content").style.display = "none"
document.getElementById("auth").style.display = "block"
}