112 lines
3.2 KiB
TypeScript
112 lines
3.2 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 {ExcelUploadResult} from "@/types/common";
|
|
import {PerformanceCompact, PerformanceFull} from "@/types/gwcf";
|
|
|
|
export function usePerformanceListQuery(enabled: boolean = true) {
|
|
return useQuery({
|
|
queryKey: qk.performanceList(),
|
|
queryFn: () => nextApi.get<PerformanceFull[]>("/performance/list"),
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function usePerformanceCompactListQuery(enabled: boolean = true) {
|
|
return useQuery({
|
|
queryKey: qk.performanceCompactList(),
|
|
queryFn: () => nextApi.get<PerformanceCompact[]>("/performance/list/compact"),
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function usePerformanceDetailQuery(performanceId: number, enabled: boolean = true) {
|
|
return useQuery({
|
|
queryKey: qk.performance(performanceId),
|
|
queryFn: () => nextApi.get<PerformanceFull>(`/performance/${performanceId}`),
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function useUpdatePerformanceMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (data: PerformanceCompact) => nextApi.patch<void>("/performance", data),
|
|
onSuccess: (_res, data) => {
|
|
qc.invalidateQueries({ queryKey: qk.performance(data.id) });
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeletePerformanceMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (performanceId: number) => nextApi.del<void>(`/performance/${performanceId}`),
|
|
onSuccess: (_res, performanceId) => {
|
|
qc.invalidateQueries({ queryKey: qk.performance(performanceId) });
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUploadPerformanceExcelMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async (file: File): Promise<ExcelUploadResult> => {
|
|
const formData = new FormData();
|
|
formData.append("file", file);
|
|
return nextApi.postForm<ExcelUploadResult>("/performance/excel-upload", formData);
|
|
},
|
|
onSuccess: (res) => {
|
|
if (!res.hasErrorFile) {
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDownloadPerformanceExcelTemplateMutation() {
|
|
return useMutation({
|
|
mutationFn: () =>
|
|
nextApi.post<ArrayBuffer>(
|
|
"/performance/excel-template",
|
|
undefined,
|
|
{ expectBinary: true }
|
|
),
|
|
});
|
|
}
|
|
|
|
export function useExportPerformanceMutation() {
|
|
return useMutation({
|
|
mutationFn: ({performanceIds}: {performanceIds: number[]}) =>
|
|
nextApi.post<ArrayBuffer>(
|
|
"/performance/excel-download",
|
|
performanceIds.map(String),
|
|
{ expectBinary: true }
|
|
),
|
|
});
|
|
}
|
|
|
|
export function useDownloadAssignExcelTemplateMutation() {
|
|
return useMutation({
|
|
mutationFn: () =>
|
|
nextApi.post<ArrayBuffer>(
|
|
"/assign/written/excel-template",
|
|
undefined,
|
|
{ expectBinary: true }
|
|
),
|
|
});
|
|
}
|