Skip to content
Snippets Groups Projects

Adding toDo app functionability

Merged Jesus Galaz Reyes requested to merge 2-build-javascript-core-functionality into main
Files
2
+ 20
2
// Obtener referencia a los elementos del DOM
// Get reference to DOM elements
const taskList = document.querySelector('.task-list ul');
const taskList = document.querySelector('.task-list ul');
const newTaskForm = document.querySelector('form');
const newTaskForm = document.querySelector('form');
@@ -78,7 +78,7 @@ newTaskForm.addEventListener('submit', async (e) => {
@@ -78,7 +78,7 @@ newTaskForm.addEventListener('submit', async (e) => {
const newTodo = await res.json();
const newTodo = await res.json();
taskList.appendChild(createTaskElement(newTodo));
taskList.appendChild(createTaskElement(newTodo));
input.value = ''; // Limpiar el campo de entrada
input.value = ''; // Clear input field
} catch (err) {
} catch (err) {
console.error('Error adding task:', err);
console.error('Error adding task:', err);
}
}
@@ -86,6 +86,12 @@ newTaskForm.addEventListener('submit', async (e) => {
@@ -86,6 +86,12 @@ newTaskForm.addEventListener('submit', async (e) => {
// Mark task as completed
// Mark task as completed
async function markAsDone(taskId, isDone) {
async function markAsDone(taskId, isDone) {
 
// Input sanitation
 
if (isNaN(parseInt(taskId, 10)) && typeof(isDone) === 'boolean') {
 
console.error('Invalid task ID or task status is invalid');
 
return;
 
}
 
try {
try {
const res = await fetch(`/api/todos/${taskId}/done`, {
const res = await fetch(`/api/todos/${taskId}/done`, {
method: 'PUT',
method: 'PUT',
@@ -104,6 +110,12 @@ async function markAsDone(taskId, isDone) {
@@ -104,6 +110,12 @@ async function markAsDone(taskId, isDone) {
// Erase task
// Erase task
async function deleteTask(taskId) {
async function deleteTask(taskId) {
 
// Input sanitation
 
if (isNaN(parseInt(taskId, 10))) {
 
console.error('Invalid task ID');
 
return;
 
}
 
try {
try {
const res = await fetch(`/api/todos/${taskId}`, {
const res = await fetch(`/api/todos/${taskId}`, {
method: 'DELETE'
method: 'DELETE'
@@ -122,6 +134,12 @@ async function deleteTask(taskId) {
@@ -122,6 +134,12 @@ async function deleteTask(taskId) {
// Edit task
// Edit task
async function updateTask(taskId, newDescription) {
async function updateTask(taskId, newDescription) {
 
// Input sanitation
 
if (isNaN(parseInt(taskId, 10)) && typeof(newDescription) === 'string') {
 
console.error('Invalid task ID or new description is not string');
 
return;
 
}
 
try {
try {
const res = await fetch(`/api/todos/${taskId}/description`, {
const res = await fetch(`/api/todos/${taskId}/description`, {
method: 'PUT',
method: 'PUT',
Loading