- refactor: replace Output component with OutputForms and update related hooks and types
This commit is contained in:
65
src/features/output/hooks/useBearer.ts
Normal file
65
src/features/output/hooks/useBearer.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import type { BearerTypeFields } from "../../../types/types";
|
||||
|
||||
const fetchBearerConfig = async (bearerConfig: string) => {
|
||||
const response = await fetch(`http://100.115.148.59/api/fetch-config?id=Dispatcher0-${bearerConfig}`, {
|
||||
method: "GET",
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
const postBearerConfig = async (config: BearerTypeFields) => {
|
||||
const channelConfigPayload = {
|
||||
id: `Dispatcher0-${config.format.toLowerCase()}`,
|
||||
fields: [
|
||||
{
|
||||
property: "propBackofficeURL",
|
||||
value: config.backOfficeURL,
|
||||
},
|
||||
{
|
||||
property: "propConnectTimeoutSeconds",
|
||||
value: config.connectTimeoutSeconds,
|
||||
},
|
||||
{
|
||||
property: "propPassword",
|
||||
value: config.password,
|
||||
},
|
||||
{
|
||||
property: "propReadTimeoutSeconds",
|
||||
value: config.readTimeoutSeconds,
|
||||
},
|
||||
{
|
||||
property: "propUsername",
|
||||
value: config.username,
|
||||
},
|
||||
],
|
||||
};
|
||||
const response = await fetch("http://192.168.202.121/api/update-config", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(channelConfigPayload),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const usePostBearerConfig = () => {
|
||||
const bearerMutation = useMutation({
|
||||
mutationFn: (query: BearerTypeFields) => postBearerConfig(query),
|
||||
mutationKey: ["outputs"],
|
||||
});
|
||||
return { bearerMutation };
|
||||
};
|
||||
|
||||
export const useGetBearerConfig = (bearerConfig: string) => {
|
||||
const bearerQuery = useQuery({
|
||||
queryKey: ["outputs", bearerConfig],
|
||||
queryFn: () => fetchBearerConfig(bearerConfig),
|
||||
});
|
||||
|
||||
return { bearerQuery };
|
||||
};
|
||||
47
src/features/output/hooks/useDispatcherConfig.ts
Normal file
47
src/features/output/hooks/useDispatcherConfig.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { useQuery, useMutation } from "@tanstack/react-query";
|
||||
import type { DispatcherConfig } from "../../../types/types";
|
||||
|
||||
const getDispatcherConfig = async () => {
|
||||
const response = await fetch(`http://192.168.202.121/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(`http://192.168.202.121/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 };
|
||||
};
|
||||
@@ -1,20 +0,0 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
const fetchOutputs = async (format: string) => {
|
||||
const response = await fetch(`http://100.115.148.59/api/fetch-config?id=Dispatcher0-${format}`, {
|
||||
method: "GET",
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const useGetOutputs = (format: string) => {
|
||||
const query = useQuery({
|
||||
queryKey: ["outputs", format],
|
||||
queryFn: () => fetchOutputs(format),
|
||||
});
|
||||
|
||||
return { query };
|
||||
};
|
||||
0
src/features/output/hooks/useOptionalConstants.tsx
Normal file
0
src/features/output/hooks/useOptionalConstants.tsx
Normal file
@@ -1,21 +0,0 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import type { BearerTypeFields } from "../../../types/types";
|
||||
|
||||
const postQuery = async (query: BearerTypeFields) => {
|
||||
const response = await fetch("/api/outputs", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ query }),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const usePostOutputs = () => {
|
||||
const mutation = useMutation({
|
||||
mutationFn: (query: BearerTypeFields) => postQuery(query),
|
||||
mutationKey: ["outputs"],
|
||||
});
|
||||
return { mutation };
|
||||
};
|
||||
Reference in New Issue
Block a user