changed design for the landingpage
This commit is contained in:
@@ -1,210 +1,178 @@
|
|||||||
import React from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { useEffect } from "react";
|
import {
|
||||||
import { useState } from "react";
|
Spinner,
|
||||||
import { Spinner, Text, VStack, Box } from "@chakra-ui/react";
|
Text,
|
||||||
import { Table, Heading } from "@chakra-ui/react";
|
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";
|
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 Landingpage: React.FC = () => {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [loans, setLoans] = useState<any[]>([]);
|
const [loans, setLoans] = useState<Loan[]>([]);
|
||||||
|
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(() => {
|
useEffect(() => {
|
||||||
setIsLoading(true);
|
const fetchLoans = async () => {
|
||||||
fetch("http://localhost:8002/apiV2/allLoans")
|
setIsLoading(true);
|
||||||
.then((response) => response.json())
|
try {
|
||||||
.then((data) => {
|
const res = await fetch("http://localhost:8002/apiV2/allLoans");
|
||||||
setLoans(data);
|
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);
|
setIsLoading(false);
|
||||||
})
|
}
|
||||||
.catch((error) => {
|
};
|
||||||
console.error("Error fetching loans:", error);
|
fetchLoans();
|
||||||
setIsLoading(false);
|
}, [reload]);
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Heading as="h1" size="lg" mb={4}>
|
<Heading as="h1" size="lg" mb={2}>
|
||||||
Matthias-Claudius-Schule Technik
|
Matthias-Claudius-Schule Technik
|
||||||
</Heading>
|
</Heading>
|
||||||
|
|
||||||
|
{/* Action toolbar */}
|
||||||
|
<HStack
|
||||||
|
mb={4}
|
||||||
|
gap={3}
|
||||||
|
justify="flex-start"
|
||||||
|
align="center"
|
||||||
|
flexWrap="wrap"
|
||||||
|
>
|
||||||
|
<Tooltip content="Ausleihen neu laden" openDelay={300}>
|
||||||
|
<IconButton
|
||||||
|
aria-label="Refresh loans"
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
rounded="md"
|
||||||
|
shadow="sm"
|
||||||
|
_hover={{ shadow: "md", transform: "translateY(-2px)" }}
|
||||||
|
_active={{ transform: "translateY(0)" }}
|
||||||
|
onClick={() => setReload(!reload)}
|
||||||
|
>
|
||||||
|
<RefreshCcwDot size={18} />
|
||||||
|
</IconButton>
|
||||||
|
</Tooltip>
|
||||||
|
</HStack>
|
||||||
|
{/* End action toolbar */}
|
||||||
|
|
||||||
<Heading as="h2" size="md" mb={4}>
|
<Heading as="h2" size="md" mb={4}>
|
||||||
Alle Ausleihen
|
Alle Ausleihen
|
||||||
</Heading>
|
</Heading>
|
||||||
|
|
||||||
|
{isError && (
|
||||||
|
<MyAlert
|
||||||
|
status={errorStatus}
|
||||||
|
description={errorDsc}
|
||||||
|
title={errorMessage}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
{isLoading && (
|
{isLoading && (
|
||||||
<VStack colorPalette="teal">
|
<VStack colorPalette="teal">
|
||||||
<Spinner color="colorPalette.600" />
|
<Spinner color="colorPalette.600" />
|
||||||
<Text color="colorPalette.600">Loading...</Text>
|
<Text color="colorPalette.600">Loading...</Text>
|
||||||
</VStack>
|
</VStack>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{!isLoading && (
|
{!isLoading && (
|
||||||
<Box
|
<Table.Root size="sm" striped>
|
||||||
borderWidth="1px"
|
<Table.Header>
|
||||||
borderRadius="lg"
|
<Table.Row>
|
||||||
overflow="hidden"
|
<Table.ColumnHeader>
|
||||||
boxShadow="sm"
|
<strong>#</strong>
|
||||||
bg="white"
|
</Table.ColumnHeader>
|
||||||
_dark={{ bg: "gray.800", borderColor: "gray.700" }}
|
<Table.ColumnHeader>
|
||||||
>
|
<strong>Benutzername</strong>
|
||||||
<Box maxH="70vh" overflow="auto">
|
</Table.ColumnHeader>
|
||||||
<Table.Root
|
<Table.ColumnHeader>
|
||||||
size="md"
|
<strong>Startdatum</strong>
|
||||||
variant="outline"
|
</Table.ColumnHeader>
|
||||||
colorPalette="teal"
|
<Table.ColumnHeader>
|
||||||
w="full"
|
<strong>Enddatum</strong>
|
||||||
minW="900px"
|
</Table.ColumnHeader>
|
||||||
>
|
<Table.ColumnHeader>
|
||||||
<Table.ColumnGroup>
|
<strong>Ausgeliehene Artikel</strong>
|
||||||
<Table.Column htmlWidth="50%" />
|
</Table.ColumnHeader>
|
||||||
<Table.Column htmlWidth="40%" />
|
<Table.ColumnHeader>
|
||||||
<Table.Column />
|
<strong>Rückgabedatum</strong>
|
||||||
</Table.ColumnGroup>
|
</Table.ColumnHeader>
|
||||||
<Table.Header
|
<Table.ColumnHeader>
|
||||||
position="sticky"
|
<strong>Ausleihdatum</strong>
|
||||||
top={0}
|
</Table.ColumnHeader>
|
||||||
zIndex={1}
|
</Table.Row>
|
||||||
bg="gray.50"
|
</Table.Header>
|
||||||
_dark={{ bg: "gray.700" }}
|
<Table.Body>
|
||||||
>
|
{loans.map((loan) => (
|
||||||
<Table.Row>
|
<Table.Row key={loan.id}>
|
||||||
<Table.ColumnHeader
|
<Table.Cell>{loan.id}</Table.Cell>
|
||||||
textTransform="uppercase"
|
<Table.Cell>{loan.username}</Table.Cell>
|
||||||
letterSpacing="wider"
|
<Table.Cell>{formatDateTime(loan.start_date)}</Table.Cell>
|
||||||
fontSize="xs"
|
<Table.Cell>{formatDateTime(loan.end_date)}</Table.Cell>
|
||||||
color="gray.600"
|
<Table.Cell>
|
||||||
_dark={{ color: "gray.200" }}
|
{Array.isArray(loan.loaned_items_name)
|
||||||
py={3}
|
? loan.loaned_items_name.join(", ")
|
||||||
>
|
: loan.loaned_items_name}
|
||||||
<strong>#</strong>
|
</Table.Cell>
|
||||||
</Table.ColumnHeader>
|
<Table.Cell>{formatDateTime(loan.returned_date)}</Table.Cell>
|
||||||
<Table.ColumnHeader
|
<Table.Cell>{formatDateTime(loan.take_date)}</Table.Cell>
|
||||||
textTransform="uppercase"
|
</Table.Row>
|
||||||
letterSpacing="wider"
|
))}
|
||||||
fontSize="xs"
|
</Table.Body>
|
||||||
color="gray.600"
|
</Table.Root>
|
||||||
_dark={{ color: "gray.200" }}
|
)}
|
||||||
py={3}
|
|
||||||
>
|
{!isLoading && loans.length === 0 && !isError && (
|
||||||
<strong>Username</strong>
|
<Text color="gray.500" mt={2}>
|
||||||
</Table.ColumnHeader>
|
Keine Ausleihen vorhanden.
|
||||||
<Table.ColumnHeader
|
</Text>
|
||||||
textTransform="uppercase"
|
|
||||||
letterSpacing="wider"
|
|
||||||
fontSize="xs"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.200" }}
|
|
||||||
py={3}
|
|
||||||
>
|
|
||||||
<strong>Start Date</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader
|
|
||||||
textTransform="uppercase"
|
|
||||||
letterSpacing="wider"
|
|
||||||
fontSize="xs"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.200" }}
|
|
||||||
py={3}
|
|
||||||
>
|
|
||||||
<strong>End Date</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader
|
|
||||||
textTransform="uppercase"
|
|
||||||
letterSpacing="wider"
|
|
||||||
fontSize="xs"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.200" }}
|
|
||||||
py={3}
|
|
||||||
>
|
|
||||||
<strong>Loaned Items</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader
|
|
||||||
textTransform="uppercase"
|
|
||||||
letterSpacing="wider"
|
|
||||||
fontSize="xs"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.200" }}
|
|
||||||
py={3}
|
|
||||||
>
|
|
||||||
<strong>Returned Date</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
<Table.ColumnHeader
|
|
||||||
textTransform="uppercase"
|
|
||||||
letterSpacing="wider"
|
|
||||||
fontSize="xs"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.200" }}
|
|
||||||
py={3}
|
|
||||||
>
|
|
||||||
<strong>Take Date</strong>
|
|
||||||
</Table.ColumnHeader>
|
|
||||||
</Table.Row>
|
|
||||||
</Table.Header>
|
|
||||||
<Table.Body>
|
|
||||||
{loans.map((loan) => (
|
|
||||||
<Table.Row
|
|
||||||
key={loan.id}
|
|
||||||
_hover={{ bg: "gray.50", _dark: { bg: "whiteAlpha.100" } }}
|
|
||||||
transition="background-color 120ms ease"
|
|
||||||
>
|
|
||||||
<Table.Cell py={3} fontWeight="semibold">
|
|
||||||
{loan.id}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell py={3}>{loan.username}</Table.Cell>
|
|
||||||
<Table.Cell
|
|
||||||
py={3}
|
|
||||||
whiteSpace="nowrap"
|
|
||||||
fontFamily="mono"
|
|
||||||
fontSize="sm"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.300" }}
|
|
||||||
>
|
|
||||||
{formatDateTime(loan.start_date)}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell
|
|
||||||
py={3}
|
|
||||||
whiteSpace="nowrap"
|
|
||||||
fontFamily="mono"
|
|
||||||
fontSize="sm"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.300" }}
|
|
||||||
>
|
|
||||||
{formatDateTime(loan.end_date)}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell
|
|
||||||
py={3}
|
|
||||||
maxW="40ch"
|
|
||||||
overflow="hidden"
|
|
||||||
textOverflow="ellipsis"
|
|
||||||
whiteSpace="nowrap"
|
|
||||||
>
|
|
||||||
{loan.loaned_items_name}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell
|
|
||||||
py={3}
|
|
||||||
whiteSpace="nowrap"
|
|
||||||
fontFamily="mono"
|
|
||||||
fontSize="sm"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.300" }}
|
|
||||||
>
|
|
||||||
{formatDateTime(loan.returned_date)}
|
|
||||||
</Table.Cell>
|
|
||||||
<Table.Cell
|
|
||||||
py={3}
|
|
||||||
whiteSpace="nowrap"
|
|
||||||
fontFamily="mono"
|
|
||||||
fontSize="sm"
|
|
||||||
color="gray.600"
|
|
||||||
_dark={{ color: "gray.300" }}
|
|
||||||
>
|
|
||||||
{formatDateTime(loan.take_date)}
|
|
||||||
</Table.Cell>
|
|
||||||
</Table.Row>
|
|
||||||
))}
|
|
||||||
</Table.Body>
|
|
||||||
</Table.Root>
|
|
||||||
</Box>
|
|
||||||
</Box>
|
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user