-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (139 loc) · 4.5 KB
/
index.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
let tasks = [];
let newTaskForm;
const tasksList = document.querySelector('#task-template').content;
let tasksContainer = document.querySelector('.tasks-container');
const priorityOptions = document.getElementsByName('priority');
const priorityLevels = {
'p1': '(a) Urgente',
'p2': '(b) Importante',
'p3': '(c) Deseable'
}
function greeting() {
const nameOwner = document.querySelector('#owner-name');
const helloUsername = document.querySelector('.username');
const username = localStorage.getItem('username') || '';
nameOwner.addEventListener('input', e => {
const newUsername = e.target.value.trim(); // Remove leading and trailing spaces
localStorage.setItem('username', newUsername);
if (newUsername !== '') {
helloUsername.textContent = newUsername + ": ";
} else {
helloUsername.textContent = '';
}
});
nameOwner.value = username;
if (username !== '') {
helloUsername.textContent = username + ": ";
} else {
helloUsername.textContent = '';
}
}
function taskPriority(priorityOptions) {
let selectedPriority = '';
for (let i = 0; i < priorityOptions.length; i++) {
if (priorityOptions[i].checked) {
selectedPriority = priorityOptions[i].id;
break;
}
}
return selectedPriority;
}
//Displays the value of the priority
function displayPriorityName(taskPriorityLevel) {
let result;
Object.entries(priorityLevels).forEach(([key, value]) => {
if (key == taskPriorityLevel) {
result = value;
}
});
return result;
}
//Displays the value of the priority in form
function taskForm(priorityOptions) {
const taskPriorityLevel = taskPriority(priorityOptions);
const taskPriorityName = displayPriorityName(taskPriorityLevel);
// Actualiza los radios y labels de prioridad
for (const key in priorityLevels) {
const label = document.querySelector(`label[for=${key}]`);
if (key === taskPriorityLevel) {
label.textContent = taskPriorityName;
} else {
label.textContent = priorityLevels[key];
}
}
}
function createNewTask(e) {
e.preventDefault();
const taskName = newTaskForm.elements['task-name'].value;
const taskPriorityID = taskPriority(priorityOptions);
//creating tasks array
const task = {
name: taskName,
priority: taskPriorityID,
done: false,
createdAt: new Date().getTime()
}
//adding a task to the array
tasks.push(task);
//saving the array localStorage
localStorage.setItem('tasks', JSON.stringify(tasks));
e.target.reset();
appendNewTask(task)
}
function createNewTaskCard(task) {
const taskCloneNode = tasksList.querySelector('.task').cloneNode(true);
const taskName = task.name;
const taskDone = task.done; // boolean
const taskPriorityLevel = task.priority;
const taskCheckbox = taskCloneNode.querySelector('.task-done');
const displayPriorityTask = displayPriorityName(taskPriorityLevel);
taskCloneNode.querySelector('.task-name').value = taskName;
taskCloneNode.querySelector('.task-priority').textContent = displayPriorityTask;
taskCloneNode.querySelector('.task-priority').classList.add(taskPriorityLevel);
if (taskDone) {
taskCheckbox.classList.add('done');
taskCheckbox.checked = true;
}
const deleteButton = taskCloneNode.querySelector('.delete');
if (deleteButton) { // Verifica si el botón de eliminar existe
deleteButton.addEventListener('click', () => deleteTask(task, taskCloneNode));
}
taskCheckbox.addEventListener('change', (e) => {
task.done = e.target.checked;
localStorage.setItem('tasks', JSON.stringify(tasks));
if (task.done) {
taskCheckbox.classList.add('done');
} else {
taskCheckbox.classList.remove('done');
}
})
tasksContainer.append(taskCloneNode);
}
function appendNewTask(task) {
createNewTaskCard(task);
}
function displayTasks() {
// verify if there are tasks in array 'tasks'
if (tasks.length > 0) {
tasks.map(task => {
createNewTaskCard(task);
})
} else {
console.log('El array de tareas está vacío.');
}
}
function deleteTask(taskToDelete, taskElement) {
// Elimina la tarea específica del array 'tasks'
tasks = tasks.filter(task => task !== taskToDelete);
localStorage.setItem('tasks', JSON.stringify(tasks));
// Elimina la tarea del DOM
taskElement.remove();
}
window.addEventListener('load', () => {
tasks = JSON.parse(localStorage.getItem('tasks')) || tasks;
newTaskForm = document.querySelector('#tasks-form');
newTaskForm.addEventListener('submit', createNewTask)
greeting();
taskForm(priorityOptions);
displayTasks()
})