import { useEffect, useState } from "react"; import Cookies from "js-cookie"; import { useNavigate } from "react-router-dom"; import MyAlert from "@/components/myChakra/MyAlert"; import { Container, VStack, Spinner, Text, Table, Button, CloseButton, Dialog, Portal, Code, } from "@chakra-ui/react"; import { Header } from "@/components/Header"; import { Trash2 } from "lucide-react"; import { useTranslation } from "react-i18next"; const API_BASE = (import.meta as any).env?.VITE_BACKEND_URL || import.meta.env.VITE_BACKEND_URL || "http://localhost:8002"; export const MyLoansPage = () => { const { t } = useTranslation(); const navigate = useNavigate(); const [loans, setLoans] = useState([]); const [isLoading, setIsLoading] = useState(false); const [delLoanCode, setDelLoanCode] = useState(null); // Error handling states const [isMsg, setIsMsg] = useState(false); const [msgStatus, setMsgStatus] = useState<"error" | "success">("error"); const [msgTitle, setMsgTitle] = useState(""); const [msgDescription, setMsgDescription] = useState(""); useEffect(() => { if (!Cookies.get("token")) { navigate("/login", { replace: true }); return; } const fetchLoans = async () => { try { setIsLoading(true); const res = await fetch(`${API_BASE}/api/userLoans`, { method: "GET", headers: { Authorization: `Bearer ${Cookies.get("token")}`, }, }); if (!res.ok) { setMsgStatus("error"); setMsgTitle(t("error")); setMsgDescription(t("error-fetching-loans")); setIsMsg(true); return; } const data = await res.json(); setLoans(data); } catch (e) { setMsgStatus("error"); setMsgTitle(t("error")); setMsgDescription(t("network-error-fetching-loans")); setIsMsg(true); } finally { setIsLoading(false); } }; fetchLoans(); }, [navigate]); const deleteLoan = async (loanId: number) => { try { const res = await fetch(`${API_BASE}/api/SETdeleteLoan/${loanId}`, { method: "DELETE", headers: { Authorization: `Bearer ${Cookies.get("token")}`, }, }); if (!res.ok) { setMsgStatus("error"); setMsgTitle(t("error")); setMsgDescription(t("error-deleting-loan")); setIsMsg(true); return; } setLoans((prev) => prev.filter((loan) => loan.id !== loanId)); setMsgStatus("success"); setMsgTitle(t("success")); setMsgDescription(t("loan-deletion-success")); setIsMsg(true); } catch (e) { setMsgStatus("error"); setMsgTitle(t("error")); setMsgDescription(t("network-error-deleting-loan")); setIsMsg(true); } }; const formatDate = (iso: string | null) => { if (!iso) return "-"; const m = iso.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/); if (!m) return iso; const [, y, M, d, h, min] = m; return `${d}.${M}.${y} ${h}:${min}`; }; return ( <>
{isMsg && ( )} {isLoading && ( {t("loading")} )} {loans && ( {/* Ausleihcode */} {/* Startdatum */} {/* Enddatum */} {/* Geräte (flexibler) */} {/* Ausleihdatum */} {/* Rückgabedatum */} {/* Aktionen */} {t("loan-code")} {t("start-date")} {t("end-date")} {t("devices")} {t("take-date")} {t("return-date")} {t("actions")} {loans.map((loan) => ( {`${loan.loan_code}`} {formatDate(loan.start_date)} {formatDate(loan.end_date)} {loan.loaned_items_name} {formatDate(loan.take_date)} {formatDate(loan.returned_date)} {t("sure")} {t("sure-delete-loan-0")} {delLoanCode} {" "} {t("sure-delete-loan-1")}
{t("sure-delete-loan-2")}
))}
)} ); };