64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import { useQuery, useMutation } from "@tanstack/react-query";
|
|
import type { OptionalBOF2Constants } from "../../../types/types";
|
|
import { CAMBASE } from "../../../utils/config";
|
|
|
|
const fetchOptionalConstants = async (format: string) => {
|
|
if (!format || format === "json") return null;
|
|
const response = await fetch(`${CAMBASE}/api/fetch-config?id=Dispatcher0-${format}-constants`);
|
|
if (!response.ok) {
|
|
throw new Error("Network response was not ok");
|
|
}
|
|
return response.json();
|
|
};
|
|
|
|
const postOptionalConstants = async (config: OptionalBOF2Constants) => {
|
|
const fields = [
|
|
{
|
|
property: "propSourceIdentifier",
|
|
value: config?.SCID,
|
|
},
|
|
{
|
|
property: "propTimeZoneType",
|
|
value: config?.timestampSource,
|
|
},
|
|
{
|
|
property: "propGpsFormat",
|
|
value: config?.GPSFormat,
|
|
},
|
|
];
|
|
|
|
if (config.FFID) {
|
|
fields.push({
|
|
property: "propFeedIdentifier",
|
|
value: config.FFID,
|
|
});
|
|
}
|
|
const updateConfigPayload = {
|
|
id: `Dispatcher0-${config.format?.toLowerCase()}-constants`,
|
|
fields: fields,
|
|
};
|
|
|
|
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 useOptionalConstants = (format: string) => {
|
|
const optionalConstantsQuery = useQuery({
|
|
queryKey: ["optionalConstants", format],
|
|
queryFn: () => fetchOptionalConstants(format),
|
|
enabled: !!format && format !== "json",
|
|
});
|
|
|
|
const optionalConstantsMutation = useMutation({
|
|
mutationKey: ["postOptionalConstants"],
|
|
mutationFn: postOptionalConstants,
|
|
});
|
|
return { optionalConstantsQuery, optionalConstantsMutation };
|
|
};
|