-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
64 lines (54 loc) · 1.66 KB
/
App.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
import NotesView from "./NotesView.js";
import NotesAPI from "./NotesAPI.js";
export default class App {
constructor(root) {
this.notes = [];
this.activeNote = null;
this.view = new NotesView(root, this._handlers());
this._refreshNotes();
}
_refreshNotes() {
const notes = NotesAPI.getAllNotes();
this._setNotes(notes);
if (notes.length > 0) {
this._setActiveNote(notes[0]);
}
}
_setNotes(notes) {
this.notes = notes;
this.view.updateNoteList(notes);
this.view.updateNotePreviewVisibility(notes.length > 0);
}
_setActiveNote(note) {
this.activeNote = note;
this.view.updateActiveNote(note);
}
_handlers() {
return {
onNoteSelect: noteId => {
const selectedNote = this.notes.find(note => note.id == noteId);
this._setActiveNote(selectedNote);
},
onNoteAdd: () => {
const newNote = {
title: "New Note",
body: "Take note..."
};
NotesAPI.saveNote(newNote);
this._refreshNotes();
},
onNoteEdit: (title, body) => {
NotesAPI.saveNote({
id: this.activeNote.id,
title,
body
});
this._refreshNotes();
},
onNoteDelete: noteId => {
NotesAPI.deleteNote(noteId);
this._refreshNotes();
},
};
}
}