Files
hermes/WebApp/ts/eventModify.ts
eee4 9de5c85120 feat: image support
also translated some strings back into english
2025-06-02 04:37:54 +02:00

107 lines
3.7 KiB
TypeScript

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 imageURL = (document.getElementById('imageURL') as HTMLInputElement).value;
const eventDateRaw = (document.getElementById('eventDate') as HTMLInputElement).value;
// Walidacja prostych pól
if (!title || !location || !eventDateRaw)
{
alert("Please fill out all of the required fields!");
return;
}
const eventDate = new Date(eventDateRaw).toISOString();
const payload = {
title,
location,
imageURL,
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("Event modified!");
window.location.href = "/"; // Przekierowanie do strony głównej
} catch (error) {
console.error("Error occurred while trying to modify event:", error);
alert("Couldn't modify event, an error occurred: " + error);
}
}
document.addEventListener("DOMContentLoaded", async () => {
var container = document.getElementById("mainContainer");
const saveBtn = document.getElementById("saveBtn");
try {
var user = await getMyAccount();
if (user) {
if (user.isOrganisation) {
unhideElementById(document, "mainContainer");
}
unhideElementById(document, "logout-btn");
}
} catch {
unhideElementById(document, "joinnow-btn");
unhideElementById(document, "signin-btn");
}
if (saveBtn)
{
saveBtn.addEventListener("click", (e) => {
e.preventDefault();
modifyEvent();
});
}
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;
const imageInput = document.getElementById( 'imageURL') as HTMLInputElement;
var ev = await getEvent(eventId);
if (ev === null) {
container.innerHTML = "<p class='text-muted'>Failed to load event data.</p>";
return;
} else {
titleInput.value = ev.title || '';
locationInput.value = ev.location || '';
descriptionInput.value = ev.description || '';
dateInput.value = ev.eventDate.slice(0, 16) || '';
imageInput.value = ev.imageURL || '';
}
} catch (err) {
console.log(err);
container.innerHTML = `<p class="text-danger">` + err + `</p>`;
}
}
});