-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
152 lines (121 loc) 路 5.36 KB
/
Copy pathauth.js
File metadata and controls
152 lines (121 loc) 路 5.36 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
import { apiGet, apiPost, getCurrentUser, setCurrentUser, clearCurrentUser } from "./api.js";
const overlay = document.getElementById("authOverlay");
const openModal = () => overlay.classList.add("open");
const closeModal = () => overlay.classList.remove("open");
document.getElementById("navLoginBtn").addEventListener("click", () => { showTab("login"); openModal(); });
document.getElementById("navSignupBtn").addEventListener("click", () => { showTab("signup"); openModal(); });
document.getElementById("heroPostBtn").addEventListener("click", () => { showTab("signup"); openModal(); });
document.getElementById("heroBrowseBtn").addEventListener("click", () => { showTab("signup"); openModal(); });
document.getElementById("modalClose").addEventListener("click", closeModal);
overlay.addEventListener("click", (e) => { if (e.target === overlay) closeModal(); });
const tabLogin = document.getElementById("tabLogin");
const tabSignup = document.getElementById("tabSignup");
const loginForm = document.getElementById("loginForm");
const signupForm = document.getElementById("signupForm");
function showTab(which) {
const isLogin = which === "login";
tabLogin.classList.toggle("active", isLogin);
tabSignup.classList.toggle("active", !isLogin);
loginForm.style.display = isLogin ? "block" : "none";
signupForm.style.display = isLogin ? "none" : "block";
}
tabLogin.addEventListener("click", () => showTab("login"));
tabSignup.addEventListener("click", () => showTab("signup"));
signupForm.addEventListener("submit", async (e) => {
e.preventDefault();
const errorEl = document.getElementById("signupError");
errorEl.textContent = "";
const payload = {
action: "signup",
name: document.getElementById("signupName").value.trim(),
email: document.getElementById("signupEmail").value.trim(),
password: document.getElementById("signupPassword").value,
locality: document.getElementById("signupLocality").value.trim()
};
const submitBtn = signupForm.querySelector("button[type=submit]");
submitBtn.disabled = true;
submitBtn.textContent = "Creating account...";
const result = await apiPost(payload);
submitBtn.disabled = false;
submitBtn.textContent = "Create account";
if (result.error) {
errorEl.textContent = result.error;
return;
}
setCurrentUser(result);
window.location.href = "dashboard.html";
});
loginForm.addEventListener("submit", async (e) => {
e.preventDefault();
const errorEl = document.getElementById("loginError");
errorEl.textContent = "";
const payload = {
action: "login",
email: document.getElementById("loginEmail").value.trim(),
password: document.getElementById("loginPassword").value
};
const submitBtn = loginForm.querySelector("button[type=submit]");
submitBtn.disabled = true;
submitBtn.textContent = "Logging in...";
const result = await apiPost(payload);
submitBtn.disabled = false;
submitBtn.textContent = "Log in";
if (result.error) {
errorEl.textContent = result.error;
return;
}
setCurrentUser(result);
window.location.href = "dashboard.html";
});
document.getElementById("logoutBtn").addEventListener("click", () => {
clearCurrentUser();
window.location.reload();
});
async function loadHeroRequest() {
const requests = await apiGet({ action: "getRequests" });
if (requests.error) return;
const candidates = requests.filter(
(r) => r.status === "open" && r.title.trim().length >= 8 && r.description.trim().length >= 15
);
if (candidates.length === 0) {
document.getElementById("heroEmptyCard").style.display = "block";
return;
}
candidates.sort((a, b) => {
const aVol = a.volunteerDetails ? a.volunteerDetails.length : 0;
const bVol = b.volunteerDetails ? b.volunteerDetails.length : 0;
if (bVol !== aVol) return bVol - aVol;
return new Date(b.createdAt) - new Date(a.createdAt);
});
const req = candidates[0];
const joinedCount = req.volunteerDetails ? req.volunteerDetails.length : 0;
const pct = Math.min(100, Math.round((joinedCount / req.peopleNeeded) * 100));
document.getElementById("heroReqMeta").textContent = `馃搷 ${escapeHtml(req.createdByName)}'s request · ${relativeTime(req.createdAt)}`.replace("·", "路");
document.getElementById("heroReqPoints").textContent = `+${req.pointsPerVolunteer} pts`;
document.getElementById("heroReqTitle").textContent = req.title;
document.getElementById("heroReqDesc").textContent = req.description;
document.getElementById("heroReqCount").textContent = `${joinedCount} / ${req.peopleNeeded} volunteers`;
document.getElementById("heroReqFill").style.width = `${pct}%`;
document.getElementById("heroRequestCard").style.display = "block";
}
function relativeTime(isoString) {
const diffMs = Date.now() - new Date(isoString).getTime();
const mins = Math.round(diffMs / 60000);
if (mins < 1) return "just now";
if (mins < 60) return `${mins}m ago`;
const hours = Math.round(mins / 60);
if (hours < 24) return `${hours}h ago`;
return `${Math.round(hours / 24)}d ago`;
}
function escapeHtml(str) {
const div = document.createElement("div");
div.textContent = str;
return div.innerHTML;
}
loadHeroRequest();
const user = getCurrentUser();
if (user) {
document.getElementById("signedInName").textContent = user.name;
document.getElementById("signedInPoints").textContent = user.socialPoints;
document.getElementById("signedInBanner").style.display = "flex";
}