-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
141 lines (126 loc) · 4.51 KB
/
Copy pathapp.js
File metadata and controls
141 lines (126 loc) · 4.51 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
import { auth, signup, login, logout } from "./auth.js";
import {
getFirestore,
collection,
query,
orderBy,
onSnapshot,
addDoc,
serverTimestamp,
updateDoc
} from "https://www.gstatic.com/firebasejs/12.1.0/firebase-firestore.js";
import { app } from "./firebase-config.js";
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/12.1.0/firebase-auth.js";
const db = getFirestore(app);
// DOM Elements
const signupForm = document.getElementById("signupForm");
const loginForm = document.getElementById("loginForm");
const logoutBtn = document.getElementById("logoutBtn");
const todoSection = document.getElementById("todoSection");
const todoList = document.getElementById("todoList");
const todoInput = document.getElementById("todoInput");
const addTodoBtn = document.getElementById("addTodoBtn");
const authSection = document.getElementById("authSection");
const showLoginBtn = document.getElementById("showLoginBtn");
const showSignupBtn = document.getElementById("showSignupBtn");
let unsubscribeTodos = null;
// Toggle between login and signup forms
showLoginBtn.addEventListener("click", () => {
loginForm.style.display = "block";
signupForm.style.display = "none";
showLoginBtn.classList.add("active");
showSignupBtn.classList.remove("active");
});
showSignupBtn.addEventListener("click", () => {
signupForm.style.display = "block";
loginForm.style.display = "none";
showSignupBtn.classList.add("active");
showLoginBtn.classList.remove("active");
});
// Signup handler
signupForm.addEventListener("submit", async (e) => {
e.preventDefault();
const email = signupForm.email.value;
const password = signupForm.password.value;
try {
await signup(email, password);
signupForm.reset();
} catch (err) {
alert(err.message);
}
});
// Login handler
loginForm.addEventListener("submit", async (e) => {
e.preventDefault();
const email = loginForm.email.value;
const password = loginForm.password.value;
try {
await login(email, password);
loginForm.reset();
} catch (err) {
alert(err.message);
}
});
// Logout handler
logoutBtn.addEventListener("click", async () => {
await logout();
});
// Add todo handler (per-user subcollection)
addTodoBtn.addEventListener("click", async () => {
const title = todoInput.value.trim();
const user = auth.currentUser;
if (title && user) {
await addDoc(
collection(db, "users", user.uid, "todos"),
{
title: title,
completed: false,
createdAt: serverTimestamp()
}
);
todoInput.value = "";
}
});
// Auth state listener
onAuthStateChanged(auth, (user) => {
if (user) {
todoSection.style.display = "block";
logoutBtn.style.display = "inline-block";
authSection.style.display = "none";
// Unsubscribe previous listener if any
if (unsubscribeTodos) unsubscribeTodos();
// Load user's todos in real-time from their subcollection
const q = query(
collection(db, "users", user.uid, "todos"),
orderBy("createdAt")
);
unsubscribeTodos = onSnapshot(q, (snapshot) => {
todoList.innerHTML = "";
snapshot.forEach((doc) => {
const data = doc.data();
const li = document.createElement("li");
li.textContent = data.title + (data.completed ? " ✅" : "");
//completed button
if (!data.completed) {
const completeBtn = document.createElement("button");
completeBtn.textContent = "Complete";
completeBtn.onclick = async () => {
await updateDoc(doc.ref, { completed: true });
};
li.appendChild(completeBtn);
}
todoList.appendChild(li);
});
});
} else {
todoSection.style.display = "none";
logoutBtn.style.display = "none";
authSection.style.display = "block";
loginForm.style.display = "block";
signupForm.style.display = "none";
showLoginBtn.classList.add("active");
showSignupBtn.classList.remove("active");
if (unsubscribeTodos) unsubscribeTodos();
todoList.innerHTML = "";
}
});