253 lines
11 KiB
TypeScript
253 lines
11 KiB
TypeScript
"use client";
|
|
|
|
import {useEffect, useMemo, useRef, useState} from "react";
|
|
import {Search} from "lucide-react";
|
|
import {Button} from "@/components/ui/button";
|
|
import {Input} from "@/components/ui/input";
|
|
import {MultiFilterSelect} from "@/components/gwcf/MultiFilterSelect";
|
|
import {PagePanel} from "@/components/gwcf/PagePanel";
|
|
import {DataTable} from "@/components/data-table/DataTable";
|
|
import {DataTableColumn} from "@/components/data-table/DataTable.type";
|
|
import {SmsSendDialog} from "@/components/dialog/SmsSendDialog";
|
|
import {AssignMonitoringDialog} from "@/components/dialog/AssignMonitoringDialog";
|
|
import {useCodesQuery} from "@/query/code";
|
|
import {usePerformanceListQuery} from "@/query/performance";
|
|
import {
|
|
useMonitoringApplyUserCountsQuery,
|
|
useMonitoringApplyUsersQuery,
|
|
useSaveMonitoringAssignMutation,
|
|
} from "@/query/assign";
|
|
import {filterPerformance, getCodeLabel} from "@/lib/gwcf/view";
|
|
import {MONITORING_ASSIGN_TYPE} from "@/lib/gwcf/performance-status";
|
|
import {PerformanceAssign, PerformanceFull} from "@/types/gwcf";
|
|
import {useDialog} from "@/hooks/useDialog";
|
|
import {useMounted} from "@/hooks/useMounted";
|
|
import {LoadingOverlay} from "@/components/common/LoadingOverlay";
|
|
import {SMS_TEMPLATE_IDS} from "@/query/notification";
|
|
import {buildPerformanceSmsParams} from "@/lib/gwcf/notification";
|
|
import {PERFORMANCE_FIELD_LABELS} from "@/lib/gwcf/field-labels";
|
|
|
|
const AssignPage = () => {
|
|
const mounted = useMounted();
|
|
const {data: codes} = useCodesQuery();
|
|
const {isDialogOpen, showDialog, hideDialog} = useDialog();
|
|
const {data = [], refetch: refetchPerformanceList} = usePerformanceListQuery();
|
|
const {data: applyCounts = [], refetch: refetchApplyCounts} = useMonitoringApplyUserCountsQuery();
|
|
const saveAssignMutation = useSaveMonitoringAssignMutation();
|
|
const suppressAssignCloseRef = useRef(false);
|
|
|
|
const [filters, setFilters] = useState({
|
|
businessType: [] as string[],
|
|
area: [] as string[],
|
|
monitoringDate: "",
|
|
keyword: "",
|
|
});
|
|
const [selected, setSelected] = useState<PerformanceFull | null>(null);
|
|
const [assignSaveTargetIds, setAssignSaveTargetIds] = useState<string[]>([]);
|
|
|
|
type SmsMode = "assignConfirm" | "normal";
|
|
const [smsMode, setSmsMode] = useState<SmsMode>("assignConfirm");
|
|
const [smsTargetIds, setSmsTargetIds] = useState<string[]>([]);
|
|
const [smsTemplateIdx, setSmsTemplateIdx] = useState<number[]>([]);
|
|
const [smsDialogTitle, setSmsDialogTitle] = useState<string>("");
|
|
const [newSmsTemplateIdx, setNewSmsTemplateIdx] = useState<number|undefined>(undefined);
|
|
const smsAddParam = useMemo(() => selected ? buildPerformanceSmsParams(selected) : {}, [selected]);
|
|
|
|
const refreshAssignList = () => {
|
|
void refetchPerformanceList();
|
|
void refetchApplyCounts();
|
|
};
|
|
|
|
const updateFilters = (updater: (prev: typeof filters) => typeof filters) => {
|
|
setFilters(updater);
|
|
refreshAssignList();
|
|
};
|
|
|
|
useEffect(() => {
|
|
hideDialog("assignMonitoringDialog");
|
|
hideDialog("assignSmsSendDialog");
|
|
setSelected(null);
|
|
setAssignSaveTargetIds([]);
|
|
setSmsTargetIds([]);
|
|
}, []);
|
|
|
|
const assignOpen = isDialogOpen("assignMonitoringDialog");
|
|
const smsOpen = isDialogOpen("assignSmsSendDialog");
|
|
const selectedPerformanceId = selected?.id ?? 0;
|
|
const {data: applyUsers = []} = useMonitoringApplyUsersQuery(selectedPerformanceId, assignOpen && selectedPerformanceId > 0);
|
|
const smsTargetUsers = useMemo(
|
|
() => applyUsers
|
|
.filter((applyUser) => smsTargetIds.includes(applyUser.userId))
|
|
.map((applyUser) => applyUser.user),
|
|
[applyUsers, smsTargetIds]
|
|
);
|
|
|
|
const applyCountMap = useMemo(() => {
|
|
return new Map(applyCounts.map((item) => [String(item.performanceId), item.applyCnt]));
|
|
}, [applyCounts]);
|
|
|
|
const filteredData = useMemo(() => {
|
|
return data
|
|
.filter((item) => filterPerformance(item, filters))
|
|
.sort((a, b) => {
|
|
const aDate = a.performanceMonitoringSchedule?.monitoringDate ?? "";
|
|
const bDate = b.performanceMonitoringSchedule?.monitoringDate ?? "";
|
|
return bDate.localeCompare(aDate);
|
|
});
|
|
}, [data, filters]);
|
|
|
|
const openAssign = (performance: PerformanceFull) => {
|
|
suppressAssignCloseRef.current = false;
|
|
setSelected(performance);
|
|
showDialog("assignMonitoringDialog");
|
|
};
|
|
|
|
const applySavedAssignsToSelected = (performanceId: number, monitoringAssigns: PerformanceAssign[]) => {
|
|
setSelected((prev) => {
|
|
if (!prev || prev.id !== performanceId) return prev;
|
|
|
|
return {
|
|
...prev,
|
|
performanceAssignList: [
|
|
...(prev.performanceAssignList ?? []).filter((assign) => assign.assignType !== MONITORING_ASSIGN_TYPE),
|
|
...monitoringAssigns,
|
|
],
|
|
};
|
|
});
|
|
};
|
|
|
|
const getMonitoringAssigns = (performance: PerformanceFull) => {
|
|
return (performance.performanceAssignList ?? []).filter((assign) => assign.assignType === MONITORING_ASSIGN_TYPE);
|
|
};
|
|
|
|
const smsBeforeSendProps =
|
|
smsMode === "assignConfirm"
|
|
? {
|
|
isBeforeSendPending: saveAssignMutation.isPending,
|
|
onBeforeDefaultTemplateSend: async () => {
|
|
if (!selected) throw new Error("공연 정보가 없습니다.");
|
|
|
|
const monitoringAssigns = await saveAssignMutation.mutateAsync({
|
|
performanceId: selected.id,
|
|
userIds: assignSaveTargetIds,
|
|
});
|
|
|
|
applySavedAssignsToSelected(selected.id, monitoringAssigns);
|
|
},
|
|
onSent: refreshAssignList,
|
|
}
|
|
: {};
|
|
|
|
const columns = useMemo<DataTableColumn<PerformanceFull>[]>(() => [
|
|
{accessorKey: "id", header: PERFORMANCE_FIELD_LABELS.id, cell: ({row}) => row.original.id},
|
|
{accessorKey: "performanceName", header: PERFORMANCE_FIELD_LABELS.performanceName},
|
|
{id: "area", header: PERFORMANCE_FIELD_LABELS.performanceArea, cell: ({row}) => getCodeLabel(codes, "area", row.original.performanceArea)},
|
|
{accessorKey: "performanceDtInfo", header: PERFORMANCE_FIELD_LABELS.performanceDtInfo, enableSorting: false},
|
|
{accessorKey: "performanceTimeInfo", header: PERFORMANCE_FIELD_LABELS.performanceTimeInfo, enableSorting: false},
|
|
{id: "monitoringDate", header: PERFORMANCE_FIELD_LABELS.monitoringDate, accessorFn:(row) => row.performanceMonitoringSchedule?.monitoringDate ?? "", cell: ({row}) => row.original.performanceMonitoringSchedule?.monitoringDate ?? ""},
|
|
{id: "monitoringTime", header: PERFORMANCE_FIELD_LABELS.monitoringTimeInfo, cell: ({row}) => row.original.performanceMonitoringSchedule?.monitoringTimeInfo ?? ""},
|
|
{id: "applyCount", header: "신청자 수", accessorFn:((row) => applyCountMap.get(String(row.id)) ?? 0), cell: ({row}) => applyCountMap.get(String(row.original.id)) ?? 0},
|
|
{id: "requestCount", header: "확정요청 수", accessorFn:((row) => getMonitoringAssigns(row).length), cell: ({row}) => getMonitoringAssigns(row.original).length},
|
|
{id: "confirmedCount", header: "확정인원 수", accessorFn:((row) => getMonitoringAssigns(row).filter((assign) => assign.assignConfirm ).length), cell: ({row}) => getMonitoringAssigns(row.original).filter((assign) => assign.assignConfirm).length},
|
|
], [applyCountMap, codes]);
|
|
|
|
if(!mounted) return <LoadingOverlay force={true} />
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<PagePanel>
|
|
<div className="grid gap-2 md:grid-cols-[160px_160px_180px_1fr_auto]">
|
|
<MultiFilterSelect value={filters.businessType} onChange={(value) => updateFilters((prev) => ({...prev, businessType: value}))} options={codes.business_type} placeholder={PERFORMANCE_FIELD_LABELS.businessType} />
|
|
<MultiFilterSelect value={filters.area} onChange={(value) => updateFilters((prev) => ({...prev, area: value}))} options={codes.area} placeholder={PERFORMANCE_FIELD_LABELS.performanceArea} />
|
|
<Input type="date" value={filters.monitoringDate} onChange={(event) => updateFilters((prev) => ({...prev, monitoringDate: event.target.value}))} />
|
|
<Input value={filters.keyword} onChange={(event) => updateFilters((prev) => ({...prev, keyword: event.target.value}))} placeholder={`${PERFORMANCE_FIELD_LABELS.id} or ${PERFORMANCE_FIELD_LABELS.performanceName}`} />
|
|
<Button type="button" variant="outline" onClick={refreshAssignList}>
|
|
<Search className="h-4 w-4" />
|
|
검색
|
|
</Button>
|
|
</div>
|
|
</PagePanel>
|
|
|
|
<PagePanel className="overflow-x-auto">
|
|
<DataTable columns={columns} data={filteredData} getRowId={(row) => String(row.id)} selectMode="click-event" onRowClick={openAssign} defaultPageSize={10} />
|
|
</PagePanel>
|
|
|
|
<AssignMonitoringDialog
|
|
open={assignOpen}
|
|
onOpenChange={(open) => {
|
|
if (open) {
|
|
showDialog("assignMonitoringDialog");
|
|
return;
|
|
}
|
|
|
|
if (suppressAssignCloseRef.current) return;
|
|
|
|
hideDialog("assignMonitoringDialog");
|
|
refreshAssignList();
|
|
}}
|
|
selected={selected}
|
|
codes={codes}
|
|
applyUsers={applyUsers}
|
|
isSavingAssign={saveAssignMutation.isPending}
|
|
onSaveAssign={(performanceId, userIds) =>
|
|
saveAssignMutation.mutate(
|
|
{performanceId, userIds},
|
|
{onSuccess: (monitoringAssigns) => applySavedAssignsToSelected(performanceId, monitoringAssigns)}
|
|
)
|
|
}
|
|
onOpenAssignSms={(assignUserIds, smsUserIds) => {
|
|
suppressAssignCloseRef.current = true;
|
|
|
|
setAssignSaveTargetIds(assignUserIds);
|
|
setSmsTargetIds(smsUserIds);
|
|
|
|
setSmsMode("assignConfirm");
|
|
setSmsTemplateIdx([SMS_TEMPLATE_IDS.ASSIGN_CONFIRM])
|
|
setSmsDialogTitle("확정 문자 발송")
|
|
setNewSmsTemplateIdx(undefined);
|
|
|
|
showDialog("assignSmsSendDialog", false);
|
|
}}
|
|
onOpenNormalSms={(smsUserIds) => {
|
|
suppressAssignCloseRef.current = true;
|
|
|
|
setSmsTargetIds(smsUserIds);
|
|
|
|
setSmsMode("normal");
|
|
setSmsTemplateIdx([SMS_TEMPLATE_IDS.INFORM_ASSIGN, SMS_TEMPLATE_IDS.REMIND_ASSIGN])
|
|
setSmsDialogTitle("문자 발송")
|
|
setNewSmsTemplateIdx(SMS_TEMPLATE_IDS.CUSTOM);
|
|
|
|
showDialog("assignSmsSendDialog", false);
|
|
}}
|
|
/>
|
|
|
|
<SmsSendDialog
|
|
open={smsOpen}
|
|
onOpenChange={(open) => {
|
|
if (open) {
|
|
suppressAssignCloseRef.current = true;
|
|
showDialog("assignSmsSendDialog", false);
|
|
return;
|
|
}
|
|
|
|
hideDialog("assignSmsSendDialog");
|
|
refreshAssignList();
|
|
window.setTimeout(() => {
|
|
suppressAssignCloseRef.current = false;
|
|
}, 0);
|
|
}}
|
|
title={smsDialogTitle}
|
|
templateIdx={smsTemplateIdx}
|
|
newMessageTemplateIdx={newSmsTemplateIdx}
|
|
selectedUsers={smsTargetUsers}
|
|
addParams={smsAddParam}
|
|
denyParamKeys={["userCmpn", "userTeam", "userRole", "id"]}
|
|
{...smsBeforeSendProps}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default AssignPage;
|