Files
hermes/WebApp/wwwroot/js/eventCreate.js
eee4 5536a9ad7f 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
2025-05-19 00:43:15 +02:00

69 lines
2.7 KiB
JavaScript

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { getMyAccount, unhideElementById } from './generalUseHelpers.js';
function createEvent() {
return __awaiter(this, void 0, void 0, function* () {
// Pobieranie danych z formularza
const title = document.getElementById('title').value;
const location = document.getElementById('location').value;
const description = document.getElementById('description').value;
const eventDateRaw = document.getElementById('eventDate').value;
// Walidacja prostych pól
if (!title || !location || !eventDateRaw) {
alert("Uzupełnij wszystkie wymagane pola!");
return;
}
const eventDate = new Date(eventDateRaw).toISOString();
const payload = {
title,
location,
description,
eventDate,
};
try {
const response = yield fetch('/api/events', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!response.ok) {
const errorText = yield response.text();
throw new Error(errorText);
}
alert("Wydarzenie zostało utworzone!");
window.location.href = "/"; // Przekierowanie do strony głównej
}
catch (error) {
console.error("Błąd podczas tworzenia:", error);
alert("Nie udało się utworzyć wydarzenia: " + error);
}
});
}
document.addEventListener("DOMContentLoaded", () => {
const saveBtn = document.getElementById("saveBtn");
var user = yield 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();
});
}
});