- updated base for endpoints
- added loading states - need to add new form for ftp type
This commit is contained in:
63
src/features/output/hooks/useOptionalConstants.ts
Normal file
63
src/features/output/hooks/useOptionalConstants.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
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 };
|
||||
};
|
||||
Reference in New Issue
Block a user