import { Formik, Form, Field, FieldArray } from "formik"; import { useSystemSettings } from "../hooks/useSystemSettings"; import type { NetworkConfig, SystemSettings } from "../../../types/types"; import { toast } from "sonner"; import { useGetNetworkConfig } from "../hooks/useGetNetworkConfig"; const SystemConfig = () => { const { systemSettingsQuery, systemSettingsMutation } = useSystemSettings(); const { networkConfigQuery, networkConfigMutation } = useGetNetworkConfig(); const isLoading = networkConfigMutation?.isPending || networkConfigMutation?.isPending; const isGettingLoading = systemSettingsQuery?.isLoading || networkConfigQuery?.isLoading; const timeZoneOptions = systemSettingsQuery?.data?.propLocalTimeZone?.accepted; const timeZoneOpts = timeZoneOptions?.split(",").map((option: string) => option.trim().replace(/\[|\]/g, "")); const timeSourceOptions = systemSettingsQuery?.data?.propTimeSource?.accepted; const timeSourceOpts = timeSourceOptions?.split(",").map((option: string) => option.trim().replace(/\[|\]/g, "")); const deviceName = systemSettingsQuery?.data?.propDeviceName?.value; const timeZone = systemSettingsQuery?.data?.propLocalTimeZone?.value; const SNTPServer = systemSettingsQuery?.data?.propSNTPServer?.value; const SNTPInterval = systemSettingsQuery?.data?.propSNTPIntervalMinutes?.value; const timeSource = systemSettingsQuery?.data?.propTimeSource?.value; const primaryServer = networkConfigQuery?.data?.propNameServerPrimary?.value; const secondaryServer = networkConfigQuery?.data?.propNameServerSecondary?.value; const ipAddress = networkConfigQuery?.data?.propHost?.value; const subnetMask = networkConfigQuery?.data?.propNetmask?.value; const gateway = networkConfigQuery?.data?.propGateway?.value; const initialValues = { deviceName: deviceName ?? "", timeZone: timeZone ?? "", localTimeZone: timeZone ?? "", SNTPServer: SNTPServer ?? "", SNTPInterval: SNTPInterval ?? 60, SNTPIntervalMinutes: SNTPInterval ?? 60, primaryServer: primaryServer ?? "", secondaryServer: secondaryServer ?? "", timeSource: timeSource ?? "", ipAddress: ipAddress ?? "", subnetMask: subnetMask ?? "", gateway: gateway ?? "", customFields: [], }; const handleSubmit = async (values: SystemSettings & NetworkConfig) => { const result = await systemSettingsMutation.mutateAsync(values); const networkResult = await networkConfigMutation.mutateAsync({ ipAddress: values.ipAddress, subnetMask: values.subnetMask, gateway: values.gateway, primaryServer: values.primaryServer, secondaryServer: values.secondaryServer, }); if (result.id && networkResult.id) { toast.success("System settings updated successfully"); } else { toast.error("Failed to update system settings"); } }; if (isGettingLoading) { return