Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
169 lines (125 loc) · 4.46 KB
/
Copy pathscript.js
File metadata and controls
169 lines (125 loc) · 4.46 KB
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
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.2/firebase-app.js"
import { getDatabase,
ref,
push,
onValue,
update,
set,
remove } from "https://www.gstatic.com/firebasejs/10.7.2/firebase-database.js"
import { getAuth,
signOut,
onAuthStateChanged,
GoogleAuthProvider,
signInWithPopup } from "https://www.gstatic.com/firebasejs/10.7.2/firebase-auth.js"
const firebaseConfig = {
apiKey: "AIzaSyDyr7ZX1PiBp1sgS_NPUDsYLSG_jJENUvs",
databaseURL: "https://dad-app-5ddff-default-rtdb.europe-west1.firebasedatabase.app/",
authDomain: "dad-app-5ddff.firebaseapp.com"
}
const app = initializeApp(firebaseConfig)
const database = getDatabase(app)
const auth = getAuth(app)
const provider = new GoogleAuthProvider()
const questionInput = document.getElementById("question-input")
const addQuestionButton = document.getElementById("add-question-button")
const questionsDiv = document.getElementById("questions")
const viewLoggedIn = document.getElementById("view-logged-in")
const viewLoggedOut = document.getElementById("view-logged-out")
const googleButton = document.getElementById("google-button")
const signOutButton = document.getElementById("sign-out-button")
const body = document.body
signOutButton.onclick = () => {
signOut(auth)
}
googleButton.onclick = () => {
signInWithPopup(auth, provider)
.then((result) => {
console.log("Signed in with Google")
}).catch((error) => {
console.error(error.message)
})
}
addQuestionButton.onclick = () => {
const referenceInDB = ref(database, `${auth.currentUser.uid}/questions`)
const questionValue = questionInput.value
if (questionValue) {
const questionObject = {
body: questionValue,
isQuestionAsked: false,
isQuestionPrivate: true
}
push(referenceInDB, questionObject)
questionInput.value = ""
} else {
console.log("Please enter a value")
questionInput.style.backgroundColor = "#b32e65"
}
}
function fetchAllQuestions() {
const referenceInDB = ref(database, `${auth.currentUser.uid}/questions`)
onValue(referenceInDB, (snapshot) => {
if (snapshot.exists()) {
let questions = Object.entries(snapshot.val())
renderAppMode(questions)
}
})
}
onAuthStateChanged(auth, (user) => {
if (user) {
showLoggedInView()
fetchAllQuestions()
} else {
showLoggedOutView()
}
})
function renderAppMode(arrayOfThings) {
questionsDiv.innerHTML = ""
for (let i = 0; i < arrayOfThings.length; i++) {
let questionContainer = document.createElement("div")
questionContainer.classList.add("question-container")
let questionDiv = document.createElement("div")
questionDiv.classList.add("question")
questionDiv.innerText = arrayOfThings[i][1].body
let isQuestionAsked = arrayOfThings[i][1].isQuestionAsked
if (isQuestionAsked) {
questionDiv.classList.add("selected")
}
questionDiv.onclick = () => {
if (!isQuestionAsked) {
update(ref(database, `${auth.currentUser.uid}/questions/${arrayOfThings[i][0]}`), {
isQuestionAsked: true,
})
} else {
update(ref(database, `${auth.currentUser.uid}/questions/${arrayOfThings[i][0]}`), {
isQuestionAsked: false,
})
}
questionDiv.classList.toggle("selected")
}
let deleteButton = document.createElement("button")
deleteButton.classList.add("delete-button")
deleteButton.innerText = "❌"
deleteButton.onclick = () => {
console.log("delete button clicked")
remove(ref(database, `${auth.currentUser.uid}/questions/${arrayOfThings[i][0]}`))
questionContainer.remove()
}
questionContainer.appendChild(questionDiv)
questionContainer.appendChild(deleteButton)
questionsDiv.appendChild(questionContainer)
}
}
function showLoggedOutView() {
hideView(viewLoggedIn)
showView(viewLoggedOut)
}
function showLoggedInView() {
hideView(viewLoggedOut)
showView(viewLoggedIn)
}
function showView(view) {
view.style.display = "flex"
}
function hideView(view) {
view.style.display = "none"
}