-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
86 lines (71 loc) · 2.21 KB
/
main.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
let form = document.getElementById('form');
let textInput = document.getElementById('textInput');
let msg = document.getElementById('msg');
let dateInput = document.getElementById('dateInput')
let textarea = document.getElementById('textarea')
let tasks = document.getElementById('tasks');
let add = document.getElementById('add');
form.addEventListener('submit', (e) => {
e.preventDefault();
formValidation();
})
let formValidation = () => {
if(textInput.value === '') {
msg.innerHTML = 'Task cannot be blank';
} else {
msg.innerHTML = '';
acceptData();
add.setAttribute('data-bs-dismiss', 'modal')
add.click();
(() => {
add.setAttribute('data-bs-dismiss', '')
})()
}
}
let data = [];
let acceptData = () => {
data.push({
text: textInput.value,
date: dateInput.value,
description: textarea.value,
});
localStorage.setItem('data', JSON.stringify(data));
createTasks();
}
let createTasks = () => {
tasks.innerHTML = '';
data.map((x,y) => {
return (tasks.innerHTML += `
<div id=${y}>
<span class="fw-bold">${x.text}</span>
<span class="small text-secondary">${x.date}</span>
<p>${x.description}</p>
<span class="options">
<i onClick="editTask(this)" data-bs-toggle="modal" data-bs-target="#form" class="fas fa-edit"></i>
<i onClick="deleteTask(this);createTasks()" class="fas fa-trash-alt"></i>
</span>
</div>`)
});
resetForm();
};
let deleteTask = (e) => {
e.parentElement.parentElement.remove();
data.splice(e.parentElement.parentElement.id, 1)
localStorage.setItem('data', JSON.stringify(data));
}
let editTask = (e) => {
let selectedTask = e.parentElement.parentElement;
textInput.value = selectedTask.children[0].innerHTML;
dateInput.value = selectedTask.children[1].innerHTML;
textarea.value = selectedTask.children[2].innerHTML;
deleteTask(e);
}
let resetForm = () => {
textInput.value = '';
dateInput.value = '';
textarea.value = '';
}
(() => {
data = JSON.parse(localStorage.getItem('data')) || [];
createTasks();
})()