-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.js
180 lines (170 loc) · 5.95 KB
/
firebase.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js";
const firebaseConfig = {
apiKey: "AIzaSyBcSeCdC1BuGwZ83PZmiWtINrc5_cEUUTY",
authDomain: "realtime-database-6131b.firebaseapp.com",
databaseURL:
"https://realtime-database-6131b-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "realtime-database-6131b",
storageBucket: "realtime-database-6131b.appspot.com",
messagingSenderId: "739374537647",
appId: "1:739374537647:web:df1e9212ddcae344d16d5a",
};
const app = initializeApp(firebaseConfig);
import {
getDatabase,
ref,
set,
get,
child,
update,
remove,
} from "https://www.gstatic.com/firebasejs/10.7.1/firebase-database.js";
const db = getDatabase(app);
let saveBtn = document.getElementById("save-btn");
let expiry = document.getElementById("medExpiryDate");
let amount = document.getElementById("medAmount");
let comrName = document.getElementById("comName");
let id = document.getElementById("medId");
let scintificName = document.getElementById("sinName");
let companName = document.getElementById("companyName");
let formula = document.getElementById("formulation");
let badge = document.getElementById("bdgId");
let price = document.getElementById("medPrice");
let searchName = document.getElementById("searchName");
let searchBtn = document.getElementById("search-btn");
let searchResult = document.getElementById("search-result");
let searchBtnName = document.getElementById("search-btnName");
let searchResultName = document.getElementById("search-resultName");
let updateBtn = document.getElementById("update-btn");
let removeBtn = document.getElementById("remove-btn");
function addData() {
set(ref(db, "med/" + id.value), {
comName: comrName.value,
medAmount: amount.value,
medExpiryDate: expiry.value,
sinName: scintificName.value,
companyName: companName.value,
formulation: formula.value,
bdgId: badge.value,
medprice: price.value,
totalPrice: price.value * amount.value,
});
clearInputFieldEl(expiry);
clearInputFieldEl(amount);
clearInputFieldEl(comrName);
clearInputFieldEl(id);
clearInputFieldEl(scintificName);
clearInputFieldEl(companName);
clearInputFieldEl(formula);
clearInputFieldEl(badge);
clearInputFieldEl(price);
}
function searchData() {
const medRef = ref(db);
clearInputFieldEl(searchResult);
get(child(medRef, "med/" + searchId.value))
.then((snapshot) => {
if (snapshot.exists()) {
searchResult.innerHTML = `<p> commercial name: ${snapshot.val().comName} </p>`;
searchResult.innerHTML += `<p> amount: ${
snapshot.val().medAmount
} </p>`;
searchResult.innerHTML += `<p> expiryDate: ${
snapshot.val().medExpiryDate
} </p>`;
searchResult.innerHTML += `<p> sinName: ${
snapshot.val().sinName
} </p>`;
searchResult.innerHTML += `<p> companyName: ${
snapshot.val().companyName
} </p>`;
searchResult.innerHTML += `<p> formulation: ${
snapshot.val().formulation
} </p>`;
searchResult.innerHTML += `<p> bdgId: ${snapshot.val().bdgId} </p>`;
searchResult.innerHTML += `<p> medprice: ${
snapshot.val().medprice
} </p>`;
searchResult.innerHTML += `<p> totalPrice: ${
snapshot.val().totalPrice
} </p>`;
} else {
searchResult.innerHTML = `<p>medicine with given Id is not found</p>`;
}
})
.catch((error) => {
alert(error.message);
});
}
function searchDataName() {
const medRef = ref(db, "med");
get(medRef).then((snapshot) => {
if (snapshot.exists()) {
const allMeds = snapshot.val();
const medId = Object.keys(allMeds).find(
(id) => allMeds[id].comName === searchName.value
);
if (medId) {
const med = allMeds[medId];
//searchResultName.innerHTML = `<p> commercial name: ${med.comName} </p>`;
searchResultName.innerHTML = `<p> amount: ${med.medAmount} </p>`;
searchResultName.innerHTML += `<p> expiryDate: ${med.medExpiryDate} </p>`;
searchResultName.innerHTML += `<p> scintific Name: ${med.sinName} </p>`;
searchResultName.innerHTML += `<p> company Name: ${med.companyName} </p>`;
searchResultName.innerHTML += `<p> formulation: ${med.formulation} </p>`;
searchResultName.innerHTML += `<p> Badge: ${med.bdgId} </p>`;
searchResultName.innerHTML += `<p> price: ${med.medprice} </p>`;
searchResultName.innerHTML += `<p> totalPrice: ${med.totalPrice} </p>`;
// searchResultName.innerHTML += `<p> id: ${med.medId} </p>`;
} else {
searchResultName.innerHTML += `<p>medicine with given name is not found</p>`;
}
}
});
// clearInputFieldEl(comName);
}
function clearInputFieldEl(inputFieldEl) {
inputFieldEl.value = "";
}
function updateData() {
update(ref(db, "med/" + id.value), {
//medName: name.value,
medAmount: amount.value,
medExpiryDate: expiry.value,
medprice: price.value,
totalPrice: price.value * amount.value,
bdgId: badge.value,
comName: comrName.value,
companyName: companName.value,
})
.then(() => {
alert("Data updated successfully");
})
.catch((error) => {
alert(error.message);
});
clearInputFieldEl(expiry);
clearInputFieldEl(amount);
clearInputFieldEl(comrName);
clearInputFieldEl(id);
clearInputFieldEl(scintificName);
clearInputFieldEl(companName);
clearInputFieldEl(formula);
clearInputFieldEl(badge);
clearInputFieldEl(price);
}
function removeData() {
remove(ref(db, "med/" + id.value))
.then(() => {
alert("Data removed successfully");
})
.catch((error) => {
alert(error.message);
});
clearInputFieldEl(id);
}
saveBtn.addEventListener("click", addData);
searchBtn.addEventListener("click", searchData);
searchBtnName.addEventListener("click", searchDataName);
updateBtn.addEventListener("click", updateData);
removeBtn.addEventListener("click", removeData);