-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (62 loc) · 2.29 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
// Inicialização do SQL.js e do banco de dados
let db;
initSqlJs().then((SQL) => {
db = new SQL.Database();
initializeDatabase();
renderTasks();
});
function initializeDatabase() {
db.run(`CREATE TABLE IF NOT EXISTS tarefas (
id INTEGER PRIMARY KEY,
descricao TEXT,
status BOOLEAN,
data_criacao TEXT
);`);
}
// Função para adicionar uma nova tarefa
function addTask(description) {
db.run(`INSERT INTO tarefas (descricao, status, data_criacao) VALUES (?, 0, datetime('now'));`, [description]);
renderTasks();
}
// Função para obter todas as tarefas e renderizar na tela
function renderTasks() {
const result = db.exec("SELECT * FROM tarefas;");
const taskList = document.getElementById("taskList");
taskList.innerHTML = "";
if (result.length > 0) {
const rows = result[0].values;
rows.forEach(row => {
const [id, descricao, status, data_criacao] = row;
const li = document.createElement("li");
li.textContent = `${descricao} - ${status ? "Concluída" : "Pendente"} (Criada em: ${data_criacao})`;
taskList.appendChild(li);
// Botão para marcar como concluída
const completeButton = document.createElement("button");
completeButton.textContent = "Completar";
completeButton.onclick = () => completeTask(id);
li.appendChild(completeButton);
// Botão para deletar
const deleteButton = document.createElement("button");
deleteButton.textContent = "Deletar";
deleteButton.onclick = () => deleteTask(id);
li.appendChild(deleteButton);
});
}
}
// Função para marcar uma tarefa como concluída
function completeTask(id) {
db.run("UPDATE tarefas SET status = 1 WHERE id = ?;", [id]);
renderTasks();
}
// Função para deletar uma tarefa
function deleteTask(id) {
db.run("DELETE FROM tarefas WHERE id = ?;", [id]);
renderTasks();
}
// Captura do evento de submissão do formulário
document.getElementById("taskForm").addEventListener("submit", function(event) {
event.preventDefault();
const description = document.getElementById("taskDescription").value;
addTask(description);
document.getElementById("taskDescription").value = "";
});