diff --git a/admin/src/components/API/Landingpage.tsx b/admin/src/components/API/Landingpage.tsx index a32d66e..51fd700 100644 --- a/admin/src/components/API/Landingpage.tsx +++ b/admin/src/components/API/Landingpage.tsx @@ -1,210 +1,178 @@ -import React from "react"; -import { useEffect } from "react"; -import { useState } from "react"; -import { Spinner, Text, VStack, Box } from "@chakra-ui/react"; -import { Table, Heading } from "@chakra-ui/react"; +import React, { useEffect, useState } from "react"; +import { + Spinner, + Text, + VStack, + Table, + Heading, + HStack, + IconButton, +} from "@chakra-ui/react"; +import { Tooltip } from "@/components/ui/tooltip"; +import { RefreshCcwDot } from "lucide-react"; +import MyAlert from "../myChakra/MyAlert"; import { formatDateTime } from "@/utils/userFuncs"; +type Loan = { + id: number; + username: string; + start_date: string; + end_date: string; + returned_date: string | null; + take_date: string | null; + loaned_items_name: string[] | string; +}; + const Landingpage: React.FC = () => { const [isLoading, setIsLoading] = useState(false); - const [loans, setLoans] = useState([]); + const [loans, setLoans] = useState([]); + const [isError, setIsError] = useState(false); + const [errorStatus, setErrorStatus] = useState<"error" | "success">("error"); + const [errorMessage, setErrorMessage] = useState(""); + const [errorDsc, setErrorDsc] = useState(""); + const [reload, setReload] = useState(false); + + const setError = ( + status: "error" | "success", + message: string, + description: string + ) => { + setIsError(false); + setErrorStatus(status); + setErrorMessage(message); + setErrorDsc(description); + setIsError(true); + }; useEffect(() => { - setIsLoading(true); - fetch("http://localhost:8002/apiV2/allLoans") - .then((response) => response.json()) - .then((data) => { - setLoans(data); + const fetchLoans = async () => { + setIsLoading(true); + try { + const res = await fetch("http://localhost:8002/apiV2/allLoans"); + const data = await res.json(); + if (Array.isArray(data)) { + setLoans(data); + } else { + setError( + "error", + "Fehler beim Laden", + "Unerwartetes Datenformat erhalten." + ); + } + } catch (e) { + setError( + "error", + "Fehler beim Laden", + "Die Ausleihen konnten nicht geladen werden." + ); + } finally { setIsLoading(false); - }) - .catch((error) => { - console.error("Error fetching loans:", error); - setIsLoading(false); - }); - }, []); + } + }; + fetchLoans(); + }, [reload]); return ( <> - + Matthias-Claudius-Schule Technik + + {/* Action toolbar */} + + + setReload(!reload)} + > + + + + + {/* End action toolbar */} + Alle Ausleihen + + {isError && ( + + )} + {isLoading && ( Loading... )} + {!isLoading && ( - - - - - - - - - - - - # - - - Username - - - Start Date - - - End Date - - - Loaned Items - - - Returned Date - - - Take Date - - - - - {loans.map((loan) => ( - - - {loan.id} - - {loan.username} - - {formatDateTime(loan.start_date)} - - - {formatDateTime(loan.end_date)} - - - {loan.loaned_items_name} - - - {formatDateTime(loan.returned_date)} - - - {formatDateTime(loan.take_date)} - - - ))} - - - - + + + + + # + + + Benutzername + + + Startdatum + + + Enddatum + + + Ausgeliehene Artikel + + + Rückgabedatum + + + Ausleihdatum + + + + + {loans.map((loan) => ( + + {loan.id} + {loan.username} + {formatDateTime(loan.start_date)} + {formatDateTime(loan.end_date)} + + {Array.isArray(loan.loaned_items_name) + ? loan.loaned_items_name.join(", ") + : loan.loaned_items_name} + + {formatDateTime(loan.returned_date)} + {formatDateTime(loan.take_date)} + + ))} + + + )} + + {!isLoading && loans.length === 0 && !isError && ( + + Keine Ausleihen vorhanden. + )} );