-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
138 lines (107 loc) · 3.39 KB
/
script.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
import {db,doc,setDoc ,collection, addDoc,getDocs, deleteDoc,updateDoc,storage,
ref, uploadBytesResumable, getDownloadURL
} from "./firebase.js"
let addToDo = document.querySelector("#addToDo")
let addToDo2 = document.querySelector("#addToDo2")
let toDoList = document.querySelector("#toDoList")
let cost = document.querySelector("#cost")
let toDoValue = async ()=>{
document.querySelector(".container").style.display = "none";
let file = document.querySelector("#file")
const storageRef = ref(storage, `images${file.files[0].name}`);
const uploadTask = uploadBytesResumable(storageRef, file.files[0]);
uploadTask.on('state_changed',
(snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case 'paused':
console.log('Upload is paused');
break;
case 'running':
console.log('Upload is running');
break;
}
},
(error) => {
console.log(error);
},
() => {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
console.log('File available at', downloadURL);
});
}
);
try{
const docRef = await addDoc(collection(db, "cities"), {
name: addToDo.value,
description:addToDo2.value,
cost:cost.value,
image:file.files[0].name
});
console.log("Document written with ID: ", docRef.id);
}catch(error){
console.log(error);
}finally {
document.querySelector(".container").style.display = "block";
setTimeout(()=>{
window.location.reload()
},5000)
}
addToDo.value = "";
}
let showDoc = document.querySelector("#showDoc")
let addBtn = document.querySelector("#addBtn")
addBtn.addEventListener("click",toDoValue)
const querySnapshot = await getDocs(collection(db, "cities"));
querySnapshot.forEach(async (doc) => {
let getValue = doc.data()
toDoList.innerHTML += `<li>
<div class="card">
<div class="card-image">
<img src="https://via.placeholder.com/150" alt="Card Image">
</div>
<div class="card-content">
<h2 class="card-title">${getValue.name}</h2>
<p class="card-description">${getValue.description}.</p>
<p class="card-cost">${getValue.cost}</p>
<button id="${doc.id}" onclick="del(this)">Delete</button>
<button id="${doc.id}" onclick="edit(this)">Edit</button>
</div>
</div>
</li>`
});
console.log(querySnapshot);
document.querySelector(".container").style.display = "none";
async function del(e) {
try{
await deleteDoc(doc(db, "cities",e.id));
} catch(error){
console.log("error",error);
} finally {
document.querySelector(".container").style.display = "block";
setTimeout(()=>{
window.location.reload()
},5000)
}
}
async function edit(e) {
const toDoRef = doc(db, "cities", e.id);
try{
// Set the "capital" field of the city 'DC'
await updateDoc(toDoRef, {
name: prompt("update value")
});
} catch(error){
console.log("error",error);
} finally {
document.querySelector(".container").style.display = "block";
setTimeout(()=>{
window.location.reload()
},5000)
}
}
window.del = del;
window.edit = edit;