-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (94 loc) · 2.63 KB
/
app.js
File metadata and controls
107 lines (94 loc) · 2.63 KB
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
const firebaseConfig = {
apiKey: 'AIzaSyCo86Cjo3BSIgBhdax9MCqelI4Lb_0GZfo',
authDomain: 'note-app-f7a7c.firebaseapp.com',
databaseURL: 'https://note-app-f7a7c.firebaseio.com',
projectId: 'note-app-f7a7c',
storageBucket: 'note-app-f7a7c.appspot.com',
messagingSenderId: '526244492109',
appId: '1:526244492109:web:f08d6746778a0cd5687d04',
measurementId: 'G-R2ENCYLHQZ'
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
const db = firebase.firestore();
(function init() {
//initilizing the app
getNotes();
// fetching Firebase data
async function getNotes() {
const loadingNode = createLoadingNode();
notesContainer.append(loadingNode);
const notes = await db.collection('notes').get();
notesContainer.removeChild(loadingNode);
notesContainer.innerHTML = '';
if (notes.empty) {
notesContainer.innerHTML = '<span>add some notes to show here</span>';
return;
}
notes.forEach(note => {
const { id } = note;
const singleNote = note.data();
notesContainer.innerHTML += createNoteNode(singleNote.text, id);
});
}
// add data to Firebase
async function addNote(text) {
try {
await db.collection('notes').add({ text });
getNotes();
} catch (error) {
console.error(error);
}
}
addFormNode.addEventListener('submit', event => {
event.preventDefault();
const { value } = addFormInputNode;
if (value === '' || value === null) {
console.warn('you must write some text!');
addFormInputNode.focus();
return;
}
addNote(value);
// clear the input text
addFormInputNode.value = '';
});
// make the handle edit and delete single note global to use
// them in the ui-functions file
window.handleEditSingleNote = function(id, text) {
showEditField();
editFormInputNode.value = text;
editFormInputNode.setAttribute('data-id', id);
};
window.handleDeleteNote = function(id) {
deleteNote(id);
};
// update Note
async function updateNote(id, text) {
await db
.collection('notes')
.doc(id)
.set({ text });
getNotes();
}
// delete the doc from Firebase
async function deleteNote(id) {
await db
.collection('notes')
.doc(id)
.delete();
getNotes();
}
editFormNode.addEventListener('submit', event => {
event.preventDefault();
});
// submit the edit
editButton.addEventListener('click', e => {
const { value } = editFormInputNode;
const id = editFormInputNode.dataset.id;
updateNote(id, value);
});
cancelButton.addEventListener('click', e => {
hideEditField();
});
})();