2025-09-12 08:21:52 +01:00
|
|
|
// Used to fetch and load the configs for the camera side
|
|
|
|
|
|
|
|
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
2025-09-12 13:28:14 +01:00
|
|
|
import { toast } from "sonner";
|
2025-09-12 08:21:52 +01:00
|
|
|
|
|
|
|
|
const base_url = import.meta.env.VITE_OUTSIDE_BASEURL;
|
|
|
|
|
|
2025-09-12 13:28:14 +01:00
|
|
|
const fetchCameraSideConfig = async ({ queryKey }: { queryKey: string[] }) => {
|
2025-09-12 08:21:52 +01:00
|
|
|
const [, cameraSide] = queryKey;
|
|
|
|
|
const fetchUrl = `${base_url}/fetch-config?id=${cameraSide}`;
|
|
|
|
|
const response = await fetch(fetchUrl);
|
|
|
|
|
if (!response.ok) throw new Error("cannot react cameraSide ");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-12 13:28:14 +01:00
|
|
|
const updateCamerasideConfig = async (data: {
|
|
|
|
|
id: string;
|
|
|
|
|
friendlyName: string;
|
|
|
|
|
}) => {
|
2025-09-12 08:21:52 +01:00
|
|
|
const updateUrl = `${base_url}/update-config?id=${data.id}`;
|
|
|
|
|
|
|
|
|
|
const updateConfigPayload = {
|
|
|
|
|
id: data.id,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
property: "propLEDDriverControlURI",
|
|
|
|
|
value: data.friendlyName,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
const response = await fetch(updateUrl, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(updateConfigPayload),
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) throw new Error("Cannot reach update camera endpoint");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useFetchCameraConfig = (cameraSide: string) => {
|
|
|
|
|
const fetchedConfigQuery = useQuery({
|
|
|
|
|
queryKey: ["cameraSideConfig", cameraSide],
|
|
|
|
|
queryFn: fetchCameraSideConfig,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const updateConfigMutation = useMutation({
|
|
|
|
|
mutationKey: ["cameraSideConfigUpdate"],
|
|
|
|
|
mutationFn: updateCamerasideConfig,
|
2025-09-12 13:28:14 +01:00
|
|
|
onError: (error) => toast.error(error.message),
|
|
|
|
|
onSuccess: () => toast("Settings Successfully saved"),
|
2025-09-12 08:21:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data: fetchedConfigQuery.data,
|
|
|
|
|
isPending: fetchedConfigQuery.isPending,
|
|
|
|
|
isError: fetchedConfigQuery.isError,
|
|
|
|
|
updateCameraConfig: updateConfigMutation.mutate,
|
2025-09-12 13:28:14 +01:00
|
|
|
updateCameraConfigError: updateConfigMutation.error,
|
2025-09-12 08:21:52 +01:00
|
|
|
};
|
|
|
|
|
};
|