-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
127 lines (102 loc) · 3.9 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
document.addEventListener('DOMContentLoaded', (event) => {
loadTasks();
});
function loadTasks() {
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
const tableBody = document.getElementById('taskTableBody');
tasks.forEach(task => {
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td>${task.title}</td>
<td>${task.description}</td>
<td>${task.date}</td>
<td>
<select>
<option value="1" ${task.status === '1' ? 'selected' : ''}>Concluída</option>
<option value="2" ${task.status === '2' ? 'selected' : ''}>Em andamento</option>
<option value="3" ${task.status === '3' ? 'selected' : ''}>Pendente</option>
</select>
</td>
<td>
<button class="btn" title="Clique aqui para editar a tarefa" onclick="editTask(this)">
<img src="imagens/botao-editar.png">
</button>
<button class="btn" title="Clique aqui para excluir a tarefa" onclick="deleteTask(this)">
<img src="imagens/excluir.png">
</button>
</td>
`;
tableBody.appendChild(newRow);
});
}
function newTask() {
const titulo = document.getElementById('titulo').value;
const descricao = document.getElementById('descricao').value;
if (titulo === '' || descricao === '') {
alert('Por favor, preencha todos os campos.');
return;
}
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
// Verificação de título existente
const taskExists = tasks.some(task => task.title === titulo);
if (taskExists) {
alert('Uma tarefa com este título já existe.');
return;
}
const date = new Date();
const formattedDate = date.toLocaleDateString('pt-BR');
const newTask = {
title: titulo,
description: descricao,
date: formattedDate,
status: '3' // Pendente
};
tasks.push(newTask);
localStorage.setItem('tasks', JSON.stringify(tasks));
const tableBody = document.getElementById('taskTableBody');
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td>${titulo}</td>
<td>${descricao}</td>
<td>${formattedDate}</td>
<td>
<select>
<option value="1">Concluída</option>
<option value="2">Em andamento</option>
<option value="3" selected>Pendente</option>
</select>
</td>
<td>
<button class="btn" title="Clique aqui para editar a tarefa" onclick="editTask(this)">
<img src="img/botao-editar.png">
</button>
<button class="btn" title="Clique aqui para excluir a tarefa" onclick="deleteTask(this)">
<img src="img/excluir.png">
</button>
</td>
`;
tableBody.appendChild(newRow);
document.getElementById('titulo').value = '';
document.getElementById('descricao').value = '';
}
function deleteTask(button) {
const row = button.parentNode.parentNode;
const title = row.cells[0].innerText;
try {
let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
tasks = tasks.filter(task => task.title.toLowerCase() !== title.toLowerCase());
localStorage.setItem('tasks', JSON.stringify(tasks));
row.parentNode.removeChild(row);
} catch (error) {
console.error('Error removing task:', error);
}
}
function editTask(button) {
const row = button.parentNode.parentNode;
const cells = row.getElementsByTagName('td');
const titulo = cells[0].innerText;
const descricao = cells[1].innerText;
document.getElementById('titulo').value = titulo;
document.getElementById('descricao').value = descricao;
deleteTask(button);
}