Files
kmusical-manage-front/src/app/(pages)/(main)/performance/page.tsx

207 lines
9.1 KiB
TypeScript

"use client";
import {useState} from "react";
import {Download, Upload} from "lucide-react";
import {Button} from "@/components/ui/button";
import {PagePanel} from "@/components/kmusical/PagePanel";
import {PerformanceFilterBar} from "@/components/kmusical/PerformanceFilterBar";
import {StatusPill} from "@/components/kmusical/StatusPill";
import {ExcelRegisterDialog} from "@/components/dialog/ExcelRegisterDialog";
import {PerformanceDetailDialog} from "@/components/dialog/PerformanceDetailDialog";
import {DataTable} from "@/components/data-table/DataTable";
import type {DataTableColumn} from "@/components/data-table/DataTable.type";
import {useCodesQuery} from "@/query/code";
import {
useDeletePerformanceMutation,
useExportPerformanceMutation,
useExportPerformanceTemplateMutation,
usePerformanceListQuery,
usePerformanceQuery,
useUpdatePerformanceMutation,
useUploadPerformanceExcelMutation,
} from "@/query/performance";
import {useDownloadReviewOriginalMutation, useReviewSlotsQuery} from "@/query/review";
import {filterPerformance, getCodeLabel, isInternalRole, isResearcherRole, type PerformanceFilterState} from "@/lib/kmusical/view";
import {getMonitoringStatusDisplay, isReviewComplete} from "@/lib/kmusical/performance-status";
import {downloadBlob, downloadExcelBuffer} from "@/lib/kmusical/file";
import {PERFORMANCE_FIELD_LABELS} from "@/lib/kmusical/field-labels";
import type {PerformanceFull} from "@/types/kmusical";
import {useAuth} from "@/hooks/useAuth";
import {useDialog} from "@/hooks/useDialog";
import ExcelDownloadButton from "@/components/kmusical/ExcelDownloadButton";
const PerformancePage = () => {
const {auth, isMonitoring} = useAuth();
const {data: codes} = useCodesQuery();
const {data = [], isLoading, refetch: refetchList} = usePerformanceListQuery();
const {data: reviewSlots = []} = useReviewSlotsQuery();
const {isDialogOpen, showDialog, hideDialog} = useDialog();
const [selectedId, setSelectedId] = useState("");
const [filters, setFilters] = useState<PerformanceFilterState>({
businessType: [],
area: [],
month: "",
monitoringDate: "",
slotStatus: {},
keyword: "",
});
const updateMutation = useUpdatePerformanceMutation();
const deleteMutation = useDeletePerformanceMutation();
const uploadMutation = useUploadPerformanceExcelMutation();
const templateMutation = useExportPerformanceTemplateMutation();
const exportMutation = useExportPerformanceMutation();
const originalMutation = useDownloadReviewOriginalMutation();
const detailOpen = isDialogOpen("performanceDetailDialog");
const registerOpen = isDialogOpen("performanceExcelRegisterDialog");
const {data: detail, refetch: refetchDetail} = usePerformanceQuery(detailOpen ? selectedId : "");
const canEdit = isInternalRole(auth.user?.userRole);
const canDelete = isResearcherRole(auth.user?.userRole);
const visibleData = isMonitoring
? data.filter((item) => Boolean(auth.user?.userId && item.performanceAssignList.some((assign) => assign.assignUser === auth.user?.userId)))
: data;
const filteredData = visibleData.filter((item) => filterPerformance(item, filters));
const downloadReview = async (performance: PerformanceFull, reviewSlot: number) => {
const review = Object.values(performance.performanceReviewMap ?? {}).find((item) => item.reviewSlot === reviewSlot);
if (!review?.uploadFileName) return;
const buffer = await originalMutation.mutateAsync({performanceId: performance.performanceId, reviewSlot});
downloadBlob(buffer, review.uploadFileName);
};
const columns: DataTableColumn<PerformanceFull>[] = [
{accessorKey: "performanceNo", header: PERFORMANCE_FIELD_LABELS.performanceNo, size: 60},
{accessorKey: "performanceId", header: PERFORMANCE_FIELD_LABELS.performanceId, size: 90},
{id: "businessType", header: PERFORMANCE_FIELD_LABELS.businessType, size: 100, cell: ({row}) => getCodeLabel(codes, "business_type", row.original.businessType)},
{id: "area", header: PERFORMANCE_FIELD_LABELS.performanceArea, size: 80, cell: ({row}) => getCodeLabel(codes, "area", row.original.performanceArea)},
{accessorKey: "artistName", header: PERFORMANCE_FIELD_LABELS.artistName, size: 160},
{accessorKey: "performanceName", header: PERFORMANCE_FIELD_LABELS.performanceName, size: 260},
{
accessorKey: "performanceDtInfo",
header: PERFORMANCE_FIELD_LABELS.performanceDtInfo,
size: 170,
enableSorting: false
},
{id: "monitoringDate", header: PERFORMANCE_FIELD_LABELS.monitoringDate,
size: 90,
accessorFn: (row) => row.performanceMonitoringSchedule.monitoringDate ?? 0,
cell: ({row}) => !row.original.performanceMonitoringSchedule.monitoringDate? "미정":`${row.original.performanceMonitoringSchedule.monitoringDate}`
},
{id: "monitoringTimeInfo", header: PERFORMANCE_FIELD_LABELS.monitoringTimeInfo,
size: 90,
accessorFn: (row) => row.performanceMonitoringSchedule.monitoringTimeInfo ?? 0,
cell: ({row}) => !row.original.performanceMonitoringSchedule.monitoringTimeInfo? "미정":`${row.original.performanceMonitoringSchedule.monitoringTimeInfo}`
},
...reviewSlots.map<DataTableColumn<PerformanceFull>>((slot) => ({
id: `review-${slot.reviewSlot}`,
header: slot.reviewLabel,
size: 110,
enableSorting: false,
cell: ({row}) => {
const assign = row.original.performanceAssignList.find((item) => item.reviewSlot === slot.reviewSlot);
const review = Object.values(row.original.performanceReviewMap ?? {}).find((item) => item.reviewSlot === slot.reviewSlot);
const display = getMonitoringStatusDisplay(getCodeLabel(codes, "review_status", review?.reviewStatus), {
reviewSlot: slot.reviewSlot,
reviewType: slot.reviewType,
assign,
review,
});
return (
<StatusPill
variant={display.variant}
onClick={isReviewComplete(review) ? () => downloadReview(row.original, slot.reviewSlot) : undefined}
tooltip={<span>담당자: {assign?.assignUser || "-"}</span>}
>
{display.label}
</StatusPill>
);
},
})),
];
const downloadTemplate = async () => {
downloadExcelBuffer(await templateMutation.mutateAsync(), "공연 등록 양식.xlsx");
};
const downloadPerformances = async () => {
if (filteredData.length === 0) {
alert("다운로드할 공연 정보가 없습니다.");
return;
}
downloadExcelBuffer(
await exportMutation.mutateAsync(filteredData.map((performance) => performance.performanceId)),
"공연 정보.xlsx"
);
};
return (
<div className="space-y-4">
<PagePanel>
<PerformanceFilterBar
codes={codes}
reviewSlots={reviewSlots}
filters={filters}
onFiltersChange={setFilters}
onSearch={() => refetchList()}
/>
</PagePanel>
<PagePanel className="overflow-x-auto">
<div className="mb-3 flex flex-wrap justify-end gap-2">
{canEdit && <Button type="button" onClick={() => showDialog("performanceExcelRegisterDialog")}><Upload className="h-4 w-4" />공연 등록</Button>}
<ExcelDownloadButton requestExcelExport={downloadPerformances} />
</div>
<DataTable
columns={columns}
data={filteredData}
getRowId={(row) => row.performanceId}
selectMode="click-event"
onRowClick={(performance) => {
setSelectedId(performance.performanceId);
showDialog("performanceDetailDialog");
}}
defaultPageSize={10}
emptyText={isLoading ? "조회 중입니다." : "조회된 공연이 없습니다."}
bodyRowClassName="[&>td]:py-2"
/>
</PagePanel>
<PerformanceDetailDialog
open={detailOpen}
onOpenChange={(open) => open ? showDialog("performanceDetailDialog") : hideDialog("performanceDetailDialog")}
performance={detail ?? data.find((item) => item.performanceId === selectedId) ?? null}
codes={codes}
reviewSlots={reviewSlots}
canEdit={canEdit}
canDelete={canDelete}
onSave={async (performance) => {
await updateMutation.mutateAsync(performance);
await Promise.all([refetchList(), refetchDetail()]);
}}
onDelete={async (performanceId) => {
await deleteMutation.mutateAsync(performanceId);
hideDialog("performanceDetailDialog");
setSelectedId("");
await refetchList();
}}
onRefresh={() => { void refetchList(); void refetchDetail(); }}
/>
<ExcelRegisterDialog
open={registerOpen}
onOpenChange={(open) => open ? showDialog("performanceExcelRegisterDialog") : hideDialog("performanceExcelRegisterDialog")}
title="공연 등록"
onDownloadTemplate={downloadTemplate}
onUpload={async (file) => {
const formData = new FormData();
formData.append("file", file, file.name);
return uploadMutation.mutateAsync(formData);
}}
isUploading={uploadMutation.isPending}
/>
</div>
);
};
export default PerformancePage;