-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotesAPI.js
34 lines (28 loc) · 1.11 KB
/
NotesAPI.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
export default class NotesAPI {
static getAllNotes() {
const notes = JSON.parse(localStorage.getItem("notesapp-notes") || "[]");
return notes.sort((a, b) => {
return new Date(a.updated) > new Date(b.updated) ? -1 : 1;
});
}
static saveNote(noteToSave) {
const notes = NotesAPI.getAllNotes();
const existing = notes.find(note => note.id == noteToSave.id);
// Edit/Update
if (existing) {
existing.title = noteToSave.title;
existing.body = noteToSave.body;
existing.updated = new Date().toISOString();
} else {
noteToSave.id = Math.floor(Math.random() * 1000000);
noteToSave.updated = new Date().toISOString();
notes.push(noteToSave);
}
localStorage.setItem("notesapp-notes", JSON.stringify(notes));
}
static deleteNote(id) {
const notes = NotesAPI.getAllNotes();
const newNotes = notes.filter(note => note.id != id);
localStorage.setItem("notesapp-notes", JSON.stringify(newNotes));
}
}