ref: master
js/tasks.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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
document.addEventListener("DOMContentLoaded", () => { const taskForm = document.getElementById("task-form"); const taskList = document.querySelector("#task-list ul"); const priorityFilter = document.getElementById("priority-filter"); const taskSummary = document.getElementById("task-summary"); const toggleErrorMessage = (field, condition, message) => { const errorMessage = field.nextElementSibling; // Mensagem de erro associada ao campo if (!condition) { errorMessage.textContent = message; errorMessage.classList.add("visible"); field.classList.add("error"); } else { errorMessage.textContent = ""; errorMessage.classList.remove("visible"); field.classList.remove("error"); } }; const validateTitle = (field) => { toggleErrorMessage(field, field.value.trim() !== "", "O título é obrigatório."); }; const validateDescription = (field) => { toggleErrorMessage(field, field.value.trim() !== "", "A descrição é obrigatória."); }; const validateDeadline = (field) => { const deadlineDate = new Date(field.value); const today = new Date(); toggleErrorMessage( field, field.value !== "" && deadlineDate > today, "O prazo deve ser uma data válida no futuro." ); }; const validatePriority = (field) => { toggleErrorMessage(field, field.value !== "", "Por favor, selecione uma prioridade."); }; const validateForm = () => { const title = document.getElementById("task-title"); const description = document.getElementById("task-desc"); const deadline = document.getElementById("task-deadline"); const priority = document.getElementById("task-priority"); validateTitle(title); validateDescription(description); validateDeadline(deadline); validatePriority(priority); const errors = taskForm.querySelectorAll(".error-message.visible"); return errors.length === 0; }; // Tasks stuff // TODO: Move this shit into somewhere else? const loadTasks = () => { const tasks = JSON.parse(localStorage.getItem("tasks")) || []; renderTasks(tasks); updateTaskSummary(tasks); }; const saveTasks = (tasks) => { localStorage.setItem("tasks", JSON.stringify(tasks)); updateTaskSummary(tasks); }; const updateTaskSummary = (tasks) => { const summary = tasks.reduce((acc, task) => { acc[task.priority] = (acc[task.priority] || 0) + 1; return acc; }, {}); taskSummary.innerHTML = ` <p>Prioridade Alta: ${summary["alta"] || 0}</p> <p>Prioridade Média: ${summary["media"] || 0}</p> <p>Prioridade Baixa: ${summary["baixa"] || 0}</p> `; }; const renderTasks = (tasks) => { taskList.innerHTML = ""; tasks.map((task) => { const taskItem = document.createElement("li"); taskItem.innerHTML = ` <strong>${task.title}</strong> <p>${task.description}</p> <p>Prazo: ${task.deadline}</p> <p>Prioridade: ${task.priority}</p> <button class="remove-task">Remover</button> `; taskList.appendChild(taskItem); }); }; const addTask = (task) => { const tasks = JSON.parse(localStorage.getItem("tasks")) || []; tasks.push(task); saveTasks(tasks); renderTasks(tasks); }; const removeTask = (index) => { const tasks = JSON.parse(localStorage.getItem("tasks")) || []; tasks.splice(index, 1); saveTasks(tasks); renderTasks(tasks); updateTaskSummary(tasks); }; const filterTasksByPriority = (priority) => { const tasks = JSON.parse(localStorage.getItem("tasks")) || []; const filteredTasks = priority === "Todas" ? tasks : tasks.filter((task) => task.priority === priority); renderTasks(filteredTasks); }; taskForm.addEventListener("submit", (e) => { e.preventDefault(); if (validateForm()) { const task = { title: document.getElementById("task-title").value.trim(), description: document.getElementById("task-desc").value.trim(), deadline: document.getElementById("task-deadline").value, priority: document.getElementById("task-priority").value, }; addTask(task); taskForm.reset(); } }); taskList.addEventListener("click", (e) => { if (e.target.classList.contains("remove-task")) { const index = Array.from(taskList.children).indexOf(e.target.parentElement); removeTask(index); } }); priorityFilter.addEventListener("change", (e) => { filterTasksByPriority(e.target.value); }); const resetForm = () => { taskForm.reset(); }; taskForm.addEventListener("input", (e) => { const field = e.target; switch (field.id) { case "task-title": validateTitle(field); break; case "task-desc": validateDescription(field); break; case "task-deadline": validateDeadline(field); break; case "task-priority": validatePriority(field); break; } }); loadTasks(); }); |