forked from Jeruzael/simple-archive-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
221 lines (182 loc) · 6.01 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import {initializeApp} from 'firebase/app';
import {getFirestore, collection, getDocs} from 'firebase/firestore/lite';
import {getAuth, createUserWithEmailAndPassword} from "firebase/auth";
import {getDatabase, ref, set, onValue, child, get, push, update} from "firebase/database";
let email = document.querySelector(".email");
let password = document.querySelector(".password");
let passRetype = document.querySelector(".retypePass");
let signupBtn = document.querySelector(".signupBtn");
let readValue = document.querySelector(".reader");
let readValOnce = document.querySelector(".readValOnce");
let container = document.querySelector(".container");
let signup = document.querySelector(".signUp");
let home = document.createElement('div');
let postCont = document.createElement('div');
let testBtn = document.querySelector('.testBtn');
let signin = document.querySelector('.signinPanel');
let btnCont = document.createElement('div');
let createTabBtn = document.createElement('div');
const firebaseConfig = {
apiKey: "AIzaSyDd0PTOLxQr3_KHMH7IcfTsfTfNHa1C5l8",
authDomain: "simple-archive-app.firebaseapp.com",
projectId: "simple-archive-app",
storageBucket: "simple-archive-app.appspot.com",
messagingSenderId: "183155358943",
appId: "1:183155358943:web:03dd0f37e13ffa1dcf7d6a",
databaseURL: "https://simple-archive-app-default-rtdb.asia-southeast1.firebasedatabase.app/"
}
const app = initializeApp(firebaseConfig);
const db = getDatabase();
const dbRef = ref(db);
const auth = getAuth();
//Set function inside is the way of how to write data to data,
//but be carefull because this will overwrite the data inside your database
/*signupBtn.addEventListener('click', function(){
set(ref(db, 'Users/user1'), {
username: email.value,
password: password.value
});
password.value = "";
email.value = "";
//tatanggalin natin yung signup which is the child of the container
container.removeChild(signup);
home.className = "homePage";
container.appendChild(home);
postCont.className = "postCont";
home.appendChild(postCont);
btnCont.className = "btnCont";
home.appendChild(btnCont);
createTabBtn.className = "createTabBtn";
btnCont.appendChild(createTabBtn);
}); */
btnCont.addEventListener('click', ()=>{
let post = document.createElement('div');
postCont.appendChild(post);
post.className = "post";
console.log("btnWork");
});
function homePg(){
//tatanggalin natin yung signup which is the child of the container
container.removeChild(signup);
home.className = "homePage";
container.appendChild(home);
postCont.className = "postCont";
home.appendChild(postCont);
btnCont.className = "btnCont";
home.appendChild(btnCont);
createTabBtn.className = "createTabBtn";
btnCont.appendChild(createTabBtn);
}
function checkEmail(emailx){
let emLeng = emailx.length;
let emAtIndex = emailx.indexOf('@');
let emProvider = emailx.slice(emAtIndex);
//console.log(emLeng, "index of @: ", emAtIndex, "sliced: ", emProvider);
if(emProvider != '@gmail.com'){
console.log('please use valid email');
}else{
return true;
}
}
function passCheck(pass){
if(pass == passRetype.value){
return true;
}else{
return false;
}
}
signupBtn.addEventListener('click', ()=>{
let emailSignupKey = false;
let passwordSignupKey = false;
let uid = "ARC000";
const newAcc = {};
if(checkEmail(email.value) == true){
emailSignupKey = true;
newAcc['email'] = email.value;
console.log(newAcc);
}else{
email.value = "";
alert("invalid email");
}
if(passCheck(password.value) == true){
passwordSignupKey = true;
newAcc['password'] = password.value;
console.log(newAcc);
}else{
alert('password don\'t match, like u and your crush');
}
if(emailSignupKey && passwordSignupKey == true){
set(ref(db, '/Users/Student/' + uid + 2), newAcc);
homePg();
}
});
//this function is use to check the existing user
function showUsers(){
const checkId = ref(db, 'Users/Student');
onValue(checkId, (data)=>{
const d = data.val();
for(let x in d){
console.log(x);
}
});
}
testBtn.addEventListener('click', function(){
showUsers();
});
//signin button v
signin.addEventListener('click', ()=>{
});
//signin button^
//Masmadaling kumuha ng user data gamit tong function nato
//Pero masSafe yung readValOnce function kase it catch errors.
//this function will only show the property name not the values
readValue.addEventListener('click', function (){
const startCountRef = ref(db, 'Users/Student');
onValue(startCountRef, (snapshot) => {
const data = snapshot.val();
//updateStartCount(postElement, data);
for(let x in data){
console.log(x);
}
//console.log(data);
});
});
//ichecheck nang function nato kung nasa database ba yung value na hinahanap natin
readValOnce.addEventListener('click', () => {
get(child(dbRef, 'Users/Student/user1/email')).then((snapshot)=>{
if(snapshot.exists()){
console.log(snapshot.val());
}else{
console.log("No data available");
}
}).catch((error)=>{
console.error(error);
});
});
//updating or deleting data
function addBook(title, author, description){
const bookData = {
title: title,
author: author,
description: description
};
//get a key for a new book
const newBookKey = push(child(dbRef, 'ThesisBooks')).key;
//update new book data simultaneously in the book list and the author's book list.
const updates = {};
updates['/ThesisBooks/' + newBookKey] = bookData;
return update(dbRef, updates);
}
const armagedonDes = "The end is near, the signs are clear"+
"We clearly saw that all that what is written"+
"is happened and continuously happening";
//addBook('armagedon', 'joms', armagedonDes);
function updateSpec(updateKey){
const upData = {
title: updateKey
};
const updates = {};//Sinubukan kong gawing string yung updata. then napalitan yung author value ng 'upData'.
updates['ThesisBooks/book1/author'] = updateKey;
update(dbRef, updates);
}
//updateSpec('kalma');