I am building a kanban board with the functionality of saving tasks to the local storage. I can create, delete, drag and drop tasks and save all these updates to the local storage. However, when I reload the page and try to delete a task, it only gets removed from the DOM and not from the local storage.
To reproduce the error, create a task and reload the page then try to delete it.
Here is my code for removing a task:
function renderTasks(list, parentId) {
list.forEach((taskObj) => {
const parentList = document.getElementById(parentId);
const taskEl = CreateTask();
const taskId = taskObj.id;
const dropzoneEl = createDropZoneEl(taskId);
taskEl.querySelector(".input-area").value = taskObj.content;
parentList.lastElementChild.before(taskEl);
taskEl.after(dropzoneEl);
// check if taskObj already exists in list
const index = list.findIndex((item) => item.id === taskObj.id);
if (index !== -1) {
// remove taskObj from list
list.splice(index, 1);
}
});
}
function render() {
renderTasks(listOne, "not-started");
renderTasks(listTwo, "in-progress");
renderTasks(listThree, "completed");
}
render()
function removeTask(id) {
const taskId = id;
const taskEl = document.getElementById(taskId);
const dropzoneEl = document.getElementById(`dropzone-${taskId}`);
taskEl.remove();
dropzoneEl.remove();
// Remove task from local storage
listOne = listOne.filter((task) => task.id !== id);
listTwo = listTwo.filter((task) => task.id !== id);
listThree = listThree.filter((task) => task.id !== id);
// save changes to local storage
saveData("listOne", listOne);
saveData("listTwo", listTwo);
saveData("listThree", listThree);
}
How can I fix this error and make sure that the tasks are removed from both the DOM and the local storage after reloading the page?
I tried to change the logic of the remove function but nothing works