93 lines
3.8 KiB
TypeScript
93 lines
3.8 KiB
TypeScript
"use client";
|
|
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import type { ExcelUploadResult } from "@kri/common";
|
|
import { nextApi } from "@/lib/http-client";
|
|
import { clientEnv } from "@/lib/env.client";
|
|
import { downloadFromApiUrl } from "@/lib/kmusical/file";
|
|
import { queryKeys } from "@/query/key/keys";
|
|
import type { PerformanceCompact, PerformanceFull, RequestFormType } from "@/types/kmusical";
|
|
|
|
const requestFormPath = (performanceId: string, formType: RequestFormType) =>
|
|
`/performance/request-form/upload/${encodeURIComponent(performanceId)}/${formType}`;
|
|
|
|
const requestFormDownloadUrl = (performanceId: string, formType: RequestFormType) =>
|
|
`${clientEnv.API_BASE}/performance/request-form/download/${encodeURIComponent(performanceId)}/${formType}`;
|
|
|
|
export const usePerformanceListQuery = () => useQuery({
|
|
queryKey: queryKeys.performance.list,
|
|
queryFn: () => nextApi.get<PerformanceFull[]>("/performance/list"),
|
|
});
|
|
|
|
export const usePerformanceCompactListQuery = () => useQuery({
|
|
queryKey: queryKeys.performance.listCompact,
|
|
queryFn: () => nextApi.get<PerformanceCompact[]>("/performance/list/compact"),
|
|
});
|
|
|
|
export const usePerformanceQuery = (performanceId: string) => useQuery({
|
|
queryKey: queryKeys.performance.detail(performanceId),
|
|
queryFn: () => nextApi.get<PerformanceFull>(`/performance/${encodeURIComponent(performanceId)}`),
|
|
enabled: Boolean(performanceId),
|
|
});
|
|
|
|
export function useUpdatePerformanceMutation() {
|
|
const client = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (request: PerformanceCompact) => nextApi.patch<void>("/performance", request),
|
|
onSuccess: () => client.invalidateQueries({ queryKey: queryKeys.performance.all }),
|
|
});
|
|
}
|
|
|
|
export function useDeletePerformanceMutation() {
|
|
const client = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (performanceId: string) => nextApi.del<void>(`/performance/${encodeURIComponent(performanceId)}`),
|
|
onSuccess: () => client.invalidateQueries({ queryKey: queryKeys.performance.all }),
|
|
});
|
|
}
|
|
|
|
export function useExportPerformanceMutation() {
|
|
return useMutation({
|
|
mutationFn: (performanceIds: string[]) =>
|
|
nextApi.post<ArrayBuffer>("/performance/excel/download", performanceIds, { expectBinary: true }),
|
|
});
|
|
}
|
|
|
|
export function useExportPerformanceTemplateMutation() {
|
|
return useMutation({
|
|
mutationFn: () => nextApi.post<ArrayBuffer>("/performance/excel/template", undefined, { expectBinary: true }),
|
|
});
|
|
}
|
|
|
|
export function useUploadPerformanceExcelMutation() {
|
|
const client = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (formData: FormData) => nextApi.postForm<ExcelUploadResult>("/performance/excel/upload", formData),
|
|
onSuccess: () => client.invalidateQueries({ queryKey: queryKeys.performance.all }),
|
|
});
|
|
}
|
|
|
|
/*
|
|
* 신청서 파일 업로드. 같은 공연·formType으로 다시 올리면 기존 파일이 교체된다.
|
|
* 응답 본문은 저장된 원본 파일명 문자열이다.
|
|
*/
|
|
export function useUploadRequestFormMutation() {
|
|
const client = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ performanceId, formType, formData }: { performanceId: string; formType: RequestFormType; formData: FormData }) =>
|
|
nextApi.postForm<string>(requestFormPath(performanceId, formType), formData),
|
|
onSuccess: () => client.invalidateQueries({ queryKey: queryKeys.performance.all }),
|
|
});
|
|
}
|
|
|
|
/*
|
|
* 신청서 파일 다운로드. Blob으로 받아 Content-Disposition의 파일명으로 저장한다.
|
|
* 파일이 없으면 404(HttpError)로 전파된다.
|
|
*/
|
|
export function useDownloadRequestFormMutation() {
|
|
return useMutation({
|
|
mutationFn: ({ performanceId, formType }: { performanceId: string; formType: RequestFormType }) =>
|
|
downloadFromApiUrl(requestFormDownloadUrl(performanceId, formType)),
|
|
});
|
|
}
|