110 lines
2.9 KiB
JavaScript
110 lines
2.9 KiB
JavaScript
import { useQuery, useMutation, useQueryClient } from "react-query";
|
|
import { fetch_ } from "../request-utils";
|
|
import {useConfig} from "../../ConfigProvider";
|
|
|
|
|
|
export const usePaths = (parent_id) => {
|
|
const queryClient = useQueryClient();
|
|
const config = useConfig();
|
|
return useQuery(
|
|
["paths", parent_id],
|
|
() => fetch_(`${config.BACKEND_HOST}/api/path/parent/${parent_id}`),
|
|
{
|
|
enabled: !!parent_id,
|
|
onSuccess: (data) => {
|
|
if(data) {
|
|
for (const pth of data)
|
|
{
|
|
queryClient.setQueryData(["path", pth.id], pth);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
);
|
|
};
|
|
|
|
|
|
export const usePath = (id) => {
|
|
const config = useConfig();
|
|
return useQuery(
|
|
["path", id],
|
|
() => fetch_(`${config.BACKEND_HOST}/api/path/${id}`),
|
|
{
|
|
enabled: !!id
|
|
}
|
|
);
|
|
};
|
|
|
|
export const useCreatePath = () => {
|
|
const config = useConfig();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation(
|
|
(data) => fetch_(`${config.BACKEND_HOST}/api/path/`, {
|
|
method: "POST",
|
|
body: JSON.stringify(data),
|
|
}),
|
|
{
|
|
onSuccess: (res) => {
|
|
queryClient.invalidateQueries(["paths", res.parent_id]);
|
|
queryClient.invalidateQueries("tree");
|
|
},
|
|
}
|
|
);
|
|
};
|
|
|
|
export const useUpdatePath = () => {
|
|
const config = useConfig();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation(
|
|
({ id, data }) => fetch_(`${config.BACKEND_HOST}/api/path/${id}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(data),
|
|
}),
|
|
{
|
|
onSuccess: (res) => {
|
|
queryClient.invalidateQueries(["paths", res.parent_id]);
|
|
queryClient.invalidateQueries(["path", res.id]);
|
|
queryClient.invalidateQueries("tree");
|
|
},
|
|
}
|
|
);
|
|
};
|
|
|
|
export const useDeletePath = () => {
|
|
const config = useConfig();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation(
|
|
(id) => fetch_(`${config.BACKEND_HOST}/api/path/${id}`, {
|
|
method: "DELETE",
|
|
}),
|
|
{
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries("paths");
|
|
queryClient.invalidateQueries("tree");
|
|
},
|
|
}
|
|
);
|
|
};
|
|
|
|
|
|
export const useMovePath = () => {
|
|
const queryClient = useQueryClient();
|
|
const config = useConfig();
|
|
|
|
return useMutation(
|
|
({path, direction}) => {
|
|
const apiEndpoint = `${config.BACKEND_HOST}/api/path/move_${direction}/${path.id}`;
|
|
return fetch_(apiEndpoint, {method: "PATCH"});
|
|
},
|
|
{
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries("paths");
|
|
queryClient.invalidateQueries("tree");
|
|
}
|
|
}
|
|
);
|
|
};
|