forked from Yash-g17/yash-g17.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.js
115 lines (99 loc) · 2.64 KB
/
notes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Structure of notes object:
{
"<course_name>": [
{
"key": ,
"desc":
},
],
}
*/
const noteForm = document.querySelector("#note-form");
const notesDisplay = document.querySelector("#notes-display");
const COURSE = notesDisplay.getAttribute("course");
const BASE_OBJ = {
CSF111: [],
MATHF112: [],
MATHF113: [],
EEEF111: [],
PHYF111: [],
BITSF110: [],
BITSF111: [],
BITSF112: [],
PHYF110: [],
CHEMF110: [],
BIOF110: [],
MEF112: []
};
/*
Returns the JSON form of the notes object from localStorage.
If the notes object does not exist, it is created using BASE_OBJ.
*/
const getNotesObj = () => {
if (!localStorage.notes) storeNotesObj(BASE_OBJ);
return JSON.parse(localStorage.getItem("notes"));
};
/*
Receives the notes object, and puts it into localStorage.
The notesDisplay must also be refreshed.
*/
const storeNotesObj = (notes) => {
localStorage.setItem("notes", JSON.stringify(notes));
refreshNotesDisplay();
};
/*
Creates a note using the info provided by the user in the form (this = form element). Generates a random key to indentify the note.
Adds it to the appropriate course of the notes object.
*/
const addNote = function (e) {
e.preventDefault();
const notes = getNotesObj(),
key = Math.random();
const note = {
key,
desc: this[0].value
};
notes[COURSE].push(note);
storeNotesObj(notes);
noteForm.reset();
};
/*
Removes the note, corresponding to the clicked note-delete button, from the appropriate course of the notes object.
Uses the key obtained from the parent div of the note-delete button to delete the correct note.
*/
const removeNote = function (e) {
e.preventDefault();
const notes = getNotesObj(),
key = this.parentElement.id;
notes[COURSE] = notes[COURSE].filter((note) => note.key != key);
storeNotesObj(notes);
};
/*
Refreshes the notesDisplay by:
1. Clearing it.
2. Generating the html for each of the notes present in the appropriate course of the notes object.
3. Adds click listeners to the delete-note buttons of each note.
*/
const refreshNotesDisplay = () => {
const courseNotes = getNotesObj()[COURSE];
notesDisplay.innerHTML = "";
courseNotes.forEach((note) => {
const noteHTML = `
<div class="noteCard my-2 mx-2 card" style="width: 18rem;">
<div class="card-body" id="${note.key}">
<p class="card-text">${note.desc}</p>
<button class="btn btn-secondary note-delete">Delete</button>
</div>
</div>
`;
notesDisplay.innerHTML += noteHTML;
});
document
.querySelectorAll(".note-delete")
.forEach((deleteBtn) =>
deleteBtn.addEventListener("click", removeNote)
);
};
refreshNotesDisplay();
noteForm.addEventListener("submit", addNote);