Add login, save and invite features
This commit is contained in:
1 parent
66c51e9238
commit
3208977b9a
7 files changed
+422
-15
No files matched your search
@@ -1,7 +1,112 @@
|
||||
import { getEvents } from "./services/eventService.js";
|
||||
import { renderEventList } from "./ui/eventList.js";
|
||||
import { getFilters } from "./ui/filters.js";
|
||||
import { login, register } from "./auth.js";
|
||||
|
||||
// =========================
|
||||
// STATE
|
||||
// =========================
|
||||
let currentUser = null;
|
||||
let currentPassword = null;
|
||||
|
||||
window.currentUser = null;
|
||||
window.currentPassword = null;
|
||||
|
||||
// =========================
|
||||
// AUTH ELEMENTS
|
||||
// =========================
|
||||
const loginBtn = document.querySelector("#login-btn");
|
||||
const registerBtn = document.querySelector("#register-btn");
|
||||
|
||||
// =========================
|
||||
// NAVIGATION ELEMENTS
|
||||
// =========================
|
||||
const navEvents = document.querySelector("#nav-events");
|
||||
const navSaved = document.querySelector("#nav-my-events");
|
||||
const navInv = document.querySelector("#nav-invitations");
|
||||
|
||||
// =========================
|
||||
// SECTIONS
|
||||
// =========================
|
||||
const searchSection = document.querySelector("#search-section");
|
||||
const eventsSection = document.querySelector("#events-section");
|
||||
const savedSection = document.querySelector("#saved-section");
|
||||
const invSection = document.querySelector("#invitations-section");
|
||||
|
||||
// =========================
|
||||
// AUTH UI
|
||||
// =========================
|
||||
const authArea = document.querySelector("#auth-area");
|
||||
const userArea = document.querySelector("#user-area");
|
||||
const userNameEl = document.querySelector("#user-name");
|
||||
const logoutBtn = document.querySelector("#logout-btn");
|
||||
|
||||
// =========================
|
||||
// LOGIN
|
||||
// =========================
|
||||
loginBtn.addEventListener("click", async () => {
|
||||
|
||||
const username = document.querySelector("#username").value;
|
||||
const password = document.querySelector("#password").value;
|
||||
|
||||
const success = await login(username, password);
|
||||
|
||||
if (success) {
|
||||
currentUser = username;
|
||||
currentPassword = password;
|
||||
|
||||
window.currentUser = username;
|
||||
window.currentPassword = password;
|
||||
|
||||
// UI switch
|
||||
authArea.classList.add("d-none");
|
||||
userArea.classList.remove("d-none");
|
||||
|
||||
userNameEl.textContent = `👤 ${username}`;
|
||||
|
||||
navSaved.classList.remove("d-none");
|
||||
navInv.classList.remove("d-none");
|
||||
|
||||
} else {
|
||||
alert("Login failed");
|
||||
}
|
||||
});
|
||||
|
||||
// =========================
|
||||
// REGISTER
|
||||
// =========================
|
||||
registerBtn.addEventListener("click", async () => {
|
||||
|
||||
const username = document.querySelector("#username").value;
|
||||
|
||||
const data = await register(username);
|
||||
|
||||
alert(`User created. Password: ${data.password}`);
|
||||
});
|
||||
|
||||
// =========================
|
||||
// LOGOUT
|
||||
// =========================
|
||||
logoutBtn.addEventListener("click", () => {
|
||||
|
||||
currentUser = null;
|
||||
currentPassword = null;
|
||||
|
||||
window.currentUser = null;
|
||||
window.currentPassword = null;
|
||||
|
||||
authArea.classList.remove("d-none");
|
||||
userArea.classList.add("d-none");
|
||||
|
||||
navSaved.classList.add("d-none");
|
||||
navInv.classList.add("d-none");
|
||||
|
||||
showSection("events");
|
||||
});
|
||||
|
||||
// =========================
|
||||
// SEARCH
|
||||
// =========================
|
||||
const button = document.querySelector("#load-events");
|
||||
const container = document.querySelector("#event-list");
|
||||
const cityInput = document.querySelector("#city-input");
|
||||
@@ -30,6 +135,9 @@ async function handleSearch() {
|
||||
renderEventList(filteredEvents, container);
|
||||
}
|
||||
|
||||
// =========================
|
||||
// FILTERS
|
||||
// =========================
|
||||
function applyFilters(events, dateFrom, dateTo, category) {
|
||||
|
||||
return events.filter(event => {
|
||||
@@ -43,4 +151,118 @@ function applyFilters(events, dateFrom, dateTo, category) {
|
||||
|
||||
return matchDateFrom && matchDateTo && matchCategory;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// =========================
|
||||
// NAVIGATION LOGIC
|
||||
// =========================
|
||||
navEvents.addEventListener("click", () => {
|
||||
showSection("events");
|
||||
});
|
||||
|
||||
navSaved.addEventListener("click", () => {
|
||||
showSection("saved");
|
||||
loadSavedEvents();
|
||||
});
|
||||
|
||||
navInv.addEventListener("click", () => {
|
||||
showSection("invitations");
|
||||
loadInvitations();
|
||||
});
|
||||
|
||||
function showSection(section) {
|
||||
|
||||
searchSection.classList.add("d-none");
|
||||
eventsSection.classList.add("d-none");
|
||||
savedSection.classList.add("d-none");
|
||||
invSection.classList.add("d-none");
|
||||
|
||||
if (section === "events") {
|
||||
searchSection.classList.remove("d-none");
|
||||
eventsSection.classList.remove("d-none");
|
||||
}
|
||||
|
||||
if (section === "saved") {
|
||||
savedSection.classList.remove("d-none");
|
||||
}
|
||||
|
||||
if (section === "invitations") {
|
||||
invSection.classList.remove("d-none");
|
||||
}
|
||||
}
|
||||
|
||||
// =========================
|
||||
// SAVED EVENTS
|
||||
// =========================
|
||||
function loadSavedEvents() {
|
||||
|
||||
const saved = JSON.parse(localStorage.getItem("savedEvents") || "[]");
|
||||
const container = document.querySelector("#saved-list");
|
||||
|
||||
container.innerHTML = "";
|
||||
|
||||
if (saved.length === 0) {
|
||||
container.innerHTML = "<p>No saved events yet.</p>";
|
||||
return;
|
||||
}
|
||||
|
||||
saved.forEach(event => {
|
||||
const div = document.createElement("div");
|
||||
div.textContent = event.name;
|
||||
container.appendChild(div);
|
||||
});
|
||||
}
|
||||
|
||||
// =========================
|
||||
// INVITATIONS
|
||||
// =========================
|
||||
async function loadInvitations() {
|
||||
|
||||
if (!currentUser) return;
|
||||
|
||||
const res = await fetch("http://localhost:3000/api/invitation", {
|
||||
headers: {
|
||||
"X-Username": currentUser
|
||||
}
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
const container = document.querySelector("#invitation-list");
|
||||
|
||||
container.innerHTML = "";
|
||||
|
||||
if (data.length === 0) {
|
||||
container.innerHTML = "<p>No invitations.</p>";
|
||||
return;
|
||||
}
|
||||
|
||||
data.forEach(inv => {
|
||||
|
||||
const div = document.createElement("div");
|
||||
|
||||
div.innerHTML = `
|
||||
<p><strong>${inv.fromUser}</strong> invited you to <em>${inv.eventName}</em></p>
|
||||
<button onclick="accept(${inv.id})">Accept</button>
|
||||
<button onclick="decline(${inv.id})">Decline</button>
|
||||
`;
|
||||
|
||||
container.appendChild(div);
|
||||
});
|
||||
}
|
||||
|
||||
// =========================
|
||||
// ACCEPT / DECLINE
|
||||
// =========================
|
||||
window.accept = async (id) => {
|
||||
await fetch(`http://localhost:3000/api/invitation/${id}/accept`, {
|
||||
method: "POST"
|
||||
});
|
||||
loadInvitations();
|
||||
};
|
||||
|
||||
window.decline = async (id) => {
|
||||
await fetch(`http://localhost:3000/api/invitation/${id}/decline`, {
|
||||
method: "POST"
|
||||
});
|
||||
loadInvitations();
|
||||
};
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
const BASE_URL = "http://localhost:3000/api";
|
||||
|
||||
export async function register(username) {
|
||||
|
||||
const res = await fetch(`${BASE_URL}/user`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({ username })
|
||||
});
|
||||
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function login(username, password) {
|
||||
|
||||
const res = await fetch(`${BASE_URL}/user`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-Username": username,
|
||||
"X-Password": password
|
||||
}
|
||||
});
|
||||
|
||||
return res.status === 201;
|
||||
}
|
||||
@@ -33,5 +33,82 @@ export function createEventCard(event) {
|
||||
|
||||
article.append(title, date, venue);
|
||||
|
||||
// =========================
|
||||
// BUTTON CONTAINER
|
||||
// =========================
|
||||
const buttonContainer = document.createElement("div");
|
||||
buttonContainer.className = "d-flex gap-2 mt-2";
|
||||
|
||||
// =========================
|
||||
// SAVE BUTTON
|
||||
// =========================
|
||||
const saveBtn = document.createElement("button");
|
||||
saveBtn.textContent = "Save";
|
||||
saveBtn.className = "btn btn-outline-primary btn-sm";
|
||||
|
||||
saveBtn.addEventListener("click", () => {
|
||||
|
||||
if (!window.currentUser) {
|
||||
alert("Please login to save events");
|
||||
return;
|
||||
}
|
||||
|
||||
const saved = JSON.parse(localStorage.getItem("savedEvents") || "[]");
|
||||
|
||||
// prevent duplicates
|
||||
if (saved.find(e => e.id === event.id)) {
|
||||
alert("Event already saved");
|
||||
return;
|
||||
}
|
||||
|
||||
saved.push(event);
|
||||
localStorage.setItem("savedEvents", JSON.stringify(saved));
|
||||
|
||||
alert("Event saved!");
|
||||
});
|
||||
|
||||
saveBtn.disabled = !window.currentUser;
|
||||
|
||||
// =========================
|
||||
// INVITE BUTTON
|
||||
// =========================
|
||||
const inviteBtn = document.createElement("button");
|
||||
inviteBtn.textContent = "Invite";
|
||||
inviteBtn.className = "btn btn-primary btn-sm";
|
||||
|
||||
inviteBtn.addEventListener("click", async () => {
|
||||
|
||||
if (!window.currentUser) {
|
||||
alert("Please login to invite users");
|
||||
return;
|
||||
}
|
||||
|
||||
const toUser = prompt("Enter username to invite:");
|
||||
if (!toUser) return;
|
||||
|
||||
await fetch("http://localhost:3000/api/invitation", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-Username": window.currentUser
|
||||
},
|
||||
body: JSON.stringify({
|
||||
toUser,
|
||||
eventId: event.id,
|
||||
eventName: event.name
|
||||
})
|
||||
});
|
||||
|
||||
alert("Invitation sent!");
|
||||
});
|
||||
|
||||
inviteBtn.disabled = !window.currentUser;
|
||||
|
||||
// =========================
|
||||
// APPEND BUTTONS
|
||||
// =========================
|
||||
buttonContainer.append(saveBtn, inviteBtn);
|
||||
article.appendChild(buttonContainer);
|
||||
|
||||
return article;
|
||||
}
|
||||
Reference in new issue
Block a user