import React from "react"; import { Table, Spinner, Text, VStack, Button, HStack, IconButton, Heading, Icon, Input, } from "@chakra-ui/react"; import { Tooltip } from "@/components/ui/tooltip"; import MyAlert from "./myChakra/MyAlert"; import { Trash2, RefreshCcwDot, CirclePlus, CheckCircle2, XCircle, Save, } from "lucide-react"; import Cookies from "js-cookie"; import { useState, useEffect } from "react"; import { deleteItem, handleEditItems, changeSafeState, } from "@/utils/userActions"; import AddItemForm from "./AddItemForm"; import { formatDateTime } from "@/utils/userFuncs"; type Items = { id: number; item_name: string; can_borrow_role: string; inSafe: boolean; entry_created_at: string; }; const ItemTable: React.FC = () => { const [items, setItems] = useState([]); const [errorStatus, setErrorStatus] = useState<"error" | "success">("error"); const [errorMessage, setErrorMessage] = useState(""); const [errorDsc, setErrorDsc] = useState(""); const [isError, setIsError] = useState(false); const [isLoading, setIsLoading] = useState(false); const [reload, setReload] = useState(false); const [addForm, setAddForm] = useState(false); const handleItemNameChange = (id: number, value: string) => { setItems((prev) => prev.map((it) => (it.id === id ? { ...it, item_name: value } : it)) ); }; const handleCanBorrowRoleChange = (id: number, value: string) => { setItems((prev) => prev.map((it) => (it.id === id ? { ...it, can_borrow_role: value } : it)) ); }; const setError = ( status: "error" | "success", message: string, description: string ) => { setIsError(false); setErrorStatus(status); setErrorMessage(message); setErrorDsc(description); setIsError(true); }; useEffect(() => { const fetchData = async () => { setIsLoading(true); try { const response = await fetch("https://backend.insta.the1s.de/api/allItems", { method: "GET", headers: { Authorization: `Bearer ${Cookies.get("token")}`, }, }); const data = await response.json(); return data; } catch (error) { setError("error", "Failed to fetch items", "There is an error"); } finally { setIsLoading(false); } }; fetchData().then((data) => { if (Array.isArray(data)) { setItems(data); } }); }, [reload]); return ( <> {/* Action toolbar */} setReload(!reload)} > {/* End action toolbar */} Gegenstände {isError && ( )} {isLoading && ( Loading... )} {addForm && ( { setAddForm(false); setReload(!reload); }} alert={setError} /> )} # Gegenstand Ausleih Berechtigung Im Schließfach Eintrag erstellt am Aktionen {items.map((item) => ( {item.id} handleItemNameChange(item.id, e.target.value) } value={item.item_name} /> handleCanBorrowRoleChange(item.id, e.target.value) } value={item.can_borrow_role} /> {formatDateTime(item.entry_created_at)} ))} ); }; export default ItemTable;