2025-10-07 11:37:37 +01:00
|
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
|
|
|
import { CAM_BASE } from "../utils/config";
|
|
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { toast } from "sonner";
|
2025-11-03 15:01:13 +00:00
|
|
|
import type { BearerTypeFieldType } from "../types/types";
|
2025-10-07 11:37:37 +01:00
|
|
|
|
|
|
|
|
const getDispatcherConfig = async () => {
|
|
|
|
|
const response = await fetch(`${CAM_BASE}/api/fetch-config?id=Dispatcher`);
|
|
|
|
|
if (!response.ok) throw new Error("Cannot get dispatcher configuration");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateDispatcherConfig = async (data: BearerTypeFieldType) => {
|
|
|
|
|
const updateConfigPayload = {
|
|
|
|
|
id: "Dispatcher",
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
property: "propEnabled",
|
|
|
|
|
value: data.enabled,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "propFormat",
|
|
|
|
|
value: data.format,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
const response = await fetch(`${CAM_BASE}/api/update-config?id=Dispatcher`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(updateConfigPayload),
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) throw new Error("Cannot update dispatcher configuration");
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useCameraOutput = () => {
|
|
|
|
|
const dispatcherQuery = useQuery({
|
|
|
|
|
queryKey: ["dispatcher"],
|
|
|
|
|
queryFn: getDispatcherConfig,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const dispatcherMutation = useMutation({
|
|
|
|
|
mutationFn: updateDispatcherConfig,
|
|
|
|
|
mutationKey: ["dispatcherUpdate"],
|
|
|
|
|
onError: (error) => toast.error(error.message),
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
if (data) {
|
|
|
|
|
toast.success("Settings successfully updated");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (dispatcherQuery.isError) toast.error(dispatcherQuery.error.message);
|
|
|
|
|
}, [dispatcherQuery?.error?.message, dispatcherQuery.isError]);
|
|
|
|
|
|
2025-10-07 14:00:58 +01:00
|
|
|
return {
|
|
|
|
|
dispatcherQuery,
|
|
|
|
|
dispatcherMutation,
|
|
|
|
|
};
|
2025-10-07 11:37:37 +01:00
|
|
|
};
|