mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 05:30:13 +01:00
feat: front-end overhaul. added search, editing, event view, validation
This commit is contained in:
@@ -42,7 +42,7 @@ async function createEvent() {
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
const saveBtn = document.getElementById("saveBtn");
|
||||
|
||||
var user = await getMyAccount();
|
||||
|
||||
@@ -3,30 +3,38 @@
|
||||
document.body.addEventListener("click", async (e) => {
|
||||
const target = e.target as HTMLElement;
|
||||
|
||||
if (!target.matches(".delete-btn")) return; // Sprawdza, czy kliknięto przycisk "Usuń"
|
||||
if (!target.matches(".mod-btn")) return; // Sprawdza, czy kliknięto przycisk "Usuń" lub "Edytuj"
|
||||
|
||||
const id = target.getAttribute("data-id"); // Pobiera ID wydarzenia
|
||||
if (!id) return;
|
||||
|
||||
const confirmed = confirm("Na pewno chcesz usunąć to wydarzenie?"); // Potwierdzenie usunięcia
|
||||
if (!confirmed) return;
|
||||
switch (target.id) {
|
||||
case "edit-btn":
|
||||
window.location.href = "/modify.html?event=" + id;
|
||||
break;
|
||||
case "remove-btn":
|
||||
const confirmed = confirm("Na pewno chcesz usunąć to wydarzenie?"); // Potwierdzenie usunięcia
|
||||
if (!confirmed) return;
|
||||
|
||||
try {
|
||||
// Wysyła żądanie DELETE do API
|
||||
const response = await fetch(`/api/events/${id}`, {
|
||||
method: "DELETE"
|
||||
});
|
||||
try {
|
||||
// Wysyła żądanie DELETE do API
|
||||
const response = await fetch(`/api/events/${id}`, {
|
||||
method: "DELETE"
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
// Usuwa kartę z DOM (bez przeładowania strony)
|
||||
const card = target.closest(".event-card");
|
||||
if (card) card.remove();
|
||||
} else {
|
||||
alert("Błąd podczas usuwania wydarzenia.");
|
||||
}
|
||||
} catch (err) {
|
||||
alert("Błąd połączenia z serwerem.");
|
||||
console.error(err);
|
||||
if (response.ok) {
|
||||
// Usuwa kartę z DOM (bez przeładowania strony)
|
||||
const card = target.closest(".event-card");
|
||||
if (card) card.remove();
|
||||
} else {
|
||||
alert("Błąd podczas usuwania wydarzenia.");
|
||||
}
|
||||
} catch (err) {
|
||||
alert("Błąd połączenia z serwerem.");
|
||||
console.error(err);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
@@ -2,25 +2,46 @@
|
||||
|
||||
var isAscending: boolean = false;
|
||||
|
||||
function toggleListSortOrder() {
|
||||
function toggleListSortOrder(org_id: number) {
|
||||
isAscending = !isAscending;
|
||||
loadEvents();
|
||||
loadEvents(org_id);
|
||||
}
|
||||
|
||||
async function getEvents() {
|
||||
const res = await fetch("/api/events" + (isAscending ? "?sort=asc" : ""));
|
||||
if (!res.ok) throw new Error("Błąd pobierania wydarzeń");
|
||||
async function getEvents(titleOrDescription?: string) {
|
||||
|
||||
var res: Response;
|
||||
|
||||
if (titleOrDescription == null) {
|
||||
res = await fetch("/api/events" + (isAscending ? "?sort=asc" : ""));
|
||||
if (!res.ok) throw new Error("Błąd pobierania wydarzeń");
|
||||
} else {
|
||||
const payload = {
|
||||
titleOrDescription
|
||||
};
|
||||
res = await fetch('/api/events/search', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
if (!res.ok) throw new Error("Błąd wyszukiwania wydarzeń");
|
||||
}
|
||||
|
||||
const events = await res.json();
|
||||
return events;
|
||||
}
|
||||
|
||||
async function loadEvents() {
|
||||
async function loadEvents(org_id: number, evs?: Promise<any>) {
|
||||
const container = document.getElementById("eventList");
|
||||
if (!container) return;
|
||||
var events: any;
|
||||
|
||||
try {
|
||||
var events = await getEvents();
|
||||
if (evs == null) {
|
||||
events = await getEvents();
|
||||
}
|
||||
else {
|
||||
events = await evs;
|
||||
}
|
||||
|
||||
if (events.length === 0) {
|
||||
container.innerHTML = "<p class='text-muted'>Brak wydarzeń do wyświetlenia.</p>";
|
||||
@@ -33,12 +54,23 @@ async function loadEvents() {
|
||||
for (const ev of events) {
|
||||
const card = document.createElement("div");
|
||||
card.className = "event-card filled";
|
||||
//card.innerHTML = `
|
||||
// <span>${ev.title}</span>`
|
||||
// Do odkomentowania kiedy widok podglądu wydarzeń będzie gotowy
|
||||
card.innerHTML = `
|
||||
<span>${ev.title}</span>
|
||||
<button class="remove-btn delete-btn" data-id="${ev.eventId}">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" height="30px" viewBox="0 -960 960 960" width="30px" fill="#FFFFFF"><path d="M280-440h400v-80H280v80ZM480-80q-83 0-156-31.5T197-197q-54-54-85.5-127T80-480q0-83 31.5-156T197-763q54-54 127-85.5T480-880q83 0 156 31.5T763-763q54 54 85.5 127T880-480q0 83-31.5 156T763-197q-54 54-127 85.5T480-80Zm0-80q134 0 227-93t93-227q0-134-93-227t-227-93q-134 0-227 93t-93 227q0 134 93 227t227 93Zm0-320Z"/></svg>
|
||||
</button>
|
||||
`;
|
||||
<span><a href="/view.html?event=${ev.eventId}" style="color: #2898BD">${ev.title}</a></span>`
|
||||
if (org_id == ev.organisationId) {
|
||||
card.innerHTML += `
|
||||
<div>
|
||||
<button class="edit-btn mod-btn" data-id="${ev.eventId}" id="edit-btn">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#FFFFFF"><path d="M200-200h57l391-391-57-57-391 391v57Zm-80 80v-170l528-527q12-11 26.5-17t30.5-6q16 0 31 6t26 18l55 56q12 11 17.5 26t5.5 30q0 16-5.5 30.5T817-647L290-120H120Zm640-584-56-56 56 56Zm-141 85-28-29 57 57-29-28Z"/></svg>
|
||||
</button>
|
||||
<button class="remove-btn mod-btn" data-id="${ev.eventId}" id="remove-btn">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" height="30px" viewBox="0 -960 960 960" width="30px" fill="#FFFFFF"><path d="M280-440h400v-80H280v80ZM480-80q-83 0-156-31.5T197-197q-54-54-85.5-127T80-480q0-83 31.5-156T197-763q54-54 127-85.5T480-880q83 0 156 31.5T763-763q54 54 85.5 127T880-480q0 83-31.5 156T763-197q-54 54-127 85.5T480-80Zm0-80q134 0 227-93t93-227q0-134-93-227t-227-93q-134 0-227 93t-93 227q0 134 93 227t227 93Zm0-320Z"/></svg>
|
||||
</button>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
container.appendChild(card);
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -50,11 +82,13 @@ async function loadEvents() {
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
|
||||
var user = await getMyAccount();
|
||||
var org_id: number = -1;
|
||||
|
||||
if (user) {
|
||||
if (user.isOrganisation) {
|
||||
unhideElementById(document, "mainContainer");
|
||||
unhideElementById(document, "addnewevent-btn");
|
||||
org_id = user.organisationId;
|
||||
}
|
||||
unhideElementById(document, "logout-btn");
|
||||
} else {
|
||||
@@ -62,10 +96,19 @@ document.addEventListener("DOMContentLoaded", async () => {
|
||||
unhideElementById(document, "signin-btn");
|
||||
}
|
||||
|
||||
loadEvents();
|
||||
loadEvents(org_id);
|
||||
// listen for clicks
|
||||
const listSortToggleButton = document.getElementById("list-sort-btn");
|
||||
if (listSortToggleButton) {
|
||||
listSortToggleButton.addEventListener("click", toggleListSortOrder);
|
||||
listSortToggleButton.addEventListener("click", () => toggleListSortOrder(org_id));
|
||||
}
|
||||
// and for enter in search bar
|
||||
const searchBar = document.getElementById('searchbar') as HTMLInputElement;
|
||||
searchBar.addEventListener('keydown', (event) => {
|
||||
if (event.key === 'Enter') {
|
||||
console.log('Enter key pressed!');
|
||||
var searchResults = getEvents(searchBar.value);
|
||||
loadEvents(org_id, searchResults);
|
||||
}
|
||||
})
|
||||
});
|
||||
@@ -68,7 +68,7 @@ document.addEventListener("DOMContentLoaded", async () => {
|
||||
{
|
||||
saveBtn.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
createEvent();
|
||||
modifyEvent();
|
||||
});
|
||||
}
|
||||
if (eventId !== null && container !== null) {
|
||||
|
||||
95
WebApp/ts/eventView.ts
Normal file
95
WebApp/ts/eventView.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js';
|
||||
|
||||
const queryString = window.location.search;
|
||||
const urlParams = new URLSearchParams(queryString);
|
||||
const eventId = urlParams.get('event');
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
|
||||
var container = document.getElementById("mainContainer");
|
||||
var user = await getMyAccount();
|
||||
var org_id: number = -1;
|
||||
const modifyBtn = document.getElementById( "editBtn");
|
||||
const removeBtn = document.getElementById("removeBtn");
|
||||
|
||||
if (user) {
|
||||
if (user.isOrganisation) {
|
||||
org_id = user.organisationId;
|
||||
}
|
||||
unhideElementById(document, "logout-btn");
|
||||
} else {
|
||||
unhideElementById(document, "joinnow-btn");
|
||||
unhideElementById(document, "signin-btn");
|
||||
}
|
||||
|
||||
var thisEvent = null;
|
||||
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 (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>`;
|
||||
} else {
|
||||
|
||||
const titleText = document.getElementById( "titleText") as HTMLElement;
|
||||
const locationText = document.getElementById( "locationText") as HTMLElement;
|
||||
const descText = document.getElementById( "descText") as HTMLElement;
|
||||
const dateText = document.getElementById( "dateText") as HTMLElement;
|
||||
const organizerText = document.getElementById("organizerText") as HTMLElement;
|
||||
const newdateText = new Date(thisEvent.eventDate).toLocaleDateString('pl-PL');
|
||||
const newtimeText = new Date(thisEvent.eventDate).toLocaleTimeString('pl-PL');
|
||||
|
||||
|
||||
titleText.innerHTML = thisEvent.title + ` (#${eventId})`;
|
||||
locationText.innerHTML = "Place: " + thisEvent.location;
|
||||
descText.innerHTML = thisEvent.description;
|
||||
dateText.innerHTML = "When: " + newdateText + " " + newtimeText; //thisEvent.eventDate;
|
||||
organizerText.innerHTML = "Organized by: " + thisEvent.organisationName;
|
||||
|
||||
if (org_id == thisEvent.organisationId) {
|
||||
// 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");
|
||||
}
|
||||
|
||||
unhideElementById(document, "mainContainer");
|
||||
|
||||
}
|
||||
|
||||
if (modifyBtn) {
|
||||
modifyBtn.addEventListener("click", (e) => {
|
||||
window.location.href = "/modify.html?event=" + eventId;
|
||||
});
|
||||
}
|
||||
|
||||
if (removeBtn) {
|
||||
removeBtn.addEventListener("click", async (e) => {
|
||||
const confirmed = confirm("Really delete?");
|
||||
if (!confirmed) return;
|
||||
|
||||
try {
|
||||
// Wysy³a ¿¹danie DELETE do API
|
||||
const response = await fetch(`/api/events/${id}`, {
|
||||
method: "DELETE"
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
alert("Event deleted.");
|
||||
window.location.href = "/";
|
||||
} else {
|
||||
alert("Couldn't delete event.");
|
||||
}
|
||||
} catch (err) {
|
||||
alert("Couldn't connect.");
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -3,6 +3,8 @@ interface EventData {
|
||||
location: string;
|
||||
description: string;
|
||||
eventDate: string;
|
||||
organisationName: string,
|
||||
organisationId: number
|
||||
}
|
||||
|
||||
interface MyAccount {
|
||||
@@ -12,13 +14,13 @@ interface MyAccount {
|
||||
lastName: string;
|
||||
createdAt: string;
|
||||
isOrganisation: boolean;
|
||||
organisationId: number;
|
||||
}
|
||||
|
||||
export async function unhideElementById(document: Document, e: string) {
|
||||
var element = document.getElementById(e);
|
||||
if (element) {
|
||||
element.classList.remove('hidden-before-load');
|
||||
console.log(element.classList);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user