Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.0 kB
4
Indexable
Never
document.addEventListener("DOMContentLoaded", function () {
    const archiveTableBody = document.getElementById("archiveBody");

    displayArchivedTodos();

    function displayArchivedTodos() {
        const archivedTodos = JSON.parse(localStorage.getItem("archive")) || [];
        archivedTodos.forEach(todo => addTodoToTable(todo));
    }

    function addTodoToTable(todo) {
        const newRow = document.createElement("tr");
        newRow.innerHTML = `
            <td>${todo.title}</td>
            <td>${todo.priority}</td>
            <td>${todo.status}</td>
            <td><button class="restoreButton">Restore</button></td>
            <td><button class="deleteButton">Delete</button></td>
        `;
        archiveTableBody.appendChild(newRow);
        newRow.querySelector(".restoreButton").addEventListener("click", function () {
            restoreTodoFromTable(newRow, todo);
        });
        newRow.querySelector(".deleteButton").addEventListener("click", function () {
            deleteTodoFromArchive(newRow, todo);
        });
    }

    function restoreTodoFromTable(row, todo) {
        const todoTitle = todo.title;
        const archive = JSON.parse(localStorage.getItem("archive"));
        const updatedArchive = archive.filter(todo => todo.title !== todoTitle);
        localStorage.setItem("archive", JSON.stringify(updatedArchive));

        const todos = JSON.parse(localStorage.getItem("todos")) || [];
        todos.push(todo);
        localStorage.setItem("todos", JSON.stringify(todos));

        row.remove();
    }

    function deleteTodoFromArchive(row, todo) {
        const todoTitle = todo.title;
        const archive = JSON.parse(localStorage.getItem("archive"));
        const updatedArchive = archive.filter(todo => todo.title !== todoTitle);
        localStorage.setItem("archive", JSON.stringify(updatedArchive));
        row.remove();
    }
});
Leave a Comment