diff --git a/admin/src/Layout/Dashboard.tsx b/admin/src/Layout/Dashboard.tsx index 7d78cdf..1b53d6d 100644 --- a/admin/src/Layout/Dashboard.tsx +++ b/admin/src/Layout/Dashboard.tsx @@ -5,6 +5,7 @@ import Sidebar from "./Sidebar"; import UserTable from "../components/UserTable"; import ItemTable from "../components/ItemTable"; import LoanTable from "../components/LoanTable"; +import APIKeyTable from "@/components/APIKeyTable"; import { MoveLeft } from "lucide-react"; type DashboardProps = { @@ -23,6 +24,7 @@ const Dashboard: React.FC = ({ onLogout }) => { viewGegenstaende={() => setActiveView("Gegenstände")} viewSchliessfaecher={() => setActiveView("Schließfächer")} viewUser={() => setActiveView("User")} + viewAPI={() => setActiveView("API")} /> = ({ onLogout }) => { {activeView === "User" && } {activeView === "Ausleihen" && } {activeView === "Gegenstände" && } + {activeView === "API" && } diff --git a/admin/src/Layout/Sidebar.tsx b/admin/src/Layout/Sidebar.tsx index 3637124..05817fe 100644 --- a/admin/src/Layout/Sidebar.tsx +++ b/admin/src/Layout/Sidebar.tsx @@ -6,12 +6,14 @@ type SidebarProps = { viewGegenstaende: () => void; viewSchliessfaecher: () => void; viewUser: () => void; + viewAPI: () => void; }; const Sidebar: React.FC = ({ viewAusleihen, viewGegenstaende, viewUser, + viewAPI, }) => { return ( = ({ > Gegenstände + + API Keys + diff --git a/admin/src/components/APIKeyTable.tsx b/admin/src/components/APIKeyTable.tsx new file mode 100644 index 0000000..ab97718 --- /dev/null +++ b/admin/src/components/APIKeyTable.tsx @@ -0,0 +1,211 @@ +import React from "react"; +import { + Table, + Spinner, + Text, + VStack, + Button, + HStack, + IconButton, + Heading, +} from "@chakra-ui/react"; +import { Tooltip } from "@/components/ui/tooltip"; +import MyAlert from "./myChakra/MyAlert"; +import { + Trash2, + RefreshCcwDot, + CirclePlus, +} from "lucide-react"; +import Cookies from "js-cookie"; +import { useState, useEffect } from "react"; +import { + deleteItem, +} from "@/utils/userActions"; +import AddItemForm from "./AddItemForm"; +import { formatDateTime } from "@/utils/userFuncs"; + +type Items = { + id: number; + apiKey: string; + user: string; + entry_created_at: string; +}; + +const APIKeyTable: 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 [addAPIForm, setAddAPIForm] = useState(false); + + 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("http://localhost:8002/api/keys", { + 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... + + )} + {addAPIForm && ( + { + setAddAPIForm(false); + setReload(!reload); + }} + alert={setError} + /> + )} + + + + + + # + + + API Key + + + Benutzer + + + Eintrag erstellt am + + + Aktionen + + + + + {items.map((apiKey) => ( + + {apiKey.id} + {apiKey.apiKey} + + {apiKey.user} + + {formatDateTime(apiKey.entry_created_at)} + + + + + ))} + + + + ); +}; + +export default APIKeyTable;