import { Formik, Field, Form } from "formik"; import FormGroup from "../components/FormGroup"; import { useReboots } from "../../../hooks/useReboots"; import { timezones } from "./timezones"; import SystemFileUpload from "./SystemFileUpload"; import type { SystemValues, SystemValuesErrors } from "../../../types/types"; import { useDNSSettings, useSystemConfig } from "../../../hooks/useSystemConfig"; import { ValidateIPaddress, isUtcOutOfSync } from "../../../utils/utils"; import { toast } from "sonner"; import { useSystemStatus } from "../../../hooks/useSystemStatus"; // import ResetUserSettings from "./resetUserSettings/ResetUserSettings"; const SystemConfigFields = () => { const { saveSystemSettings, systemSettingsData, saveSystemSettingsLoading } = useSystemConfig(); const { systemStatusQuery } = useSystemStatus(); const { hardRebootMutation } = useReboots(); const { dnsQuery, dnsMutation } = useDNSSettings(); if (systemStatusQuery?.isLoading || !systemStatusQuery?.data) { return
Loading...
; } const utcTime = systemStatusQuery?.data?.SystemStatus?.utctime; const localDate = systemStatusQuery?.data?.SystemStatus?.localdate; const localTime = systemStatusQuery?.data?.SystemStatus?.localtime; const utcOutOfSync = isUtcOutOfSync({ utctime: utcTime, localdate: localDate, localtime: localTime, }); const syncTime = new Date(systemStatusQuery?.data?.SystemStatus?.synctime * 1000).toLocaleString(); const sntpInterval = systemSettingsData?.sntpInterval; const dnsPrimary = dnsQuery?.data?.propNameServerPrimary?.value; const dnsSecondary = dnsQuery?.data?.propNameServerSecondary?.value; const initialvalues: SystemValues = { deviceName: systemSettingsData?.deviceName ?? "", timeZone: systemSettingsData?.timeZone ?? "", sntpServer: systemSettingsData?.sntpServer ?? "", sntpInterval: sntpInterval ?? 60, serverPrimary: dnsPrimary ?? "", serverSecondary: dnsSecondary ?? "", softwareUpdate: null, }; const handleSubmit = async (values: SystemValues) => { saveSystemSettings(values); await dnsMutation.mutateAsync(values); }; const validateValues = (values: SystemValues) => { const errors: SystemValuesErrors = {}; const interval = Number(values.sntpInterval); if (!values.deviceName) errors.deviceName = "Required"; if (!values.timeZone) errors.timeZone = "Required"; if (isNaN(interval) || interval <= 0) errors.sntpInterval = "Cannot be less than 0"; if (!values.sntpServer) errors.sntpServer = "Required"; const invalidPrimary = ValidateIPaddress(values.serverPrimary); const invalidSecondary = ValidateIPaddress(values.serverSecondary); if (invalidPrimary || invalidSecondary) { toast.error(invalidPrimary || invalidSecondary, { id: "invalid-ip", }); } return errors; }; // const handleSoftReboot = async () => { // await softRebootMutation.mutate(); // }; const handleHardReboot = async () => { await hardRebootMutation.mutate(); }; return ( {({ values, errors, touched, isSubmitting }) => (
{touched.deviceName && errors.deviceName && ( {errors.deviceName} )} {touched.timeZone && errors.timeZone && ( {errors.timeZone} )} {timezones.map((timezone) => ( ))} {touched.sntpServer && errors.sntpServer && ( {errors.sntpServer} )} {touched.sntpInterval && errors.sntpInterval && ( {errors.sntpInterval} )}
{utcOutOfSync?.outOfSync ? ( UTC is out of sync ) : ( UTC is in sync )}

Last Sync Time: {syncTime}

Reboot

{/* */} {/* */} )}
); }; export default SystemConfigFields;