Skip to content

Commit 20a5b3e

Browse files
committed
Updated localStorage function to Es6 Class
1 parent 530df8b commit 20a5b3e

3 files changed

Lines changed: 132 additions & 117 deletions

File tree

Notes App/script.js

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,59 @@ let isEditingMode = false;
88
let LOCALSTORAGE_NOTES_KEY = `Notes App`;
99
let previousBackground = null;
1010

11+
// LocalStorage
12+
13+
class LocalStorage {
14+
constructor(localStorageKey) {
15+
this.localStorageKey = localStorageKey;
16+
this.local = localStorage.getItem(this.localStorageKey)
17+
? JSON.parse(localStorage.getItem(this.localStorageKey))
18+
: [];
19+
}
20+
21+
save(newNote) {
22+
this.local.push(newNote);
23+
this.stringifyLocal();
24+
}
25+
26+
delete(id) {
27+
this.local = this.local.filter((item) => item.id !== id);
28+
this.stringifyLocal();
29+
}
30+
31+
update(id, title, text) {
32+
this.local = this.local.map((item) => {
33+
if (item.id === id) {
34+
item.title = title;
35+
item.text = text;
36+
}
37+
return item;
38+
});
39+
this.stringifyLocal();
40+
}
41+
42+
updateIsStared(id, value) {
43+
this.local = this.local.map((item) => {
44+
if (item.id === id) {
45+
item.isStared = value;
46+
}
47+
return item;
48+
});
49+
this.stringifyLocal();
50+
}
51+
52+
stringifyLocal() {
53+
return localStorage.setItem(
54+
this.localStorageKey,
55+
JSON.stringify(this.local)
56+
);
57+
}
58+
}
59+
60+
const noteLocal = new LocalStorage("Notes App");
61+
1162
addNoteEl.addEventListener("click", (e) => {
12-
let noteCount = getLocal().length ? getLocal().length : ``;
63+
let noteCount = noteLocal.local.length ? noteLocal.local.length : ``;
1364
getNotePad(`New note ${noteCount}`, null);
1465
bodyEl.classList.add("notePad__active");
1566
});
@@ -51,7 +102,7 @@ function createNoteDOM(data) {
51102
data.isStared
52103
? noteEl.classList.add("stared")
53104
: noteEl.classList.remove("stared");
54-
updateLocalIsStared(data.id, data.isStared);
105+
noteLocal.updateIsStared(data.id, data.isStared);
55106
});
56107

57108
// Text area cont
@@ -97,7 +148,7 @@ function createNoteDOM(data) {
97148

98149
delBtn.addEventListener("click", (e) => {
99150
noteEl.remove();
100-
deleteLocal(data.id);
151+
noteLocal.delete(data.id);
101152
});
102153
}
103154

@@ -199,14 +250,14 @@ function setNotesDOM(title, note, currentEditEl) {
199250
isStared: false,
200251
};
201252
createNoteDOM(newNote);
202-
setLocal(newNote);
253+
noteLocal.save(newNote);
203254
} else if (isEditingMode) {
204255
const id = currentEditEl.dataset.noteid;
205256
const titleEl = currentEditEl.querySelector("header .title h1");
206257
const noteEl = currentEditEl.querySelector(".textareaNote p");
207258
titleEl.innerHTML = title;
208259
noteEl.innerHTML = note;
209-
updateLocalNotes(id, title, note);
260+
noteLocal.update(id, title, note);
210261
isEditingMode = false;
211262
}
212263
}
@@ -237,59 +288,6 @@ function getCurrentDate() {
237288
return `${month}, ${todayDate} ${year}`;
238289
}
239290

240-
// LOCAL STORAGE
241-
242-
function getLocal() {
243-
return localStorage.getItem(LOCALSTORAGE_NOTES_KEY)
244-
? JSON.parse(localStorage.getItem(LOCALSTORAGE_NOTES_KEY))
245-
: [];
246-
}
247-
248-
function setLocal(newNote) {
249-
let local = getLocal();
250-
local.push(newNote);
251-
return localStorage.setItem(LOCALSTORAGE_NOTES_KEY, JSON.stringify(local));
252-
}
253-
254-
(function SetNoteDate() {
255-
let local = getLocal();
256-
if (local.length) {
257-
local.forEach((note) => {
258-
createNoteDOM(note);
259-
previousBackground = local[local.length - 1].backgroundColor;
260-
});
261-
}
262-
})();
263-
264-
function updateLocalNotes(id, title, note) {
265-
let local = getLocal();
266-
local = local.map((item) => {
267-
if (item.id === id) {
268-
item.title = title;
269-
item.text = note;
270-
}
271-
return item;
272-
});
273-
return localStorage.setItem(LOCALSTORAGE_NOTES_KEY, JSON.stringify(local));
274-
}
275-
276-
function deleteLocal(id) {
277-
let local = getLocal();
278-
local = local.filter((item) => item.id !== id);
279-
return localStorage.setItem(LOCALSTORAGE_NOTES_KEY, JSON.stringify(local));
280-
}
281-
282-
function updateLocalIsStared(id, value) {
283-
let local = getLocal();
284-
local = local.map((item) => {
285-
if (item.id === id) {
286-
item.isStared = value;
287-
}
288-
return item;
289-
});
290-
return localStorage.setItem(LOCALSTORAGE_NOTES_KEY, JSON.stringify(local));
291-
}
292-
293291
function getRandomBackground() {
294292
let backgroundColors = [
295293
`#b8dbd9`,
@@ -319,11 +317,20 @@ function searchNote(e) {
319317
notes.forEach((note) => {
320318
let noteTitle = note.querySelector("header .title h1");
321319
let notePara = note.querySelector(".textareaNote p");
322-
let notePara2 = notePara.innerText;
323320
let regex = new RegExp(value, "gi");
324321
note.style.display = `none`;
325322
if (notePara.innerText.match(regex) || noteTitle.innerText.match(regex)) {
326323
note.style.display = `unset`;
327324
}
328325
});
329326
}
327+
328+
(function () {
329+
let local = noteLocal.local;
330+
if (local.length) {
331+
local.forEach((item) => {
332+
createNoteDOM(item);
333+
});
334+
previousBackground = local[local.length - 1].backgroundColor;
335+
}
336+
})();

Todo App/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ <h1>Hey, <span id="userName">Dinesh</span></h1>
6262
<img src="./Images/Logo.png" alt="logo" />
6363
</div>
6464
<div class="textContent">
65-
<h1>Dinesh</h1>
65+
<h1 id="statUserName">Dinesh</h1>
6666
<p>Todo App</p>
6767
</div>
6868
</div>

Todo App/script.js

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,66 @@ const totalTaskEl = document.getElementById("totalTask");
1010
const CompletedTaskEl = document.getElementById("CompletedTask");
1111
const inCompletedTaskEl = document.getElementById("inCompletedTask");
1212
const userNameEl = document.getElementById("userName");
13+
const statUserNameEl = document.getElementById("statUserName");
1314

1415
let isEditMode = false;
1516
let editEl = null;
1617
let TODO_LOCALSTORAGE_KEY = `Todo`;
1718
let userName = getUserName();
18-
1919
userNameEl.innerHTML = userName;
2020

2121
formEl.addEventListener("submit", addTodo);
2222

23+
class LocalStorage {
24+
constructor(localStorageKey) {
25+
this.localStorageKey = localStorageKey;
26+
this.local = localStorage.getItem(this.localStorageKey)
27+
? JSON.parse(localStorage.getItem(this.localStorageKey))
28+
: [];
29+
}
30+
31+
save(newData) {
32+
this.local.push(newData);
33+
this.stringifyLocal();
34+
}
35+
36+
delete(id) {
37+
this.local = this.local.filter((item) => item.id !== id);
38+
this.stringifyLocal();
39+
}
40+
41+
update(id, task, description) {
42+
this.local = this.local.map((item) => {
43+
if (item.id === id) {
44+
item.task = task;
45+
item.description = description;
46+
}
47+
return item;
48+
});
49+
this.stringifyLocal();
50+
}
51+
52+
updateIsCompleted(id, value) {
53+
this.local = this.local.map((item) => {
54+
if (item.id === id) {
55+
item.isCompleted = value;
56+
}
57+
return item;
58+
});
59+
60+
this.stringifyLocal();
61+
}
62+
63+
stringifyLocal() {
64+
return localStorage.setItem(
65+
this.localStorageKey,
66+
JSON.stringify(this.local)
67+
);
68+
}
69+
}
70+
71+
let todoLocal = new LocalStorage(TODO_LOCALSTORAGE_KEY);
72+
2373
function addTodo(e) {
2474
e.preventDefault();
2575
let inputEl = this.querySelector("#userInput");
@@ -55,7 +105,7 @@ function createNewTodo(inputEl) {
55105
description: description,
56106
};
57107
createTodoEl(todoData);
58-
addTodoToLocal(todoData);
108+
todoLocal.save(todoData);
59109
inputEl.value = null;
60110
}
61111

@@ -147,7 +197,7 @@ function createTodoEl(todo) {
147197

148198
delBtn.addEventListener("click", (e) => {
149199
liEl.remove();
150-
deleteLocal(liEl.dataset.id);
200+
todoLocal.delete(liEl.dataset.id);
151201
});
152202

153203
// Checkbox Event
@@ -167,7 +217,7 @@ function createTodoEl(todo) {
167217
checkEl.innerHTML = `<span class="material-icons"> done </span>`;
168218
isCompValue = true;
169219
}
170-
updateIsCompletedLocal(liEl.dataset.id, isCompValue);
220+
todoLocal.updateIsCompleted(liEl.dataset.id, isCompValue);
171221
});
172222
}
173223

@@ -231,7 +281,7 @@ function editMyTodo(inputEl) {
231281
isEditMode = false;
232282
editEl = null;
233283
AddTodoBtnEl.innerHTML = `Add Todo`;
234-
editLocalTodo(id, task, description);
284+
todoLocal.update(id, task, description);
235285
}
236286

237287
function updateEditView(liEl) {
@@ -251,53 +301,6 @@ function getTodoId() {
251301
return Math.floor(Math.random() * 10000000).toString(16);
252302
}
253303

254-
function getLocal() {
255-
return localStorage.getItem(TODO_LOCALSTORAGE_KEY)
256-
? JSON.parse(localStorage.getItem(TODO_LOCALSTORAGE_KEY))
257-
: [];
258-
}
259-
260-
function addTodoToLocal(newTodo) {
261-
let local = getLocal();
262-
local.push(newTodo);
263-
return localStorage.setItem(TODO_LOCALSTORAGE_KEY, JSON.stringify(local));
264-
}
265-
266-
function editLocalTodo(id, task, description) {
267-
let local = getLocal();
268-
local = local.map((item) => {
269-
if (item.id === id) {
270-
item.task = task;
271-
item.description = description;
272-
}
273-
return item;
274-
});
275-
return localStorage.setItem(TODO_LOCALSTORAGE_KEY, JSON.stringify(local));
276-
}
277-
278-
function deleteLocal(id) {
279-
let local = getLocal();
280-
local = local.filter((item) => item.id !== id);
281-
return localStorage.setItem(TODO_LOCALSTORAGE_KEY, JSON.stringify(local));
282-
}
283-
284-
function updateIsCompletedLocal(id, value) {
285-
let local = getLocal();
286-
local = local.map((item) => {
287-
if (item.id === id) item.isCompleted = value;
288-
return item;
289-
});
290-
return localStorage.setItem(TODO_LOCALSTORAGE_KEY, JSON.stringify(local));
291-
}
292-
293-
(function upadateTodoList() {
294-
listsEl.innerHTML = null;
295-
let local = getLocal();
296-
if (local.length) {
297-
return local.forEach((todo) => createTodoEl(todo));
298-
}
299-
})();
300-
301304
// Button Ripple Effect
302305

303306
ctaBtnsEl.forEach((button) => {
@@ -368,12 +371,13 @@ window.addEventListener("click", (e) => {
368371
});
369372

370373
function showStats() {
371-
let local = getLocal();
374+
let local = todoLocal.local;
372375
let totalTaskLength = local.length;
373376
const completedTaskLength = local.filter((item) => item.isCompleted).length;
374377
const inCompletedTaskLength = totalTaskLength - completedTaskLength;
375378
const percentage =
376-
Math.floor((completedTaskLength / totalTaskLength) * 100).toString() === `NaN`
379+
Math.floor((completedTaskLength / totalTaskLength) * 100).toString() ===
380+
`NaN`
377381
? 0
378382
: Math.floor((completedTaskLength / totalTaskLength) * 100);
379383

@@ -436,6 +440,7 @@ function NameModalInput() {
436440
if (inputEl.value.trim()) {
437441
userName = inputEl.value;
438442
userNameEl.innerHTML = userName;
443+
statUserNameEl.innerHTML = userName;
439444
localStorage.setItem("userName", userName);
440445
}
441446
modalCont.remove();
@@ -458,7 +463,10 @@ function getUserName() {
458463

459464
(function setUserName() {
460465
let user = localStorage.getItem("userName");
461-
if (!user) {
462-
return NameModalInput();
463-
}
466+
if (!user) return NameModalInput();
467+
})();
468+
469+
(function () {
470+
let local = todoLocal.local;
471+
if (local.length) local.forEach((item) => createTodoEl(item));
464472
})();

0 commit comments

Comments
 (0)