-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
70 lines (51 loc) · 2.13 KB
/
todo.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
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.5/firebase-app.js";
import { getFirestore, collection, addDoc, getDocs } from "https://www.gstatic.com/firebasejs/10.12.5/firebase-firestore.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.12.5/firebase-analytics.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyDY3zwoBy9HfpsugbaS8BJbE4c-wf5BAl4",
authDomain: "batch-11-3191d.firebaseapp.com",
projectId: "batch-11-3191d",
storageBucket: "batch-11-3191d.appspot.com",
messagingSenderId: "1087219169012",
appId: "1:1087219169012:web:5b0babdb382fa5bddb90a1",
measurementId: "G-P1PS3T51ZB"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getFirestore(app);
// console.log("connected", db);
let input = document.getElementById('input-text');
let btn = document.getElementById('btn');
let ul = document.getElementById('ul');
btn.addEventListener('click', async function () {
let text = input.value
console.log(text);
if (text) {
try {
const docRef = await addDoc(collection(db, "todo"), {
todo: input.value,
});
input.value = ''
ul.innerHTML += `<li> ${text} </li>`
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}
const querySnapshot = await getDocs(collection(db, "todo"));
querySnapshot.forEach((doc) => {
// console.log(`${doc.id} => ${doc.data()}`);
let todos = doc.data()
console.log(todos.todo);
});
}else{
// // input.value = ''
// ul.innerHTML += `<li> ${text} </li>`
alert('Enter Your Message')
}
})