43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||
|
|
import { CAM_BASE } from "../utils/config";
|
||
|
|
import type { CameraBlackBoardOptions } from "../types/types";
|
||
|
|
|
||
|
|
const getBlackboardData = async () => {
|
||
|
|
const response = await fetch(`${CAM_BASE}/api/blackboard`);
|
||
|
|
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"],
|
||
|
|
queryFn: getBlackboardData,
|
||
|
|
});
|
||
|
|
|
||
|
|
const mutation = useMutation({
|
||
|
|
mutationKey: ["cameraBlackboard"],
|
||
|
|
mutationFn: (options?: CameraBlackBoardOptions) =>
|
||
|
|
viewBlackboardData({
|
||
|
|
operation: options?.operation,
|
||
|
|
path: options?.path,
|
||
|
|
value: options?.value,
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
|
||
|
|
return { query, mutation };
|
||
|
|
};
|