2025-09-12 13:28:14 +01:00
|
|
|
import { Formik, Field, Form } from "formik";
|
|
|
|
|
import FormGroup from "../components/FormGroup";
|
2025-10-07 09:56:24 +01:00
|
|
|
import { useReboots } from "../../../hooks/useReboots";
|
2025-09-12 13:28:14 +01:00
|
|
|
import { timezones } from "./timezones";
|
|
|
|
|
import SystemFileUpload from "./SystemFileUpload";
|
|
|
|
|
import type { SystemValues, SystemValuesErrors } from "../../../types/types";
|
2025-09-15 10:27:31 +01:00
|
|
|
import { useSystemConfig } from "../../../hooks/useSystemConfig";
|
2025-09-12 13:28:14 +01:00
|
|
|
|
|
|
|
|
const SystemConfigFields = () => {
|
2025-10-06 14:21:56 +01:00
|
|
|
const { saveSystemSettings, systemSettingsData, saveSystemSettingsLoading } =
|
|
|
|
|
useSystemConfig();
|
2025-10-07 09:56:24 +01:00
|
|
|
const { softRebootMutation, hardRebootMutation } = useReboots();
|
|
|
|
|
|
2025-09-12 13:28:14 +01:00
|
|
|
const initialvalues: SystemValues = {
|
2025-09-16 14:20:38 +01:00
|
|
|
deviceName: systemSettingsData?.deviceName ?? "",
|
|
|
|
|
timeZone: systemSettingsData?.timeZone ?? "",
|
|
|
|
|
sntpServer: systemSettingsData?.sntpServer ?? "",
|
|
|
|
|
sntpInterval: systemSettingsData?.sntpInterval ?? 60,
|
2025-09-12 13:28:14 +01:00
|
|
|
softwareUpdate: null,
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-15 10:27:31 +01:00
|
|
|
const handleSubmit = (values: SystemValues) => saveSystemSettings(values);
|
2025-10-07 09:56:24 +01:00
|
|
|
|
2025-09-12 13:28:14 +01:00
|
|
|
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";
|
|
|
|
|
return errors;
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-07 09:56:24 +01:00
|
|
|
const handleSoftReboot = async () => {
|
|
|
|
|
await softRebootMutation.mutate();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleHardReboot = async () => {
|
|
|
|
|
await hardRebootMutation.mutate();
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-12 13:28:14 +01:00
|
|
|
return (
|
|
|
|
|
<Formik
|
|
|
|
|
initialValues={initialvalues}
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
validate={validateValues}
|
2025-09-16 14:20:38 +01:00
|
|
|
enableReinitialize
|
|
|
|
|
validateOnChange
|
|
|
|
|
validateOnBlur
|
2025-09-12 13:28:14 +01:00
|
|
|
>
|
2025-10-06 14:21:56 +01:00
|
|
|
{({ values, errors, touched, isSubmitting }) => (
|
2025-09-26 13:58:14 +01:00
|
|
|
<Form className="flex flex-col space-y-5 px-2">
|
2025-09-12 13:28:14 +01:00
|
|
|
<FormGroup>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor="deviceName"
|
|
|
|
|
className="font-medium whitespace-nowrap md:w-1/2 text-left"
|
|
|
|
|
>
|
|
|
|
|
Device Name
|
|
|
|
|
</label>
|
|
|
|
|
{touched.deviceName && errors.deviceName && (
|
|
|
|
|
<small className="absolute right-0 -top-5 text-red-500">
|
|
|
|
|
{errors.deviceName}
|
|
|
|
|
</small>
|
|
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="deviceName"
|
|
|
|
|
name="deviceName"
|
|
|
|
|
type="text"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg w-full max-w-xs"
|
|
|
|
|
placeholder="Enter device name"
|
2025-09-16 14:20:38 +01:00
|
|
|
autoComplete="off"
|
2025-09-12 13:28:14 +01:00
|
|
|
/>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
<FormGroup>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor="timeZone"
|
|
|
|
|
className="font-medium whitespace-nowrap md:w-1/2 text-left"
|
|
|
|
|
>
|
|
|
|
|
Local Time Zone
|
|
|
|
|
</label>
|
|
|
|
|
{touched.timeZone && errors.timeZone && (
|
|
|
|
|
<small className="absolute right-0 -top-5 text-red-500">
|
|
|
|
|
{errors.timeZone}
|
|
|
|
|
</small>
|
|
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="timeZone"
|
|
|
|
|
name="timeZone"
|
|
|
|
|
as="select"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg text-white bg-[#253445] w-full max-w-xs"
|
|
|
|
|
>
|
2025-09-16 14:20:38 +01:00
|
|
|
<option value="">Select a timezone…</option>
|
2025-09-12 13:28:14 +01:00
|
|
|
{timezones.map((timezone) => (
|
|
|
|
|
<option value={timezone.value} key={timezone.label}>
|
|
|
|
|
{timezone.label}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</Field>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
<FormGroup>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor="sntpServer"
|
|
|
|
|
className="font-medium whitespace-nowrap md:w-1/2 text-left"
|
|
|
|
|
>
|
|
|
|
|
SNTP Server
|
|
|
|
|
</label>
|
|
|
|
|
{touched.sntpServer && errors.sntpServer && (
|
|
|
|
|
<small className="absolute right-0 -top-5 text-red-500">
|
|
|
|
|
{errors.sntpServer}
|
|
|
|
|
</small>
|
|
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="sntpServer"
|
|
|
|
|
name="sntpServer"
|
|
|
|
|
type="text"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg w-full max-w-xs"
|
|
|
|
|
placeholder="Enter SNTP server address"
|
2025-09-16 14:20:38 +01:00
|
|
|
autoComplete="off"
|
2025-09-12 13:28:14 +01:00
|
|
|
/>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
<FormGroup>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor="sntpInterval"
|
|
|
|
|
className="font-medium whitespace-nowrap md:w-1/2 text-left"
|
|
|
|
|
>
|
|
|
|
|
SNTP Interval minutes
|
|
|
|
|
</label>
|
|
|
|
|
{touched.sntpInterval && errors.sntpInterval && (
|
|
|
|
|
<small className="absolute right-0 -top-5 text-red-500">
|
|
|
|
|
{errors.sntpInterval}
|
|
|
|
|
</small>
|
|
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
id="sntpInterval"
|
|
|
|
|
name="sntpInterval"
|
|
|
|
|
type="number"
|
|
|
|
|
min={1}
|
2025-09-16 14:20:38 +01:00
|
|
|
inputMode="numeric"
|
2025-09-12 13:28:14 +01:00
|
|
|
className="p-2 border border-gray-400 rounded-lg w-full max-w-xs"
|
|
|
|
|
/>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
2025-09-23 16:02:14 +01:00
|
|
|
className="bg-[#26B170] text-white px-4 py-2 rounded hover:bg-green-700 transition w-full md:w-[50%]"
|
2025-10-06 14:21:56 +01:00
|
|
|
disabled={isSubmitting}
|
2025-09-12 13:28:14 +01:00
|
|
|
>
|
2025-10-06 14:21:56 +01:00
|
|
|
{saveSystemSettingsLoading ? "Saving..." : "Save System Settings"}
|
2025-09-12 13:28:14 +01:00
|
|
|
</button>
|
|
|
|
|
<SystemFileUpload
|
|
|
|
|
name={"softwareUpdate"}
|
|
|
|
|
selectedFile={values.softwareUpdate}
|
|
|
|
|
/>
|
2025-09-17 11:39:26 +01:00
|
|
|
<div className="border-b border-gray-600">
|
|
|
|
|
<p>Reboot</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-09-12 13:28:14 +01:00
|
|
|
<button
|
2025-09-16 14:20:38 +01:00
|
|
|
type="button"
|
2025-09-23 16:02:14 +01:00
|
|
|
className="bg-red-600 text-white px-4 py-2 rounded hover:bg-red-700 transition w-full md:w-[50%]"
|
2025-09-12 13:28:14 +01:00
|
|
|
onClick={handleSoftReboot}
|
|
|
|
|
>
|
2025-10-07 09:56:24 +01:00
|
|
|
{softRebootMutation.isPending || isSubmitting
|
|
|
|
|
? "Rebooting..."
|
|
|
|
|
: "Software Reboot"}
|
2025-09-12 13:28:14 +01:00
|
|
|
</button>
|
|
|
|
|
<button
|
2025-09-16 14:20:38 +01:00
|
|
|
type="button"
|
2025-09-23 16:02:14 +01:00
|
|
|
className="bg-red-600 text-white px-4 py-2 rounded hover:bg-red-700 transition w-full md:w-[50%]"
|
2025-09-12 13:28:14 +01:00
|
|
|
onClick={handleHardReboot}
|
|
|
|
|
>
|
2025-10-07 09:56:24 +01:00
|
|
|
{hardRebootMutation.isPending || isSubmitting
|
|
|
|
|
? "Rebooting"
|
|
|
|
|
: "Hardware Reboot"}
|
2025-09-12 13:28:14 +01:00
|
|
|
</button>
|
|
|
|
|
</Form>
|
|
|
|
|
)}
|
|
|
|
|
</Formik>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SystemConfigFields;
|