-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
137 lines (119 loc) · 3.25 KB
/
main.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
import initHtml from "./lib/initHtml.js";
document.body.querySelector("#app").innerHTML = initHtml;
const enterID = document.querySelector("#enterID");
const enterName = document.querySelector("#enterName");
const enterAge = document.querySelector("#enterAge");
const findID = document.querySelector("#findID");
const allUsers = document.querySelector("#allUsers");
const insertBtn = document.querySelector("#insert");
const updateBtn = document.querySelector("#update");
const removeBtn = document.querySelector("#remove");
const findBtn = document.querySelector("#find");
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-app.js";
const firebaseConfig = {
apiKey: "AIzaSyAGAKcs4m4QnBr_IbWDzT6C8myM7H9XL34",
authDomain: "user-collection-95378.firebaseapp.com",
databaseURL: "https://user-collection-95378-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "user-collection-95378",
storageBucket: "user-collection-95378.appspot.com",
messagingSenderId: "524608473235",
appId: "1:524608473235:web:f542d96c7f5820abce2566"
};
const app = initializeApp(firebaseConfig);
import {
getDatabase,
ref,
get,
set,
child,
update,
remove
} from "https://www.gstatic.com/firebasejs/10.11.0/firebase-database.js";
const db = getDatabase();
getAllUsers();
function createUser(user) {
return `<tr>
<td>${user._id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>`;
}
function getAllUsers() {
allUsers.innerHTML = `Loading...`;
get(child(ref(db), "users"))
.then(snapshot => {
if (snapshot.exists()) {
allUsers.innerHTML = snapshot
.val()
.map(user => createUser(user))
.join("");
(enterID.value = ""), (enterName.value = ""), (enterAge.value = "");
} else {
alert("No User found");
}
})
.catch(error => {
alert(error);
});
}
function InsertUser() {
set(ref(db, "users/" + enterID.value), {
_id: enterID.value,
name: enterName.value,
age: enterAge.value
})
.then(() => {
alert("User added successfully");
getAllUsers();
})
.catch(error => {
alert(error);
});
}
function FindUser() {
const dbref = ref(db);
get(child(dbref, "users/" + findID.value))
.then(snapshot => {
if (snapshot.exists()) {
allUsers.innerHTML = `
<tr>
<td>${snapshot.val()._id}</td>
<td>${snapshot.val().name}</td>
<td>${snapshot.val().age}</td>
</tr>
`;
} else {
alert("No User found");
}
})
.catch(error => {
alert(error);
});
}
function UpdateUser() {
update(ref(db, "users/" + enterID.value), {
name: enterName.value,
age: enterAge.value
})
.then(() => {
alert("User updated successfully");
getAllUsers();
})
.catch(error => {
alert(error);
});
}
function RemoveUser() {
remove(ref(db, "users/" + enterID.value))
.then(() => {
alert("User deleted successfully");
getAllUsers();
})
.catch(error => {
alert(error);
});
}
insertBtn.addEventListener("click", InsertUser);
updateBtn.addEventListener("click", UpdateUser);
removeBtn.addEventListener("click", RemoveUser);
findBtn.addEventListener("click", FindUser);