document.addEventListener("DOMContentLoaded", () => { // Obsługuje kliknięcie na przycisk "Usuń" document.body.addEventListener("click", async (e) => { const target = e.target as HTMLElement; 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; 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" }); 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; } }); });