mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
fix: front-end improvements
makes some fields visible and other hidden depending on the result of some API calls, e.g. visitor is logged in -> show "Log out" button
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
console.log("TypeScript działa!");
|
||||
import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js';
|
||||
|
||||
async function createEvent() {
|
||||
// Pobieranie danych z formularza
|
||||
@@ -44,6 +44,18 @@ async function createEvent() {
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const saveBtn = document.getElementById("saveBtn");
|
||||
|
||||
var user = await getMyAccount();
|
||||
if (user) {
|
||||
if (user.isOrganisation) {
|
||||
unhideElementById(document, "mainContainer");
|
||||
}
|
||||
unhideElementById(document, "logout-btn");
|
||||
} else {
|
||||
unhideElementById(document, "joinnow-btn");
|
||||
unhideElementById(document, "signin-btn");
|
||||
}
|
||||
|
||||
if (saveBtn) {
|
||||
saveBtn.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
var isAscending: boolean = false;
|
||||
import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js';
|
||||
|
||||
var isAscending: boolean = false;
|
||||
|
||||
function toggleListSortOrder() {
|
||||
isAscending = !isAscending;
|
||||
loadEvents();
|
||||
}
|
||||
|
||||
async function getEvents() {
|
||||
const res = await fetch("/api/events" + (isAscending ? "?sort=asc" : ""));
|
||||
if (!res.ok) throw new Error("Błąd pobierania wydarzeń");
|
||||
|
||||
const events = await res.json();
|
||||
return events;
|
||||
}
|
||||
|
||||
async function loadEvents() {
|
||||
const container = document.getElementById("eventList");
|
||||
if (!container) return;
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/events" + (isAscending ? "?sort=asc" : ""));
|
||||
if (!res.ok) throw new Error("Błąd pobierania wydarzeń");
|
||||
|
||||
const events = await res.json();
|
||||
var events = await getEvents();
|
||||
|
||||
if (events.length === 0) {
|
||||
container.innerHTML = "<p class='text-muted'>Brak wydarzeń do wyświetlenia.</p>";
|
||||
@@ -41,6 +48,20 @@ async function loadEvents() {
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
|
||||
var user = await getMyAccount();
|
||||
|
||||
if (user) {
|
||||
if (user.isOrganisation) {
|
||||
unhideElementById(document, "mainContainer");
|
||||
unhideElementById(document, "addnewevent-btn");
|
||||
}
|
||||
unhideElementById(document, "logout-btn");
|
||||
} else {
|
||||
unhideElementById(document, "joinnow-btn");
|
||||
unhideElementById(document, "signin-btn");
|
||||
}
|
||||
|
||||
loadEvents();
|
||||
// listen for clicks
|
||||
const listSortToggleButton = document.getElementById("list-sort-btn");
|
||||
|
||||
99
WebApp/ts/eventModify.ts
Normal file
99
WebApp/ts/eventModify.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js';
|
||||
|
||||
const queryString = window.location.search;
|
||||
const urlParams = new URLSearchParams(queryString);
|
||||
const eventId = urlParams.get('event');
|
||||
|
||||
async function modifyEvent()
|
||||
{
|
||||
// Pobieranie danych z formularza
|
||||
const title = (document.getElementById('title') as HTMLInputElement).value;
|
||||
const location = (document.getElementById('location') as HTMLInputElement).value;
|
||||
const description = (document.getElementById('description') as HTMLTextAreaElement).value;
|
||||
const eventDateRaw = (document.getElementById('eventDate') as HTMLInputElement).value;
|
||||
|
||||
// Walidacja prostych pól
|
||||
if (!title || !location || !eventDateRaw)
|
||||
{
|
||||
alert("Należy uzupełnić wszystkie pola!");
|
||||
return;
|
||||
}
|
||||
|
||||
const eventDate = new Date(eventDateRaw).toISOString();
|
||||
|
||||
const payload = {
|
||||
title,
|
||||
location,
|
||||
description,
|
||||
eventDate,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
const response = await fetch('/api/events/' + eventId, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
if (!response.ok)
|
||||
{
|
||||
const errorText = await response.text();
|
||||
throw new Error(errorText);
|
||||
}
|
||||
|
||||
alert("Wydarzenie zmodyfikowane!");
|
||||
window.location.href = "/"; // Przekierowanie do strony głównej
|
||||
} catch (error) {
|
||||
console.error("Błąd podczas modyfikowania:", error);
|
||||
alert("Nie udało się zmodyfikować wydarzenia: " + error);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
var container = document.getElementById("mainContainer");
|
||||
const saveBtn = document.getElementById("saveBtn");
|
||||
var user = await getMyAccount();
|
||||
if (user) {
|
||||
if (user.isOrganisation) {
|
||||
unhideElementById(document, "mainContainer");
|
||||
}
|
||||
unhideElementById(document, "logout-btn");
|
||||
} else {
|
||||
unhideElementById(document, "joinnow-btn");
|
||||
unhideElementById(document, "signin-btn");
|
||||
}
|
||||
|
||||
if (saveBtn)
|
||||
{
|
||||
saveBtn.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
createEvent();
|
||||
});
|
||||
}
|
||||
if (eventId !== null && container !== null) {
|
||||
|
||||
try {
|
||||
const titleInput = document.getElementById( 'title') as HTMLInputElement;
|
||||
const locationInput = document.getElementById( 'location') as HTMLInputElement;
|
||||
const descriptionInput = document.getElementById('description') as HTMLInputElement;
|
||||
const dateInput = document.getElementById( 'eventDate') as HTMLInputElement;
|
||||
var ev = await getEvent(eventId);
|
||||
|
||||
if (ev === null) {
|
||||
container.innerHTML = "<p class='text-muted'>Brak wydarzeń do wyświetlenia.</p>";
|
||||
return;
|
||||
} else {
|
||||
titleInput.value = ev.title || '';
|
||||
locationInput.value = ev.location || '';
|
||||
descriptionInput.value = ev.description || '';
|
||||
dateInput.value = ev.eventDate.slice(0, 16) || '';
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
container.innerHTML = `<p class="text-danger">` + err + `</p>`;
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
42
WebApp/ts/generalUseHelpers.ts
Normal file
42
WebApp/ts/generalUseHelpers.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
interface EventData {
|
||||
title: string;
|
||||
location: string;
|
||||
description: string;
|
||||
eventDate: string;
|
||||
}
|
||||
|
||||
interface MyAccount {
|
||||
userId: number;
|
||||
email: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
createdAt: string;
|
||||
isOrganisation: boolean;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getEvent(id: string): Promise<EventData> {
|
||||
const res = await fetch("/api/events/" + id);
|
||||
if (!res.ok) {
|
||||
throw Error("To wydarzenie nie istnieje");
|
||||
}
|
||||
const events = await res.json();
|
||||
return events;
|
||||
}
|
||||
|
||||
export async function getMyAccount(): Promise<MyAccount> {
|
||||
const res = await fetch("/api/auth/my_account");
|
||||
if (!res.ok) {
|
||||
throw Error("U¿ytkownik niezalogowany!");
|
||||
}
|
||||
const data = await res.json();
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user