import { useQuery, useMutation } from "@tanstack/react-query"; import { CAMBASE } from "../../../utils/config"; import type { NetworkConfig } from "../../../types/types"; const fetchNetworkConfig = async () => { const response = await fetch(`${CAMBASE}/api/fetch-config?id=GLOBAL--NetworkConfig`); if (!response.ok) throw new Error("Network response was not ok"); return response.json(); }; const postNetworkConfig = async (networkConfig: NetworkConfig) => { const fields = [ { property: "propNetmask", value: networkConfig.subnetMask }, { property: "propHost", value: networkConfig.ipAddress }, { property: "propGateway", value: networkConfig.gateway }, ]; if (networkConfig.primaryServer !== undefined) { fields.push({ property: "propNameServerPrimary", value: networkConfig.primaryServer }); } if (networkConfig.secondaryServer !== undefined) { fields.push({ property: "propNameServerSecondary", value: networkConfig.secondaryServer }); } const networkConfigPayload = { id: "GLOBAL--NetworkConfig", fields, }; const respones = await fetch(`${CAMBASE}/api/update-config?id=GLOBAL--NetworkConfig`, { method: "POST", body: JSON.stringify(networkConfigPayload), }); if (!respones.ok) throw new Error("Network response was not ok"); return respones.json(); }; export const useGetNetworkConfig = () => { const networkConfigQuery = useQuery({ queryKey: ["networkConfig"], queryFn: fetchNetworkConfig, }); const networkConfigMutation = useMutation({ mutationKey: ["networkConfigMutation"], mutationFn: postNetworkConfig, }); return { networkConfigQuery, networkConfigMutation }; };