fix(tasks): retain task IDs on reload

Added an optional 'id' parameter to the createTaskElement method to retain task IDs when reloading tasks from storage. This ensures task IDs remain consistent across sessions, preventing issues related to task identification and management.
This commit is contained in:
Kumi 2024-07-31 11:51:26 +02:00
parent 5c5473ad7b
commit 482576ed32
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -353,12 +353,12 @@ class Kanblendar {
} }
} }
createTaskElement(title, description, dueTime, column, notifyBefore) { createTaskElement(title, description, dueTime, column, notifyBefore, id = null) {
const id = `task-${Date.now()}`; const taskId = id || `task-${Date.now()}`;
const newTask = document.createElement('div'); const newTask = document.createElement('div');
newTask.classList.add('kanblendar-task'); newTask.classList.add('kanblendar-task');
newTask.setAttribute('draggable', 'true'); newTask.setAttribute('draggable', 'true');
newTask.setAttribute('id', id); newTask.setAttribute('id', taskId);
newTask.dataset.dueTime = dueTime; newTask.dataset.dueTime = dueTime;
newTask.dataset.column = column; newTask.dataset.column = column;
newTask.dataset.notifyBefore = notifyBefore; newTask.dataset.notifyBefore = notifyBefore;
@ -368,7 +368,7 @@ class Kanblendar {
`; `;
newTask.addEventListener('dragstart', (e) => this.dragStart(e)); newTask.addEventListener('dragstart', (e) => this.dragStart(e));
newTask.addEventListener('click', (e) => this.openModal(newTask)); // Add click event to open modal for editing newTask.addEventListener('click', (e) => this.openModal(newTask)); // Add click event to open modal for editing
this.tasks.set(id, { title, description, dueTime, column, notifyBefore }); this.tasks.set(taskId, { title, description, dueTime, column, notifyBefore });
return newTask; return newTask;
} }
@ -507,9 +507,8 @@ class Kanblendar {
const tasksArray = JSON.parse(serializedData); const tasksArray = JSON.parse(serializedData);
tasksArray.forEach(taskData => { tasksArray.forEach(taskData => {
const { id, title, description, dueTime, column, notifyBefore } = taskData; const { id, title, description, dueTime, column, notifyBefore } = taskData;
const taskElement = this.createTaskElement(title, description, dueTime, column, notifyBefore); const taskElement = this.createTaskElement(title, description, dueTime, column, notifyBefore, id);
taskElement.id = id; // Restore original ID this.tasks.set(id, { title, description, dueTime, column, notifyBefore });
this.tasks.set(id, { title, description, dueTime, notifyBefore });
this.moveTaskToColumn(taskElement, column); this.moveTaskToColumn(taskElement, column);
if (dueTime) { if (dueTime) {
this.updateTaskLocation(taskElement, dueTime); this.updateTaskLocation(taskElement, dueTime);