-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
48 lines (42 loc) · 1.86 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
const localStorageName = 'to-do-list-gn';
function validateIfExistsNewTask() {
let values = JSON.parse(localStorage.getItem(localStorageName) || "[]");
let inputValue = document.getElementById('input-new-task').value;
let exists = values.find(x => x.name === inputValue);
return exists ? true : false;
}
function newTask() {
let input = document.getElementById('input-new-task');
// Validação
if (!input.value) {
input.style.border = '1px solid red';
alert('Digite algo para inserir em sua lista');
} else if (validateIfExistsNewTask()) {
alert('Já existe uma task com essa descrição');
} else {
// Incrementar ao localStorage
let values = JSON.parse(localStorage.getItem(localStorageName) || "[]");
values.push({
name: input.value
});
localStorage.setItem(localStorageName, JSON.stringify(values));
showValues();
}
input.value = '';
}
function showValues() {
let values = JSON.parse(localStorage.getItem(localStorageName) || "[]");
let list = document.getElementById('to-do-list');
list.innerHTML = '';
for (let i = 0; i < values.length; i++) {
list.innerHTML += `<li>${values[i]['name']}<button id='btn-ok' onclick='removeItem("${values[i]['name']}")'><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check2" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg></button></li>`;
}
}
function removeItem(data) {
let values = JSON.parse(localStorage.getItem(localStorageName) || "[]");
let index = values.findIndex(x => x.name === data);
values.splice(index, 1);
localStorage.setItem(localStorageName, JSON.stringify(values));
showValues();
}
showValues();