Skip to content

Commit

Permalink
add: local storage saves, remove session storage,comments
Browse files Browse the repository at this point in the history
  • Loading branch information
amylour committed Oct 6, 2024
1 parent 3b1dcd1 commit fa0296a
Showing 1 changed file with 18 additions and 44 deletions.
62 changes: 18 additions & 44 deletions assets/js/script.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
let currentTaskBox = null;

// Load tasks from session storage on page load
// Load tasks from local storage on page load
window.addEventListener('load', function() {
const taskDisplaySection = document.getElementById('taskDisplaySection');
const tasks = JSON.parse(sessionStorage.getItem('tasks')) || [];
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];

tasks.forEach(task => {
if (!task.deleted) {
const taskBox = createTaskBox(task.text, task.size, task.comment, task.completed, task.deleted);
const taskBox = createTaskBox(task.text, task.size, task.completed, task.deleted);
taskDisplaySection.appendChild(taskBox);
}
});
Expand All @@ -20,40 +20,20 @@ document.getElementById('saveTaskButton').addEventListener('click', function() {
const taskDisplaySection = document.getElementById('taskDisplaySection');

if (taskInput.value.trim() !== '') {
const taskBox = createTaskBox(taskInput.value, taskSize, '', false, false);
const taskBox = createTaskBox(taskInput.value, taskSize, false, false);
taskDisplaySection.appendChild(taskBox);

// Save task to session storage
const tasks = JSON.parse(sessionStorage.getItem('tasks')) || [];
tasks.push({ text: taskInput.value, size: taskSize, comment: '', completed: false, deleted: false });
sessionStorage.setItem('tasks', JSON.stringify(tasks));
// Save task to local storage
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
tasks.push({ text: taskInput.value, size: taskSize, completed: false, deleted: false });
localStorage.setItem('tasks', JSON.stringify(tasks));

taskInput.value = ''; // Clear the input field
}
});

// Save comment to the current task box and session storage
document.getElementById('saveCommentButton').addEventListener('click', function() {
const taskComment = document.getElementById('taskComment').value;

if (currentTaskBox) {
// Set the comment on the current task box element
currentTaskBox.setAttribute('data-comment', taskComment);
$('#commentModal').modal('hide');

// Retrieve tasks from session storage and update the correct one
const tasks = JSON.parse(sessionStorage.getItem('tasks')) || [];
const taskIndex = tasks.findIndex(t => t.text === currentTaskBox.textContent && t.size === currentTaskBox.className.split(' ').pop());

if (taskIndex > -1) {
tasks[taskIndex].comment = taskComment;
sessionStorage.setItem('tasks', JSON.stringify(tasks)); // Update session storage
}
}
});

// Function to create a task box
function createTaskBox(text, size, comment, completed, deleted) {
function createTaskBox(text, size, completed, deleted) {
const taskDisplaySection = document.getElementById('taskDisplaySection');
const existingTasks = taskDisplaySection.getElementsByClassName('task-box ' + size);

Expand All @@ -73,7 +53,6 @@ function createTaskBox(text, size, comment, completed, deleted) {
const taskBox = document.createElement('div');
taskBox.className = 'task-box ' + size;
taskBox.textContent = text;
taskBox.setAttribute('data-comment', comment);

// Create tick button
const tickButton = document.createElement('button');
Expand All @@ -83,26 +62,21 @@ function createTaskBox(text, size, comment, completed, deleted) {
e.stopPropagation(); // Prevent triggering the task box click event
showConfetti(); // Trigger confetti effect

// Mark task as completed and deleted in session storage
const tasks = JSON.parse(sessionStorage.getItem('tasks')) || [];
const taskIndex = tasks.findIndex(t => t.text === text && t.size === size);
if (taskIndex > -1) {
tasks[taskIndex].completed = true;
tasks[taskIndex].deleted = true;
sessionStorage.setItem('tasks', JSON.stringify(tasks)); // Update session storage
}
// Mark task as deleted in local storage
let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
tasks = tasks.map(t => {
if (t.text === text && t.size === size && !t.deleted) {
t.deleted = true;
}
return t;
});
localStorage.setItem('tasks', JSON.stringify(tasks));

// Remove task box from DOM
taskBox.remove();
});
taskBox.appendChild(tickButton);

taskBox.addEventListener('click', function() {
currentTaskBox = taskBox;
document.getElementById('taskComment').value = taskBox.getAttribute('data-comment'); // Load the comment into the input field
$('#commentModal').modal('show');
});

return taskBox;
}

Expand Down

0 comments on commit fa0296a

Please sign in to comment.