Add applying to event

This commit is contained in:
AleksDw
2025-06-01 17:13:47 +02:00
parent b440a0334c
commit 7e3759927f
5 changed files with 104 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js';
import { getEvent, getMyAccount, unhideElementById, getMyRegisteredEventIds } from './generalUseHelpers.js';
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
@@ -9,6 +9,7 @@ document.addEventListener("DOMContentLoaded", async () => {
var container = document.getElementById("mainContainer");
const modifyBtn = document.getElementById("editBtn");
const removeBtn = document.getElementById("removeBtn");
const applyBtn = document.getElementById("applyBtn");
var org_id: number = -1;
try {
@@ -28,11 +29,11 @@ document.addEventListener("DOMContentLoaded", async () => {
try {
if (eventId) thisEvent = await getEvent(eventId);
} catch (err) {
if (container !== null) container.innerHTML = `<p class="text-danger">To wydarzenie nie istnieje! <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
if (container !== null) container.innerHTML = `<p class="text-danger">To wydarzenie nie istnieje! <a href="/" style="color:#2898BD;">Powr<EFBFBD>t -></a></p>`;
}
if (thisEvent == null) {
if (container !== null) container.innerHTML = `<p class="text-danger">B³¹d we wczytywaniu wydarzenia. <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
if (container !== null) container.innerHTML = `<p class="text-danger">Błąd we wczytywaniu wydarzenia. <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
} else {
const titleText = document.getElementById( "titleText") as HTMLElement;
@@ -51,13 +52,20 @@ document.addEventListener("DOMContentLoaded", async () => {
organizerText.innerHTML = "Organized by: " + thisEvent.organisationName;
if (org_id == thisEvent.organisationId) {
// U¿ytkownik jest organizacj¹, która
// stworzy³a to wydarzenie
// Użytkownik jest organizacją, która
// stworzyła to wydarzenie
unhideElementById(document, "editBtn");
unhideElementById(document, "removeBtn");
} else if (org_id == -1) {
// U¿ytkownik jest wolontariuszem
unhideElementById(document, "applyBtn");
// Użytkownik jest wolontariuszem
const registeredIds = await getMyRegisteredEventIds();
const isRegistered = registeredIds.includes(Number(eventId));
if (isRegistered) {
unhideElementById(document, "leaveBtn");
} else {
unhideElementById(document, "applyBtn");
}
}
unhideElementById(document, "mainContainer");
@@ -76,7 +84,7 @@ document.addEventListener("DOMContentLoaded", async () => {
if (!confirmed) return;
try {
// Wysy³a ¿¹danie DELETE do API
// Wysyła żądanie DELETE do API
const response = await fetch(`/api/events/${eventId}`, {
method: "DELETE"
});
@@ -94,4 +102,32 @@ document.addEventListener("DOMContentLoaded", async () => {
});
}
});
if (applyBtn) {
applyBtn.addEventListener("click", async (e) => {
try {
const response = await fetch(`/api/events/join/${eventId}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
});
const result: {
success: boolean;
error_msg?: string;
} = await response.json();
if (result.success) {
(applyBtn as HTMLButtonElement).disabled = true;
(applyBtn as HTMLButtonElement).textContent = "Applied Succesfully";
} else {
alert(`Error: ${result.error_msg ?? "Unknown error occurred."}`);
}
} catch (error) {
console.error("Failed to apply:", error);
alert("Failed to apply due to a network or server error.");
}
});
}
});

View File

@@ -36,9 +36,20 @@ export async function getEvent(id: string): Promise<EventData> {
export async function getMyAccount(): Promise<MyAccount> {
const res = await fetch("/api/auth/my_account");
if (!res.ok) {
throw Error("U¿ytkownik niezalogowany!");
throw Error("U<EFBFBD>ytkownik niezalogowany!");
}
const data = await res.json();
return data;
}
export async function getMyRegisteredEventIds(): Promise<number[]> {
const res = await fetch("/api/auth/my_events");
if (!res.ok) {
throw Error("Użytkownik niezalogowany!");
}
const events = await res.json();
return events.map((event: { eventId: number }) => event.eventId);
}