Skip to content

Commit 954cd97

Browse files
authored
Create app.js
1 parent ab871b3 commit 954cd97

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/app.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const noteTitle = document.getElementById("noteTitle");
2+
const noteContent = document.getElementById("noteContent");
3+
const addNoteBtn = document.getElementById("addNote");
4+
const notesList = document.getElementById("notesList");
5+
6+
// Add note
7+
addNoteBtn.addEventListener("click", () => {
8+
const title = noteTitle.value.trim();
9+
const content = noteContent.value.trim();
10+
if (!title || !content) return alert("Both fields required!");
11+
12+
db.collection("notes").add({ title, content, timestamp: Date.now() })
13+
.then(() => {
14+
noteTitle.value = "";
15+
noteContent.value = "";
16+
});
17+
});
18+
19+
// Real-time listener
20+
db.collection("notes").orderBy("timestamp", "desc")
21+
.onSnapshot(snapshot => {
22+
notesList.innerHTML = "";
23+
snapshot.forEach(doc => {
24+
const note = doc.data();
25+
const div = document.createElement("div");
26+
div.className = "note-card";
27+
div.innerHTML = `
28+
<h3>${note.title}</h3>
29+
<p>${note.content}</p>
30+
<button onclick="deleteNote('${doc.id}')">Delete</button>
31+
`;
32+
notesList.appendChild(div);
33+
});
34+
});
35+
36+
// Delete note
37+
function deleteNote(id) {
38+
db.collection("notes").doc(id).delete();
39+
}

0 commit comments

Comments
 (0)