-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
135 lines (123 loc) · 3.25 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
import {
collection,
addDoc,
db,
serverTimestamp,
onSnapshot,
query,
orderBy,
getDocs,
deleteDoc,
doc,
updateDoc,
} from "./firebase.js";
//Sending data to firestore
let input = document.getElementById("input");
let list_item = document.getElementById("list_item");
const addItem = async (e) => {
e.preventDefault();
if (input.value.trim() !== "") {
if (input.value.length < 23) {
const docRef = await addDoc(collection(db, "todos"), {
value: input.value,
timestamp: serverTimestamp(),
});
} else {
Swal.fire({
title: "Value should lessthen 22 leter",
showClass: {
popup: `
animate__animated
animate__fadeInUp
animate__faster
`
},
hideClass: {
popup: `
animate__animated
animate__fadeOutDown
animate__faster
`
}
});
}
} else {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Can't add an empty value",
});
}
input.value = "";
}
const addItem_btn = document.getElementById("addItem_btn");
addItem_btn.addEventListener("click", addItem);
//Button event listener
const getElements = () => {
let editButtons = document.querySelectorAll(".edit_btn");
let deleteButtons = document.querySelectorAll(".del_btn");
editButtons.forEach((button, i) => {
button.addEventListener("click", () => {
editItem(i);
});
});
deleteButtons.forEach((button, i) => {
button.addEventListener("click", () => {
deleteItem(i);
});
});
};
//Edit Button
const editItem = async (index) => {
let newValue = prompt("Enter new value:");
if (newValue !== null) {
if (newValue.length < 23) {
// Update Firestore data
const ref = query(collection(db, "todos"), orderBy("timestamp", "desc"));
const querySnapshot = await getDocs(ref);
const documents = querySnapshot.docs;
const docId = documents[index].id;
await updateDoc(doc(db, "todos", docId), {
value: newValue,
timestamp: serverTimestamp(),
});
// Update UI
let paragraphs = document.querySelectorAll(".para");
paragraphs[index].textContent = newValue;
} else {
alert("Value should be less than 20 characters");
}
} else {
alert("Can't add an empty value");
}
};
//Delete button
const deleteItem = async (index) => {
const ref = query(collection(db, "todos"), orderBy("timestamp", "desc"));
const querySnapshot = await getDocs(ref);
const documents = querySnapshot.docs;
const docId = documents[index].id;
await deleteDoc(doc(db, "todos", docId));
};
//Getting data from FireStore & Display
const getData = async () => {
const ref = query(collection(db, "todos"), orderBy("timestamp", "desc"));
const unsubscribe = onSnapshot(ref, (querySnapshot) => {
list_item.innerHTML = "";
querySnapshot.forEach((doc) => {
list_item.innerHTML += `
<div class="list_item">
<div>
<p class="para">${doc.data().value}</p>
</div>
<div>
<button class="edit_btn">Edit</button>
<button class="del_btn">Delete</button>
</div>
</div>
`;
});
getElements();
});
};
getData();