Files
Mav-Mobile-UI/src/components/SettingForms/System/SettingSaveRecall.tsx

92 lines
2.7 KiB
TypeScript
Raw Normal View History

import { toast } from "sonner";
2025-09-12 13:28:14 +01:00
import type { SystemValues } from "../../../types/types";
import { CAM_BASE } from "../../../utils/config";
2025-09-12 13:28:14 +01:00
2025-11-04 17:04:19 +00:00
const camBase = import.meta.env.MODE !== "development" ? CAM_BASE : "";
2025-09-12 13:28:14 +01:00
export async function handleSystemSave(values: SystemValues) {
2025-09-12 08:21:52 +01:00
const payload = {
// Build JSON
id: "GLOBAL--Device",
fields: [
2025-09-12 13:28:14 +01:00
{ property: "propDeviceName", value: values.deviceName },
{ property: "propSNTPServer", value: values.sntpServer },
{
property: "propSNTPIntervalMinutes",
value: Number(values.sntpInterval),
},
{ property: "propLocalTimeZone", value: values.timeZone },
2025-09-12 08:21:52 +01:00
],
};
2025-09-12 08:21:52 +01:00
try {
2025-11-04 17:04:19 +00:00
const response = await fetch(`${camBase}/api/update-config`, {
2025-09-12 08:21:52 +01:00
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(payload),
});
2025-09-12 08:21:52 +01:00
if (!response.ok) {
const text = await response.text().catch(() => "");
2025-11-04 17:04:19 +00:00
throw new Error(`HTTP ${response.status} ${response.statusText}${text ? ` - ${text}` : ""}`);
}
2025-09-12 08:21:52 +01:00
} catch (err) {
if (err instanceof Error) {
toast.error(`Failed to save system settings: ${err.message}`);
console.error(err);
} else {
toast.error("An unexpected error occurred while saving.");
console.error("Unknown error:", err);
}
2025-09-12 08:21:52 +01:00
}
}
export async function handleSystemRecall() {
2025-11-04 17:04:19 +00:00
const url = `${camBase}/api/fetch-config?id=GLOBAL--Device`;
2025-09-12 08:21:52 +01:00
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 7000);
2025-09-12 08:21:52 +01:00
try {
const response = await fetch(url, {
method: "GET",
headers: { Accept: "application/json" },
signal: controller.signal,
});
2025-09-12 08:21:52 +01:00
if (!response.ok) {
const text = await response.text().catch(() => "");
2025-11-04 17:04:19 +00:00
throw new Error(`HTTP ${response.status} ${response.statusText}${text ? ` - ${text}` : ""}`);
2025-09-12 08:21:52 +01:00
}
2025-09-12 08:21:52 +01:00
const data = await response.json();
2025-09-12 08:21:52 +01:00
const deviceName = data?.propDeviceName?.value ?? null;
const sntpServer = data?.propSNTPServer?.value ?? null;
const timeZone = data?.propLocalTimeZone?.value ?? null;
2025-09-12 08:21:52 +01:00
const sntpIntervalRaw = data?.propSNTPIntervalMinutes?.value;
let sntpInterval =
2025-11-04 17:04:19 +00:00
typeof sntpIntervalRaw === "number" ? sntpIntervalRaw : Number.parseInt(String(sntpIntervalRaw).trim(), 10);
2025-09-12 08:21:52 +01:00
if (!Number.isFinite(sntpInterval)) {
sntpInterval = 60;
}
2025-09-12 08:21:52 +01:00
return { deviceName, sntpServer, sntpInterval, timeZone };
} catch (err) {
if (err instanceof Error) {
toast.error(`Error: ${err.message}`);
} else {
toast.error("An unexpected error occurred");
}
2025-09-12 08:21:52 +01:00
return null;
} finally {
clearTimeout(timeoutId);
}
}