-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
211 lines (183 loc) · 6.2 KB
/
Copy pathindex.js
File metadata and controls
211 lines (183 loc) · 6.2 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
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
import {
getDatabase,
ref,
push,
onValue,
remove,
} from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
import {
auth,
googleProvider,
signInWithPopup,
signOut,
app,
} from "./firebaseAuth.js";
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-auth.js";
import { GoogleAuthProvider } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-auth.js";
const appSettings = {
databaseURL:
"https://todoapp-da79d-default-rtdb.asia-southeast1.firebasedatabase.app/",
};
// const app = initializeApp(appSettings)
const database = getDatabase(app);
const shoppingListInDB = ref(database, "shoppingList");
const inputFieldEl = document.getElementById("input-field");
const addButtonEl = document.getElementById("add-button");
const shoppingListEl = document.getElementById("shopping-list");
const signInButton = document.getElementById("sign-in-button");
const signOutButton = document.getElementById("sign-out-button");
const userProfilePic = document.getElementById("user-profile-pic");
const userName = document.getElementById("user-name");
const deadlineFieldEl = document.getElementById("deadline-field");
let currentUser = null; // Variable to store the current authenticated user
onAuthStateChanged(auth, (user) => {
if (user) {
currentUser = user; // Store the user object
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/auth.user
const uid = user.uid;
console.log("User signed in:", user);
if (userProfilePic) userProfilePic.src = user.photoURL;
if (userName) userName.textContent = user.displayName;
// Redirect to index.html if on Wellcome.html or root path
if (
window.location.pathname.toLowerCase().includes("wellcome")
|| window.location.pathname === "/"
) {
window.location.href = "/index.html";
return;
}
if (document.body.classList.contains("logged-out")) {
document.body.classList.remove("logged-out");
}
if (!document.body.classList.contains("logged-in")) {
document.body.classList.add("logged-in");
}
if (userProfilePic) userProfilePic.style.display = "inline-block";
if (userName) userName.style.display = "inline-block";
if (signOutButton) signOutButton.style.display = "inline-block";
// ...
} else {
currentUser = null; // Clear user object on sign out
// User is signed out
console.log("User signed out");
if (
window.location.pathname.endsWith("index.html") ||
window.location.pathname === "/"
) {
window.location.href = "wellcome.html";
}
}
});
if (signInButton) {
console.log("Sign In Button Found");
signInButton.addEventListener("click", () => {
signInWithPopup(auth, googleProvider)
.then((result) => {
// This gives you a Google Access Token. You can use it to access the Google API.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// The signed-in user info.
const user = result.user;
// IdP data available in result.additionalUserInfo.profile
// ...
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email =
error.customData && error.customData.email
? error.customData.email
: "N/A";
console.error(
"Google Sign-In Error:",
errorCode,
errorMessage,
"Email:",
email
);
// The AuthCredential type that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
});
});
}
if (signOutButton) {
signOutButton.addEventListener("click", () => {
signOut(auth)
.then(() => {
// Sign-out successful.
console.log("Signed out successfully");
})
.catch((error) => {
// An error happened.
console.error("Error signing out:", error);
});
});
}
inputFieldEl.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
addButtonEl.click();
}
});
deadlineFieldEl.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
addButtonEl.click();
}
});
addButtonEl.addEventListener("click", function () {
let inputValue = inputFieldEl.value.trim();
let deadlineValue = deadlineFieldEl.value;
if (inputValue === "" || !currentUser) {
// Optionally, alert the user if they are not signed in or input is empty
alert("Please sign in and enter a task!");
return;
}
const taskData = {
text: inputValue,
userId: currentUser.uid,
userName: currentUser.displayName || "Anonymous",
deadline: deadlineValue,
status: "pending", // Initial status
};
push(shoppingListInDB, taskData);
clearInputFieldEl();
deadlineFieldEl.value = ""; // Clear the deadline field
});
onValue(shoppingListInDB, function (snapshot) {
if (snapshot.exists()) {
let itemsArray = Object.entries(snapshot.val());
clearShoppingListEl();
for (let i = 0; i < itemsArray.length; i++) {
let currentItem = itemsArray[i];
appendItemToShoppingListEl(currentItem);
}
} else {
shoppingListEl.innerHTML = `<span style="color: #ac7256; font-style: italic; font-size: 18px;">No items here... yet</span>`;
}
});
function clearShoppingListEl() {
shoppingListEl.innerHTML = "";
}
function clearInputFieldEl() {
inputFieldEl.value = "";
}
function appendItemToShoppingListEl(item) {
let itemID = item[0];
let itemData = item[1]; // Now itemData is an object
let newEl = document.createElement("li");
let userTag = itemData.userName
? `<span class="user-tag">@${itemData.userName.split(" ")[0]}</span>`
: "";
let deadlineDisplay = itemData.deadline
? `<span class="deadline">Due: ${itemData.deadline}</span>`
: "";
newEl.innerHTML = `${itemData.text} ${userTag} ${deadlineDisplay}`;
newEl.addEventListener("click", function () {
let exactLocationOfItemInDB = ref(database, `shoppingList/${itemID}`);
remove(exactLocationOfItemInDB);
});
shoppingListEl.append(newEl);
}