|
|
|
|
@ -3,7 +3,7 @@ import { useMutation, useQuery } from "@tanstack/react-query";
|
|
|
|
|
import { STATUSES } from "./config";
|
|
|
|
|
import { usePointSelection } from "./stores/usePointSelection";
|
|
|
|
|
import { RANGE_FILTERS_KEYS, usePendingPointsFilters } from "./stores/usePendingPointsFilters";
|
|
|
|
|
import { fieldHasChanged } from "./utils.js";
|
|
|
|
|
import { appendFiltersInUse } from "./utils.js";
|
|
|
|
|
import { useMode } from "./stores/useMode.js";
|
|
|
|
|
|
|
|
|
|
export const BASE_URL = import.meta.env.VITE_API_URL;
|
|
|
|
|
@ -18,13 +18,13 @@ export const api = axios.create({
|
|
|
|
|
xsrfCookieName: "csrftoken",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const getApiName = () => {
|
|
|
|
|
export const useDbTableName = () => {
|
|
|
|
|
const {isImportMode} = useMode();
|
|
|
|
|
if (isImportMode) return "pre_placement_points";
|
|
|
|
|
return "placement_points";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getSourceLayerName = () => {
|
|
|
|
|
export const useSourceLayerName = () => {
|
|
|
|
|
const {isImportMode} = useMode();
|
|
|
|
|
if (isImportMode) return "public.service_preplacementpoint";
|
|
|
|
|
return "public.points_with_dist";
|
|
|
|
|
@ -46,11 +46,11 @@ const enrichParamsWithRegionFilter = (params, region) => {
|
|
|
|
|
return resultParams;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getPoints = async (params, region, link = "placement_points") => {
|
|
|
|
|
export const getPoints = async (params, region, dbTable = "placement_points") => {
|
|
|
|
|
const resultParams = enrichParamsWithRegionFilter(params, region);
|
|
|
|
|
|
|
|
|
|
const { data } = await api.get(
|
|
|
|
|
`/api/${link}/?${resultParams.toString()}`
|
|
|
|
|
`/api/${dbTable}/?${resultParams.toString()}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
@ -91,9 +91,9 @@ export const importPoints = async (id) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useGetTotalInitialPointsCount = () => {
|
|
|
|
|
const link = getApiName();
|
|
|
|
|
const dbTable = useDbTableName();
|
|
|
|
|
return useQuery(
|
|
|
|
|
["all-initial-count", link],
|
|
|
|
|
["all-initial-count", dbTable],
|
|
|
|
|
async () => {
|
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
|
page: 1,
|
|
|
|
|
@ -101,13 +101,13 @@ export const useGetTotalInitialPointsCount = () => {
|
|
|
|
|
"status[]": [STATUSES.pending],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await getPoints(params, null, link);
|
|
|
|
|
return await getPoints(params, null, dbTable);
|
|
|
|
|
},
|
|
|
|
|
{ select: (data) => data.count }
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useGetFilteredPendingPointsCount = () => {
|
|
|
|
|
export const useGetFilteredPendingPointsCount = (isMerge) => {
|
|
|
|
|
const { filters, ranges } = usePendingPointsFilters();
|
|
|
|
|
const {
|
|
|
|
|
prediction,
|
|
|
|
|
@ -128,45 +128,61 @@ export const useGetFilteredPendingPointsCount = () => {
|
|
|
|
|
"categories[]": categories,
|
|
|
|
|
"included[]": includedIds,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RANGE_FILTERS_KEYS.map((filterKey) => {
|
|
|
|
|
if (!fieldHasChanged(filters, ranges, filterKey).result) return;
|
|
|
|
|
if (/d[0-9]/.test(filterKey)) {
|
|
|
|
|
params.append('dist_to_group__gt', [
|
|
|
|
|
filterKey.split('d')[1],
|
|
|
|
|
filters[`${filterKey}__gt`] - 1
|
|
|
|
|
].join(','));
|
|
|
|
|
params.append('dist_to_group__lt', [
|
|
|
|
|
filterKey.split('d')[1],
|
|
|
|
|
filters[`${filterKey}__lt`] + 1
|
|
|
|
|
].join(','));
|
|
|
|
|
} else {
|
|
|
|
|
params.append(`${filterKey}__gt`, filters[`${filterKey}__gt`] - 1);
|
|
|
|
|
params.append(`${filterKey}__lt`, filters[`${filterKey}__lt`] + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
appendFiltersInUse(params, filters, ranges);
|
|
|
|
|
if (isMerge) params.append("matching_status", "New")
|
|
|
|
|
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const link = getApiName();
|
|
|
|
|
const dbTable = useDbTableName();
|
|
|
|
|
|
|
|
|
|
return useQuery(
|
|
|
|
|
["filtered-points", filters, link, includedIds],
|
|
|
|
|
["filtered-points", filters, dbTable, includedIds],
|
|
|
|
|
async () => {
|
|
|
|
|
const params = getParams();
|
|
|
|
|
|
|
|
|
|
return await getPoints(params, region, link);
|
|
|
|
|
return await getPoints(params, region, dbTable);
|
|
|
|
|
},
|
|
|
|
|
{ select: (data) => data.count, keepPreviousData: true }
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useMergePointsToDb = () => {
|
|
|
|
|
const { filters, ranges } = usePendingPointsFilters();
|
|
|
|
|
const {
|
|
|
|
|
prediction,
|
|
|
|
|
categories,
|
|
|
|
|
} = filters;
|
|
|
|
|
const {
|
|
|
|
|
selection: { included },
|
|
|
|
|
} = usePointSelection();
|
|
|
|
|
|
|
|
|
|
const includedIds = [...included];
|
|
|
|
|
const getParams = () => {
|
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
|
page: 1,
|
|
|
|
|
page_size: 1,
|
|
|
|
|
"prediction_current[]": prediction,
|
|
|
|
|
"categories[]": categories,
|
|
|
|
|
"included[]": includedIds,
|
|
|
|
|
});
|
|
|
|
|
appendFiltersInUse(params, filters, ranges);
|
|
|
|
|
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: () => {
|
|
|
|
|
return api.post(
|
|
|
|
|
`/api/pre_placement_points/move_points/?${getParams().toString()}`
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useGetPermissions = () => {
|
|
|
|
|
return useQuery(["permissions"], async () => {
|
|
|
|
|
const { data } = await api.get("/api/me");
|
|
|
|
|
const { data } = await api.get("/api/me/");
|
|
|
|
|
|
|
|
|
|
if (data?.groups?.includes("Редактор")) {
|
|
|
|
|
return "editor";
|
|
|
|
|
@ -186,7 +202,7 @@ export const useUpdatePostamatId = () => {
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: (params) => {
|
|
|
|
|
return api.put(
|
|
|
|
|
`/api/placement_points/update_postamat_id?${params.toString()}`
|
|
|
|
|
`/api/placement_points/update_postamat_id/?${params.toString()}`
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
@ -243,12 +259,12 @@ export const useLastMLRun = () => {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useGetPendingPointsRange = (link) => {
|
|
|
|
|
export const useGetPendingPointsRange = (dbTable) => {
|
|
|
|
|
return useQuery(
|
|
|
|
|
["prediction-max-min", link],
|
|
|
|
|
["prediction-max-min", dbTable],
|
|
|
|
|
async () => {
|
|
|
|
|
const { data, isInitialLoading, isFetching } = await api.get(
|
|
|
|
|
`/api/${link}/filters?status[]=${STATUSES.pending}`
|
|
|
|
|
`/api/${dbTable}/filters/?status[]=${STATUSES.pending}`
|
|
|
|
|
);
|
|
|
|
|
return {data, isLoading: isInitialLoading || isFetching};
|
|
|
|
|
},
|
|
|
|
|
@ -282,7 +298,7 @@ export const useGetPendingPointsRange = (link) => {
|
|
|
|
|
|
|
|
|
|
export const useGetPopupPoints = (features) => {
|
|
|
|
|
const pointsIds = features.map(f => f.properties.id);
|
|
|
|
|
const link = getApiName();
|
|
|
|
|
const dbTable = useDbTableName();
|
|
|
|
|
|
|
|
|
|
const { data, isInitialLoading, isFetching } = useQuery(
|
|
|
|
|
["popup_data", features],
|
|
|
|
|
@ -292,7 +308,7 @@ export const useGetPopupPoints = (features) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { data } = await api.get(
|
|
|
|
|
`/api/${link}/?${params.toString()}`
|
|
|
|
|
`/api/${dbTable}/?${params.toString()}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return data.results;
|
|
|
|
|
|