123 lines
4.4 KiB
TypeScript
123 lines
4.4 KiB
TypeScript
"use client";
|
|
|
|
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query";
|
|
import {nextApi} from "@/lib/http-client";
|
|
import {qk} from "@/query/key/keys";
|
|
import {AssignUserCount, MonitoringApplyUser, PerformanceApplyCount, PerformanceAssign, PerformanceFull} from "@/types/gwcf";
|
|
import {ExcelUploadResult} from "@/types/common";
|
|
|
|
const MONITORING_ASSIGN_TYPE = 1;
|
|
|
|
const mergeMonitoringAssigns = <T extends PerformanceFull>(performance: T, monitoringAssigns: PerformanceAssign[]): T => ({
|
|
...performance,
|
|
performanceAssignList: [
|
|
...(performance.performanceAssignList ?? []).filter((assign) => assign.assignType !== MONITORING_ASSIGN_TYPE),
|
|
...monitoringAssigns,
|
|
],
|
|
});
|
|
|
|
export function useMonitoringApplyUsersQuery(performanceId: number, enabled: boolean = true) {
|
|
return useQuery({
|
|
queryKey: qk.assignApplyUsers(performanceId),
|
|
queryFn: () => nextApi.get<MonitoringApplyUser[]>(`/assign/monitoring/apply-users/${performanceId}`),
|
|
enabled: enabled && performanceId > 0,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function useAssignUserCountsQuery(enabled: boolean = true) {
|
|
return useQuery({
|
|
queryKey: qk.assignUserCounts(),
|
|
queryFn: () => nextApi.get<AssignUserCount[]>("/assign/users/count"),
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function useMonitoringApplyUserCountsQuery(enabled: boolean = true) {
|
|
return useQuery({
|
|
queryKey: qk.assignApplyUserCounts(),
|
|
queryFn: () => nextApi.get<PerformanceApplyCount[]>("/assign/monitoring/apply-users/count"),
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function useSaveMonitoringAssignMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({performanceId, userIds}: {performanceId: number; userIds: string[]}) =>
|
|
nextApi.post<PerformanceAssign[]>("/assign/monitoring", {performanceId, userIds}),
|
|
onSuccess: (res, {performanceId}) => {
|
|
qc.setQueryData<PerformanceFull | undefined>(qk.performance(performanceId), (old) =>
|
|
old ? mergeMonitoringAssigns(old, res) : old
|
|
);
|
|
qc.setQueryData<PerformanceFull[] | undefined>(qk.performanceList(), (old) =>
|
|
old?.map((performance) => performance.id === performanceId ? mergeMonitoringAssigns(performance, res) : performance)
|
|
);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAssignConfirmMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({performanceId, userId}: {performanceId: number; userId: string}) =>
|
|
nextApi.patch<void>(`/assign/confirm/${performanceId}/${encodeURIComponent(userId)}`),
|
|
onSuccess: (_res, {performanceId, userId}) => {
|
|
qc.invalidateQueries({ queryKey: qk.assignConfirm(performanceId, userId) });
|
|
qc.invalidateQueries({ queryKey: qk.performance(performanceId) });
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAssignCancelMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({performanceId, userId}: {performanceId: number; userId: string}) =>
|
|
nextApi.patch<void>(`/assign/cancel/${performanceId}/${encodeURIComponent(userId)}`),
|
|
onSuccess: (_res, {performanceId, userId}) => {
|
|
qc.invalidateQueries({ queryKey: qk.assignConfirm(performanceId, userId) });
|
|
qc.invalidateQueries({ queryKey: qk.performance(performanceId) });
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useKonAssignConfirmMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({performanceId, userId}: {performanceId: number; userId: string}) =>
|
|
nextApi.post<string>(`/assign/confirm/${performanceId}/${encodeURIComponent(userId)}`),
|
|
onSuccess: (_res, {performanceId, userId}) => {
|
|
qc.invalidateQueries({ queryKey: qk.assignConfirm(performanceId, userId) });
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUploadWrittenAssignExcelMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async (file: File): Promise<ExcelUploadResult> => {
|
|
const formData = new FormData();
|
|
formData.append("file", file);
|
|
return nextApi.postForm<ExcelUploadResult>("/assign/written/excel-upload", formData);
|
|
},
|
|
onSuccess: (res) => {
|
|
if (!res.hasErrorFile) {
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
}
|
|
},
|
|
});
|
|
}
|