import React, { useEffect, useState, useRef } from "react"; function App() { const [datum, setDatum] = useState("2025-05-14"); const [data, setData] = useState({ abwesenheiten: [], zeitslots: [] }); const [startzeit, setStartzeit] = useState(null); const [endzeit, setEndzeit] = useState(null); const [manualEndzeit, setManualEndzeit] = useState(""); const [showEndzeitInput, setShowEndzeitInput] = useState(false); // Gemeinsames Formular für neue Einträge const [showForm, setShowForm] = useState(false); const [isAbwesenheit, setIsAbwesenheit] = useState(true); const [newEntry, setNewEntry] = useState({ titel: "", start: "", ende: "", farbe: "#ccffcc" }); // Ref für das neue Eintragsformular (Hülle) const newEntryRef = useRef(null); // Ref für das Texteingabefeld im neuen Eintrag const newEntryInputRef = useRef(null); // Fokussieren des neuen Eintragsformulars und Scrollen bei Anzeige useEffect(() => { if (showForm && newEntryInputRef.current) { newEntryInputRef.current.focus(); newEntryInputRef.current.scrollIntoView({ behavior: "smooth", block: "center" }); } }, [showForm]); const [editAbwesenheitId, setEditAbwesenheitId] = useState(null); const [editZeitslotId, setEditZeitslotId] = useState(null); const [editForm, setEditForm] = useState({ titel: "", start: "", ende: "" }); const zeitachseRef = useRef(null); const [pixelsPerMinute, setPixelsPerMinute] = useState(1); const [manualStartzeit, setManualStartzeit] = useState(""); const [showStartzeitInput, setShowStartzeitInput] = useState(false); useEffect(() => { setPixelsPerMinute(1); }, []); const startEdit = (eintrag, typ) => { setEditForm({ titel: eintrag.titel, start: eintrag.start, ende: eintrag.ende }); if (typ === "abwesenheit") setEditAbwesenheitId(eintrag.id); if (typ === "zeitslot") setEditZeitslotId(eintrag.id); }; // Speichern-Funktionen für Edit und Neu const handleSaveEdit = async (typ) => { const url = typ === "abwesenheit" ? `http://localhost:3001/api/abwesenheit/${editAbwesenheitId}` : `http://localhost:3001/api/zeitslot/${editZeitslotId}`; await fetch(url, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(editForm), }); setEditAbwesenheitId(null); setEditZeitslotId(null); setEditForm({ titel: "", start: "", ende: "" }); reloadData(); }; const handleSaveNew = async () => { const endpoint = isAbwesenheit ? "abwesenheit" : "zeitslot"; await fetch(`http://localhost:3001/api/${endpoint}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ datum, ...newEntry }), }); setNewEntry({ titel: "", start: "", ende: "", farbe: "#ccffcc" }); setShowForm(false); reloadData(); }; const deleteEntry = async (id, typ) => { const url = `http://localhost:3001/api/${typ}/${id}`; await fetch(url, { method: "DELETE" }); reloadData(); if (typ === "abwesenheit") setEditAbwesenheitId(null); else setEditZeitslotId(null); }; // Daten laden useEffect(() => { fetch(`http://localhost:3001/api/data/${datum}`) .then((res) => res.json()) .then((data) => { setData(data); setStartzeit(data.startzeit); setEndzeit(data.endzeit); }); }, [datum]); const reloadData = () => { fetch(`http://localhost:3001/api/data/${datum}`) .then((res) => res.json()) .then((data) => { setData(data); setStartzeit(data.startzeit); setEndzeit(data.endzeit); }); }; const beginSetEndzeit = () => { const now = new Date(); const h = String(now.getHours()).padStart(2, "0"); const m = String(Math.floor(now.getMinutes() / 15) * 15).padStart(2, "0"); setManualEndzeit(`${h}:${m}`); setShowEndzeitInput(true); }; const saveManualEndzeit = async () => { await fetch("http://localhost:3001/api/endzeit", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ datum, zeit: manualEndzeit }), }); setShowEndzeitInput(false); reloadData(); }; const beginSetStartzeit = () => { const now = new Date(); const h = String(now.getHours()).padStart(2, "0"); const m = String(Math.floor(now.getMinutes() / 15) * 15).padStart(2, "0"); setManualStartzeit(`${h}:${m}`); setShowStartzeitInput(true); }; const saveManualStartzeit = async () => { await fetch("http://localhost:3001/api/startzeit", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ datum, zeit: manualStartzeit }), }); setShowStartzeitInput(false); reloadData(); }; return (