You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.0 KiB
126 lines
3.0 KiB
import axios from "axios";
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
import { STATUSES } from "./config";
|
|
import { usePointSelection } from "./stores/usePointSelection";
|
|
import { usePendingPointsFilters } from "./stores/usePendingPointsFilters";
|
|
|
|
export const BASE_URL = import.meta.env.VITE_API_URL;
|
|
|
|
export const api = axios.create({
|
|
baseURL:
|
|
import.meta.env.MODE === "development"
|
|
? "http://localhost:5173/"
|
|
: BASE_URL,
|
|
withCredentials: true,
|
|
xsrfHeaderName: "X-CSRFToken",
|
|
xsrfCookieName: "csrftoken",
|
|
});
|
|
|
|
const enrichParamsWithRegionFilter = (params, region) => {
|
|
const resultParams = params ? params : new URLSearchParams();
|
|
|
|
if (region) {
|
|
if (region.type === "ao") {
|
|
resultParams.append("district[]", region.id);
|
|
}
|
|
|
|
if (region.type === "rayon") {
|
|
resultParams.append("area[]", region.id);
|
|
}
|
|
}
|
|
|
|
return resultParams;
|
|
};
|
|
|
|
export const getPoints = async (params, region) => {
|
|
const resultParams = enrichParamsWithRegionFilter(params, region);
|
|
|
|
const { data } = await api.get(
|
|
`/api/placement_points?${resultParams.toString()}`
|
|
);
|
|
|
|
return data;
|
|
};
|
|
|
|
export const exportPoints = async (params, region) => {
|
|
const resultParams = enrichParamsWithRegionFilter(params, region);
|
|
|
|
const { data } = await api.get(
|
|
`/api/placement_points/to_excel?${resultParams.toString()}`,
|
|
{ responseType: "arraybuffer" }
|
|
);
|
|
|
|
return data;
|
|
};
|
|
|
|
export const useGetTotalInitialPointsCount = () => {
|
|
return useQuery(
|
|
["all-initial-count"],
|
|
async () => {
|
|
const params = new URLSearchParams({
|
|
page: 1,
|
|
page_size: 1,
|
|
"status[]": [STATUSES.pending],
|
|
});
|
|
|
|
return await getPoints(params);
|
|
},
|
|
{ select: (data) => data.count }
|
|
);
|
|
};
|
|
|
|
export const useGetFilteredPendingPointsCount = () => {
|
|
const { filters } = usePendingPointsFilters();
|
|
const { prediction, categories, region } = filters;
|
|
const {
|
|
selection: { included },
|
|
} = usePointSelection();
|
|
|
|
const includedIds = [...included];
|
|
|
|
return useQuery(
|
|
["filtered-points", filters, includedIds],
|
|
async () => {
|
|
const params = new URLSearchParams({
|
|
page: 1,
|
|
page_size: 1,
|
|
"prediction_current[]": prediction,
|
|
"status[]": [STATUSES.pending],
|
|
"categories[]": categories,
|
|
"included[]": includedIds,
|
|
});
|
|
|
|
return await getPoints(params, region);
|
|
},
|
|
{ select: (data) => data.count, keepPreviousData: true }
|
|
);
|
|
};
|
|
|
|
export const useGetPermissions = () => {
|
|
return useQuery(["permissions"], async () => {
|
|
const { data } = await api.get("/api/me");
|
|
|
|
if (data?.groups?.includes("Редактор")) {
|
|
return "editor";
|
|
}
|
|
|
|
return "viewer";
|
|
});
|
|
};
|
|
|
|
export const useCanEdit = () => {
|
|
const { data } = useGetPermissions();
|
|
|
|
return data === "editor";
|
|
};
|
|
|
|
export const useUpdatePostamatId = () => {
|
|
return useMutation({
|
|
mutationFn: (params) => {
|
|
return api.put(
|
|
`/api/placement_points/update_postamat_id?${params.toString()}`
|
|
);
|
|
},
|
|
});
|
|
};
|