-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
146 lines (122 loc) · 4.42 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const firebaseConfig = {
apiKey: import.meta.env.apiKey,
authDomain: "crud-cda59.firebaseapp.com",
databaseURL: "https://crud-cda59-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "crud-cda59",
storageBucket: "crud-cda59.appspot.com",
messagingSenderId: "923543580003",
appId: "1:923543580003:web:437971f5d8c20bca793756",
measurementId: "G-BSTBWV9M2X"
};
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.8/firebase-app.js";
import { getDatabase, ref, serverTimestamp, set, onChildAdded, onValue, remove, update } from "https://www.gstatic.com/firebasejs/9.6.8/firebase-database.js";
//initialize database and global db ref
const app = initializeApp(firebaseConfig);
var db = getDatabase(app);
var reviews = document.getElementById("reviews");
var reviewsRef = ref(db, "reviews");
setupEventHandlers();
function setupEventHandlers() {
//user clicks the submit button (create or update)
reviewForm.addEventListener("submit", e => {
e.preventDefault(); //prevent page reload
handleSubmit();
Materialize.updateTextFields();
});
//user updates/deletes a particular review
reviews.addEventListener("click", e => {
updateReview(e);
deleteReview(e);
});
//Firebase events
onChildAdded(reviewsRef, (snapshot) => {
const data = snapshot.val();
const li = document.createElement("li");
li.id = snapshot.key;
li.innerHTML = reviewTemplate(data);
reviews.appendChild(li);
})
// reviewsRef.on("child_changed", data => {
// var reviewNode = document.getElementById(data.key);
// reviewNode.innerHTML = reviewTemplate(data.val());
// });
// reviewsRef.on("child_removed", data => {
// var reviewNode = document.getElementById(data.key);
// reviewNode.parentNode.removeChild(reviewNode);
// });
}
//CRUD operations
function handleSubmit() {
var reviewForm = document.getElementById("reviewForm");
var fullName = document.getElementById("fullName");
var message = document.getElementById("message");
var tags = document.getElementById("tags");
var hiddenId = document.getElementById("hiddenId");
//perform simple client-side validation
if (!fullName.value || !message.value || !tags) return null;
var id = hiddenId.value || Date.now();
//create new node / update existing node in Firebase
set(ref(db,"reviews/" + id), {
fullName: fullName.value,
message: message.value,
tags: tags.value,
createdAt: serverTimestamp()
});
clearForm();
}
function readReviews(data) {
var li = document.createElement("li");
li.id = data.key;
li.innerHTML = reviewTemplate(data.val());
reviews.appendChild(li);
}
function updateReview(e) {
var reviewNode = e.target.parentNode;
if (e.target.classList.contains("edit")) {
fullName.value = reviewNode.querySelector(".fullName").innerText;
message.value = reviewNode.querySelector(".message").innerText;
tags.value = reviewNode.querySelector(".tags").innerText;
hiddenId.value = reviewNode.id;
Materialize.updateTextFields();
}
}
function deleteReview(e) {
var reviewNode = e.target.parentNode;
if (e.target.classList.contains("delete")) {
var id = reviewNode.id;
db.ref("reviews/" + id).remove(); //Delete node at Firebase
clearForm();
}
}
function reviewTemplate({ fullName, message, tags, createdAt }) {
var createdAtFormatted = new Date(createdAt);
return `
<div>
<label>Full Name:</label>
<label class="fullName"><strong>${fullName}</strong></label>
</div>
<div>
<label>Message:</label>
<label class="message">${message}</label>
</div>
<div>
<label>Tags:</label>
<label class="tags">${tags}</label>
</div>
<div>
<label>Created:</label>
<label class="createdAt">${createdAtFormatted}</label>
</div>
<br>
<button class="waves-effect waves-light btn delete">Delete</button>
<button class="waves-effect waves-light btn edit">Edit</button>
<br><br><br><br>
`;
}
// Utility method to clear the form
function clearForm() {
fullName.value = "";
message.value = "";
tags.value = "";
hiddenId.value = "";
}