import React from "react"; import { Table, Spinner, Text, VStack, Heading, Switch, } from "@chakra-ui/react"; import MyAlert from "./myChakra/MyAlert"; import Cookies from "js-cookie"; import { useState, useEffect } from "react"; import { formatDateTime } from "@/utils/userFuncs"; import { API_BASE } from "@/config/api.config"; type Items = { id: number; function_name: string; active: boolean; entry_created_at: string; updated_at: string | null; }; const ServerConfig: 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 handleSwitchChange = async (id: number, newState: boolean) => { try { const response = await fetch( `${API_BASE}/api/admin/server-config/update?functionName=${encodeURIComponent( items.find((item) => item.id === id)?.function_name || "", )}&active=${newState}`, { method: "POST", headers: { Authorization: `Bearer ${Cookies.get("token")}`, }, }, ); if (response.ok) { setReload((prev) => !prev); setError( "success", "Status updated", "The function status was updated successfully.", ); } else { setError( "error", "Failed to update status", "There is an error updating the function status.", ); } } catch (error) { setError( "error", "Failed to update status", "There is an error updating the function status.", ); } }; 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( `${API_BASE}/api/admin/server-config/all`, { method: "GET", headers: { Authorization: `Bearer ${Cookies.get("token")}`, }, }, ); const data = await response.json(); return data.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 ( <> Server Konfiguration {isError && ( )} {isLoading && ( Loading... )} # Service Name Toggle Eintrag erstellt am {items.map((item) => ( {item.id} {item.function_name} handleSwitchChange(item.id, !item.active) } > {formatDateTime(item.entry_created_at)} ))} ); }; export default ServerConfig;