From 4b00dd655413e84fddc7b8f5a9300491aed41f92 Mon Sep 17 00:00:00 2001 From: Theis Gaedigk Date: Sat, 25 Oct 2025 20:01:06 +0200 Subject: [PATCH] added borrowable items fetching and date input functionality to HomePage --- FrontendV2/src/pages/HomePage.tsx | 102 ++++++++++++++++++++++++++++++ FrontendV2/src/states/Atoms.tsx | 1 + FrontendV2/src/utils/Fetcher.ts | 49 ++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 FrontendV2/src/utils/Fetcher.ts diff --git a/FrontendV2/src/pages/HomePage.tsx b/FrontendV2/src/pages/HomePage.tsx index 216a057..bf1eb72 100644 --- a/FrontendV2/src/pages/HomePage.tsx +++ b/FrontendV2/src/pages/HomePage.tsx @@ -7,10 +7,18 @@ import { Badge, Flex, Button, + Input, + Spinner, + VStack, + Table, } from "@chakra-ui/react"; import { triggerLogoutAtom, setIsLoggedInAtom } from "@/states/Atoms"; import { useAtom } from "jotai"; import Cookies from "js-cookie"; +import { getBorrowableItems } from "@/utils/Fetcher"; +import { useState } from "react"; +import MyAlert from "@/components/myChakra/MyAlert"; +import { borrowAbleItemsAtom } from "@/states/Atoms"; export interface User { username: string; @@ -21,6 +29,15 @@ export const HomePage = () => { const userData = useUserContext(); const [, setTriggerLogout] = useAtom(triggerLogoutAtom); const [, setIsLoggedIn] = useAtom(setIsLoggedInAtom); + const [borrowableItems, setBorrowableItems] = useAtom(borrowAbleItemsAtom); + const [startDate, setStartDate] = useState(""); + const [endDate, setEndDate] = useState(""); + const [isLoadingA, setIsLoadingA] = useState(false); + + // Error handling states + const [isError, setIsError] = useState(false); + const [errorTitle, setErrorTitle] = useState(""); + const [errorDescription, setErrorDescription] = useState(""); return ( @@ -74,6 +91,91 @@ export const HomePage = () => { + {isError && ( + + )} + + + setStartDate(e.target.value)} + /> + + setEndDate(e.target.value)} + /> + + {isLoadingA && ( + + + Loading... + + )} + {borrowableItems.length > 0 && ( + + + + + + Gegenstand + + + + + {borrowableItems.map((item) => ( + + + + + {item.item_name} + + ))} + + + + )} + ); }; diff --git a/FrontendV2/src/states/Atoms.tsx b/FrontendV2/src/states/Atoms.tsx index c4f3f51..64f35dc 100644 --- a/FrontendV2/src/states/Atoms.tsx +++ b/FrontendV2/src/states/Atoms.tsx @@ -3,3 +3,4 @@ import { atom } from "jotai"; export const testAtom = atom(0); export const setIsLoggedInAtom = atom(false); export const triggerLogoutAtom = atom(false); +export const borrowAbleItemsAtom = atom([]); diff --git a/FrontendV2/src/utils/Fetcher.ts b/FrontendV2/src/utils/Fetcher.ts new file mode 100644 index 0000000..72e6f11 --- /dev/null +++ b/FrontendV2/src/utils/Fetcher.ts @@ -0,0 +1,49 @@ +import Cookies from "js-cookie"; +const API_BASE = + (import.meta as any).env?.VITE_BACKEND_URL || + import.meta.env.VITE_BACKEND_URL || + "http://localhost:8002"; + +export const getBorrowableItems = async ( + startDate: string, + endDate: string +) => { + try { + const response = await fetch(`${API_BASE}/api/borrowableItems`, { + method: "POST", + headers: { + Authorization: `Bearer ${Cookies.get("token") || ""}`, + "Content-Type": "application/json", + Accept: "application/json", + }, + body: JSON.stringify({ startDate, endDate }), + }); + + if (!response.ok) { + return { + data: null, + status: "error", + title: "Server error", + description: + "Ein Fehler ist auf dem Server aufgetreten. Manchmal hilft es, die Seite neu zu laden.", + }; + } + + const data = await response.json(); + console.log(data); + return { + data: data, + status: "success", + title: null, + description: null, + }; + } catch (error) { + return { + data: null, + status: "error", + title: "Netzwerkfehler", + description: + "Es konnte keine Verbindung zum Server hergestellt werden. Bitte überprüfe deine Internetverbindung.", + }; + } +};