2025-09-24 12:28:14 +01:00
|
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
|
|
|
import { CAM_BASE } from "../utils/config";
|
|
|
|
|
import type { CameraBlackBoardOptions } from "../types/types";
|
2025-10-06 14:21:56 +01:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { toast } from "sonner";
|
2025-09-24 12:28:14 +01:00
|
|
|
|
2025-10-01 10:59:10 +01:00
|
|
|
const getAllBlackboardData = async () => {
|
2025-10-06 14:21:56 +01:00
|
|
|
const response = await fetch(`${CAM_BASE}/api/blackboard`, {
|
|
|
|
|
signal: AbortSignal.timeout(500),
|
|
|
|
|
});
|
2025-09-24 12:28:14 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Failed to fetch blackboard data");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const viewBlackboardData = async (options: CameraBlackBoardOptions) => {
|
|
|
|
|
const response = await fetch(`${CAM_BASE}/api/blackboard`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify(options),
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Failed to fetch blackboard data");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useCameraBlackboard = () => {
|
|
|
|
|
const query = useQuery({
|
|
|
|
|
queryKey: ["cameraBlackboard"],
|
2025-10-01 10:59:10 +01:00
|
|
|
queryFn: getAllBlackboardData,
|
2025-09-24 12:28:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mutation = useMutation({
|
|
|
|
|
mutationKey: ["cameraBlackboard"],
|
|
|
|
|
mutationFn: (options?: CameraBlackBoardOptions) =>
|
|
|
|
|
viewBlackboardData({
|
|
|
|
|
operation: options?.operation,
|
|
|
|
|
path: options?.path,
|
|
|
|
|
value: options?.value,
|
|
|
|
|
}),
|
2025-10-06 14:21:56 +01:00
|
|
|
onError: (error) => {
|
2025-10-07 15:42:06 +01:00
|
|
|
toast.error(`${error.message}`, {
|
2025-10-06 15:18:58 +01:00
|
|
|
id: "viewBlackboardData",
|
|
|
|
|
});
|
2025-10-06 14:21:56 +01:00
|
|
|
},
|
2025-10-07 15:42:06 +01:00
|
|
|
onSuccess: () => toast.success("Sighting successfully added to alert list"),
|
2025-09-24 12:28:14 +01:00
|
|
|
});
|
|
|
|
|
|
2025-10-06 14:21:56 +01:00
|
|
|
useEffect(() => {
|
2025-10-06 15:18:58 +01:00
|
|
|
if (query.isError)
|
|
|
|
|
toast.error(query.error.message, { id: "viewBlackboardData" });
|
2025-10-06 14:21:56 +01:00
|
|
|
}, [query?.error?.message, query.isError]);
|
|
|
|
|
|
2025-09-24 12:28:14 +01:00
|
|
|
return { query, mutation };
|
|
|
|
|
};
|