"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("/performance/list"), enabled, staleTime: 0, refetchOnMount: "always", }); } export function usePerformanceCompactListQuery(enabled: boolean = true) { return useQuery({ queryKey: qk.performanceCompactList(), queryFn: () => nextApi.get("/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(`/performance/${performanceId}`), enabled, staleTime: 0, refetchOnMount: "always", }); } export function useUpdatePerformanceMutation() { const qc = useQueryClient(); return useMutation({ mutationFn: (data: PerformanceCompact) => nextApi.patch("/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(`/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 => { const formData = new FormData(); formData.append("file", file); return nextApi.postForm("/performance/excel-upload", formData); }, onSuccess: (res) => { if (!res.hasErrorFile) { qc.invalidateQueries({ queryKey: ["performance", "list"] }); } }, }); } export function useDownloadPerformanceExcelTemplateMutation() { return useMutation({ mutationFn: () => nextApi.post( "/performance/excel-template", undefined, { expectBinary: true } ), }); } export function useDownloadAssignExcelTemplateMutation() { return useMutation({ mutationFn: () => nextApi.post( "/assign/written/excel-template", undefined, { expectBinary: true } ), }); }