59 lines
2.3 KiB
TypeScript
59 lines
2.3 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 {MonitoringGpsRequest, PerformanceMonitoringVisit} from "@/types/gwcf";
|
|
import {compressImageFiles} from "@/lib/utils";
|
|
|
|
export function useSaveDepartureMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({performanceId, gps}: {performanceId: number; gps: MonitoringGpsRequest}) =>
|
|
nextApi.post<PerformanceMonitoringVisit>(`/monitoring/visit/departure/${performanceId}`, gps),
|
|
onSuccess: (_res, {performanceId}) => {
|
|
qc.invalidateQueries({ queryKey: qk.performance(performanceId) });
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useSaveArrivalMutation() {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async ({performanceId, gps, files}: {performanceId: number; gps: MonitoringGpsRequest; files?: File[]}) => {
|
|
const formData = new FormData();
|
|
const compressedFiles = files ? await compressImageFiles(files) : [];
|
|
formData.append("gps", new Blob([JSON.stringify(gps)], {type: "application/json"}));
|
|
compressedFiles.forEach((file) => formData.append("files", file, file.name));
|
|
return nextApi.postForm<PerformanceMonitoringVisit>(`/monitoring/visit/arrival/${performanceId}`, formData);
|
|
},
|
|
onSuccess: (_res, {performanceId}) => {
|
|
qc.invalidateQueries({ queryKey: qk.performance(performanceId) });
|
|
qc.invalidateQueries({ queryKey: ["performance", "list"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useMonitoringEvidenceQuery(performanceId: number, userId: string, enabled: boolean = true) {
|
|
return useQuery({
|
|
queryKey: qk.monitoringEvidence(performanceId, userId),
|
|
queryFn: () => nextApi.get<PerformanceMonitoringVisit[]>(`/monitoring/visit/evidence/${performanceId}/${encodeURIComponent(userId)}`),
|
|
enabled: enabled && performanceId > 0 && Boolean(userId),
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function useDownloadEvidenceZipMutation() {
|
|
return useMutation({
|
|
mutationFn: ({performanceId, userId}: {performanceId: number; userId: string}) =>
|
|
nextApi.get<ArrayBuffer>(
|
|
`/monitoring/visit/evidence/zip/${performanceId}/${encodeURIComponent(userId)}`,
|
|
{expectBinary: true}
|
|
),
|
|
});
|
|
}
|