Skip to content

Commit 33c84d2

Browse files
committed
Feat: localStorage implementation for data storage
1 parent 43e9fd4 commit 33c84d2

File tree

2 files changed

+199
-4
lines changed

2 files changed

+199
-4
lines changed

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h1 id="todoTitle">ToDo App</h1>
2727
</div>
2828

2929
<div class="task-container">
30-
<div class="task">
30+
<div class="task" taskid="1">
3131
<!-- Pending Task -->
3232
<i class="fa-regular fa-circle pendingSvg" data="check"></i>
3333

@@ -40,7 +40,7 @@ <h1 id="todoTitle">ToDo App</h1>
4040
<i class="fa-solid fa-trash deleteSvg" data="delete"></i>
4141
</div>
4242

43-
<div class="task">
43+
<div class="task" taskid="2">
4444
<i class="fa-regular fa-circle pendingSvg" data="check"></i>
4545

4646
<span class="taskText" data="task">Complete the first task</span>
@@ -49,7 +49,7 @@ <h1 id="todoTitle">ToDo App</h1>
4949
<i class="fa-solid fa-trash deleteSvg" data="delete"></i>
5050
</div>
5151

52-
<div class="task">
52+
<div class="task" taskid="3">
5353
<i class="fa-regular fa-circle pendingSvg" data="check"></i>
5454

5555
<span class="taskText" data="task">Thank you for visiting here</span>

script.js

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const completedTask = (task) => {
1616

1717
const uncheckIcon = task.querySelector("i.pendingSvg");
1818
const editIcon = task.querySelector("i.editSvg");
19+
const taskId = Number.parseInt(task.getAttribute("taskid"));
1920

2021
if(uncheckIcon.className === taskUncompletedIcon)
2122
{
@@ -32,6 +33,9 @@ const completedTask = (task) => {
3233
// update task counts
3334
taskCount--;
3435
updateTaskCount();
36+
37+
// update the localStorage data
38+
updateTaskStatus(true, taskId);
3539
}
3640
else
3741
{
@@ -43,6 +47,9 @@ const completedTask = (task) => {
4347
// update task counts
4448
taskCount++;
4549
updateTaskCount();
50+
51+
// update the localStorage data
52+
updateTaskStatus(false, taskId);
4653
}
4754
}
4855

@@ -58,12 +65,19 @@ const editTaskText = (task) => {
5865
editTaskText.addEventListener("blur", () => {
5966
// Make the task text uneditable
6067
editTaskText.setAttribute("contenteditable", "false");
68+
69+
// update task text into local storage
70+
updateTaskTextInStorage(task);
6171
});
6272
}
6373

6474
// delete the task when clicked on deleteIcon
6575
const deleteTask = (task) => {
66-
// remove the task from the list
76+
77+
// delete task from storage
78+
deleteTaskFromStorage(task);
79+
80+
// remove the task from the list
6781
task.remove();
6882

6983
// update task counts
@@ -91,6 +105,9 @@ const addNewTask = () => {
91105
// update task counts
92106
taskCount++;
93107
updateTaskCount();
108+
109+
// add the task data to localStorage
110+
addNewTaskToLocalStorage(newTaskField);
94111
}
95112
else
96113
alert("Task must be of at least 5 characters to be registered.");
@@ -190,5 +207,183 @@ clearAllTaskBtn.addEventListener("click", () => {
190207

191208
// focus the input box for typing
192209
taskInput.focus();
210+
211+
// format the localStorage for tasks data
212+
localStorage.setItem("tasks", JSON.stringify([]));
193213
}
194214
});
215+
216+
// get data from local storage
217+
const loadData = () => {
218+
const tasks = JSON.parse(localStorage.getItem("tasks"));
219+
return tasks ? tasks : [];
220+
}
221+
222+
// list the tasks from storage
223+
const listTasksFromStorage = () => {
224+
225+
// get data from local storage
226+
const tasksData = loadData();
227+
taskCount = 0;
228+
229+
// clear the task-container
230+
taskContainer.innerHTML = '';
231+
232+
// list down the tasks
233+
for(const task of tasksData)
234+
{
235+
const newTaskField = taskField.cloneNode(true);
236+
237+
// select the task text field and the text from storage
238+
const newTaskText = newTaskField.querySelector(".taskText");
239+
newTaskText.textContent = task.text;
240+
241+
// if true add the checked class
242+
if(task.completed)
243+
{
244+
newTaskField.classList.add("checked");
245+
const uncheckIcon = newTaskField.querySelector("i.pendingSvg");
246+
uncheckIcon.className = taskCompletedIcon;
247+
}
248+
else
249+
taskCount++;
250+
251+
// add taskid to tasks
252+
newTaskField.setAttribute("taskid", task.id);
253+
254+
// append the task
255+
taskContainer.append(newTaskField);
256+
}
257+
258+
// update the taskCount
259+
updateTaskCount();
260+
}
261+
262+
// add initial tasks to the local storage
263+
const executeOnceOnVisit = () => {
264+
265+
// function to add initial tasks data if visited first time
266+
const addInitialTasks = () => {
267+
// initial 3 tasks data
268+
const initialTasks = [
269+
{
270+
id: 1,
271+
text: "Follow @Alkaison on Twitter",
272+
completed: false
273+
},
274+
{
275+
id: 2,
276+
text: "Complete the first task",
277+
completed: false
278+
},
279+
{
280+
id: 3,
281+
text: "Thank you for visiting here",
282+
completed: false
283+
}
284+
];
285+
localStorage.setItem("tasks", JSON.stringify(initialTasks));
286+
localStorage.setItem("firstVisit", true);
287+
localStorage.setItem("taskId", 3);
288+
}
289+
290+
// check if the user came first time
291+
const firstVisit = localStorage.getItem("firstVisit");
292+
293+
if(!firstVisit)
294+
addInitialTasks();
295+
else
296+
listTasksFromStorage();
297+
}
298+
299+
// execute on first visit
300+
executeOnceOnVisit();
301+
302+
// generate new taskID
303+
const getNewTaskID = () => {
304+
let currentTaskID = localStorage.getItem("taskId");
305+
currentTaskID++;
306+
localStorage.setItem("taskId", currentTaskID);
307+
return currentTaskID;
308+
}
309+
310+
// add new Task data to local storage
311+
const addNewTaskToLocalStorage = (newTaskData) => {
312+
313+
// select task text and add the taskId
314+
const taskText = newTaskData.querySelector(".taskText").textContent;
315+
taskId = getNewTaskID();
316+
newTaskData.setAttribute("taskid", taskId);
317+
318+
// load existing data from localStorage
319+
const tasksData = loadData();
320+
321+
const newData = {
322+
id: taskId,
323+
text: taskText,
324+
completed: false
325+
}
326+
327+
// add the data and update localStorage
328+
tasksData.push(newData);
329+
localStorage.setItem("tasks", JSON.stringify(tasksData));
330+
}
331+
332+
// delete data from localStorage
333+
const deleteTaskFromStorage = (deleteTask) => {
334+
335+
// get data from local storage
336+
const tasksData = loadData();
337+
338+
// get task id from task-element
339+
const ID = deleteTask.getAttribute("taskid");
340+
341+
// filter and remove the task id data from storage
342+
const newTasksData = tasksData.filter((task) => task.id != ID);
343+
344+
// update the localStorage data
345+
localStorage.setItem("tasks", JSON.stringify(newTasksData));
346+
}
347+
348+
// task completed or not
349+
const updateTaskStatus = (status, taskId) => {
350+
351+
// get data from local storage
352+
const tasksData = loadData();
353+
354+
// update status of task
355+
for(const task of tasksData)
356+
{
357+
if(task.id === taskId)
358+
{
359+
task.completed = status;
360+
}
361+
}
362+
363+
// update the data at local storage
364+
localStorage.setItem("tasks", JSON.stringify(tasksData));
365+
}
366+
367+
// update task text into local storage
368+
const updateTaskTextInStorage = (task) => {
369+
370+
// get data from local storage
371+
const tasksData = loadData();
372+
373+
// select task text
374+
const taskText = task.querySelector(".taskText").textContent;
375+
376+
const taskId = Number.parseInt(task.getAttribute("taskid"));
377+
378+
// update text of task
379+
for(const task of tasksData)
380+
{
381+
if(task.id === taskId)
382+
{
383+
task.text = taskText;
384+
}
385+
}
386+
387+
// update the data at local storage
388+
localStorage.setItem("tasks", JSON.stringify(tasksData));
389+
}

0 commit comments

Comments
 (0)