r- efactored reboots api to use useMutation for maintainability and readability.

- updated zoom debug
This commit is contained in:
2025-10-07 09:56:24 +01:00
parent a4e1e6e16f
commit b18d4272ec
5 changed files with 65 additions and 22 deletions

View File

@@ -36,10 +36,10 @@ const CameraSettingFields = ({
const parsed = parseRTSPUrl(initialData?.propURI?.value); const parsed = parseRTSPUrl(initialData?.propURI?.value);
useEffect(() => { useEffect(() => {
if (!query.data) return; if (!query?.data) return;
const apiZoom = getZoomLevel(query.data); const apiZoom = getZoomLevel(query.data);
onZoomLevelChange?.(apiZoom); onZoomLevelChange?.(apiZoom);
}, [query.data, onZoomLevelChange]); }, [query?.data, onZoomLevelChange]);
const getZoomLevel = (levelstring: string | undefined) => { const getZoomLevel = (levelstring: string | undefined) => {
switch (levelstring) { switch (levelstring) {

View File

@@ -1,15 +0,0 @@
export async function handleSoftReboot() {
const response = await fetch(
`http://192.168.75.11/api/restart-flexiai`
);
if (!response.ok) throw new Error("Failed to Software Reboot");
else alert("Software reboot triggered!");
}
export async function handleHardReboot() {
const response = await fetch(
`http://192.168.75.11/api/restart-hardware`
);
if (!response.ok) throw new Error("Failed to Hardware Reboot");
else alert("Hardware reboot triggered!");
}

View File

@@ -1,6 +1,6 @@
import { Formik, Field, Form } from "formik"; import { Formik, Field, Form } from "formik";
import FormGroup from "../components/FormGroup"; import FormGroup from "../components/FormGroup";
import { handleSoftReboot, handleHardReboot } from "./Reboots"; import { useReboots } from "../../../hooks/useReboots";
import { timezones } from "./timezones"; import { timezones } from "./timezones";
import SystemFileUpload from "./SystemFileUpload"; import SystemFileUpload from "./SystemFileUpload";
import type { SystemValues, SystemValuesErrors } from "../../../types/types"; import type { SystemValues, SystemValuesErrors } from "../../../types/types";
@@ -9,6 +9,8 @@ import { useSystemConfig } from "../../../hooks/useSystemConfig";
const SystemConfigFields = () => { const SystemConfigFields = () => {
const { saveSystemSettings, systemSettingsData, saveSystemSettingsLoading } = const { saveSystemSettings, systemSettingsData, saveSystemSettingsLoading } =
useSystemConfig(); useSystemConfig();
const { softRebootMutation, hardRebootMutation } = useReboots();
const initialvalues: SystemValues = { const initialvalues: SystemValues = {
deviceName: systemSettingsData?.deviceName ?? "", deviceName: systemSettingsData?.deviceName ?? "",
timeZone: systemSettingsData?.timeZone ?? "", timeZone: systemSettingsData?.timeZone ?? "",
@@ -18,6 +20,7 @@ const SystemConfigFields = () => {
}; };
const handleSubmit = (values: SystemValues) => saveSystemSettings(values); const handleSubmit = (values: SystemValues) => saveSystemSettings(values);
const validateValues = (values: SystemValues) => { const validateValues = (values: SystemValues) => {
const errors: SystemValuesErrors = {}; const errors: SystemValuesErrors = {};
const interval = Number(values.sntpInterval); const interval = Number(values.sntpInterval);
@@ -29,6 +32,14 @@ const SystemConfigFields = () => {
return errors; return errors;
}; };
const handleSoftReboot = async () => {
await softRebootMutation.mutate();
};
const handleHardReboot = async () => {
await hardRebootMutation.mutate();
};
return ( return (
<Formik <Formik
initialValues={initialvalues} initialValues={initialvalues}
@@ -149,14 +160,18 @@ const SystemConfigFields = () => {
className="bg-red-600 text-white px-4 py-2 rounded hover:bg-red-700 transition w-full md:w-[50%]" className="bg-red-600 text-white px-4 py-2 rounded hover:bg-red-700 transition w-full md:w-[50%]"
onClick={handleSoftReboot} onClick={handleSoftReboot}
> >
Software Reboot {softRebootMutation.isPending || isSubmitting
? "Rebooting..."
: "Software Reboot"}
</button> </button>
<button <button
type="button" type="button"
className="bg-red-600 text-white px-4 py-2 rounded hover:bg-red-700 transition w-full md:w-[50%]" className="bg-red-600 text-white px-4 py-2 rounded hover:bg-red-700 transition w-full md:w-[50%]"
onClick={handleHardReboot} onClick={handleHardReboot}
> >
Hardware Reboot {hardRebootMutation.isPending || isSubmitting
? "Rebooting"
: "Hardware Reboot"}
</button> </button>
</Form> </Form>
)} )}

View File

@@ -6,6 +6,7 @@ import {
import { CAM_BASE } from "../utils/config"; import { CAM_BASE } from "../utils/config";
import type { zoomConfig, ZoomInOptions } from "../types/types"; import type { zoomConfig, ZoomInOptions } from "../types/types";
import { toast } from "sonner"; import { toast } from "sonner";
import { useEffect } from "react";
async function zoomIn(options: ZoomInOptions) { async function zoomIn(options: ZoomInOptions) {
const response = await fetch( const response = await fetch(
@@ -38,8 +39,10 @@ export const useCameraZoom = (options: zoomConfig) => {
const mutation = useMutation({ const mutation = useMutation({
mutationKey: ["zoomIn"], mutationKey: ["zoomIn"],
mutationFn: (options: ZoomInOptions) => zoomIn(options), mutationFn: (options: ZoomInOptions) => zoomIn(options),
onError: () => { onError: (err) => {
toast.error("Failed to update zoom settings", { id: "zoom" }); toast.error(`Failed to update zoom settings: ${err.message}`, {
id: "zoom",
});
}, },
}); });
@@ -47,5 +50,10 @@ export const useCameraZoom = (options: zoomConfig) => {
queryKey: ["fetchZoomInConfig", options], queryKey: ["fetchZoomInConfig", options],
queryFn: fetchZoomInConfig, queryFn: fetchZoomInConfig,
}); });
useEffect(() => {
if (query.isError) toast.error(query.error.message, { id: "hardReboot" });
}, [query?.error?.message, query.isError]);
return { mutation, query }; return { mutation, query };
}; };

35
src/hooks/useReboots.ts Normal file
View File

@@ -0,0 +1,35 @@
import { CAM_BASE } from "../utils/config";
import { useMutation } from "@tanstack/react-query";
import { toast } from "sonner";
async function handleSoftReboot() {
const response = await fetch(`${CAM_BASE}/api/restart-flexiai`);
if (!response.ok) throw new Error("Failed to Software Reboot");
return "Software reboot triggered!";
}
async function handleHardReboot() {
const response = await fetch(`${CAM_BASE}/api/restart-hardware`);
if (!response.ok) throw new Error("Failed to Hardware Reboot");
return "Hardware reboot triggered!";
}
export const useReboots = () => {
const softRebootMutation = useMutation({
mutationKey: ["softReboot"],
mutationFn: handleSoftReboot,
onSuccess: () => toast.success("Software reboot triggered!"),
onError: (error) => toast.error(error.message),
});
const hardRebootMutation = useMutation({
mutationKey: ["hardReboot"],
mutationFn: handleHardReboot,
onSuccess: () => toast.success("Harware reboot triggered!"),
onError: (error) => toast.error(error.message),
});
return { softRebootMutation, hardRebootMutation };
};