49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { useQuery, useMutation } from "@tanstack/react-query";
|
|
import type { DispatcherConfig } from "../../../types/types";
|
|
import { CAMBASE } from "../../../utils/config";
|
|
|
|
const getDispatcherConfig = async () => {
|
|
const response = await fetch(`${CAMBASE}/api/fetch-config?id=Dispatcher0`);
|
|
if (!response.ok) {
|
|
throw new Error("Network response was not ok");
|
|
}
|
|
return response.json();
|
|
};
|
|
|
|
const postDispatcherConfig = async (config: DispatcherConfig) => {
|
|
const updateConfigPayload = {
|
|
id: "Dispatcher0",
|
|
fields: [
|
|
{
|
|
property: "propEnabled",
|
|
value: config.enabled,
|
|
},
|
|
{
|
|
property: "propFormat",
|
|
value: config.format,
|
|
},
|
|
],
|
|
};
|
|
const response = await fetch(`${CAMBASE}/api/update-config`, {
|
|
method: "POST",
|
|
body: JSON.stringify(updateConfigPayload),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error("Network response was not ok");
|
|
}
|
|
return response.json();
|
|
};
|
|
|
|
export const useDispatcherConfig = () => {
|
|
const dispatcherQuery = useQuery({
|
|
queryKey: ["dispatcherConfig"],
|
|
queryFn: () => getDispatcherConfig(),
|
|
});
|
|
|
|
const dispatcherMutation = useMutation({
|
|
mutationKey: ["postDispatcherConfig"],
|
|
mutationFn: (config: DispatcherConfig) => postDispatcherConfig(config),
|
|
});
|
|
return { dispatcherQuery, dispatcherMutation };
|
|
};
|