432 lines
15 KiB
TypeScript
432 lines
15 KiB
TypeScript
'use client'
|
|
|
|
import {useEffect, useMemo, useState} from 'react'
|
|
import { KriDialog } from '@/components/common/KriDialog'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import {Trash2, Plus, Download} from 'lucide-react'
|
|
import {ProjectInfo, ResultCode, ResultCodeType} from "@/types/project";
|
|
import {PROJECT_SEARCH_COUNT, RESULT_CODE_TYPE_OPTIONS} from "@/const/projectConst";
|
|
import { useDebouncedValue } from "@kri/common/hooks";
|
|
import {SelectedProject} from "@/store/jotai/project";
|
|
import {KriSingleSelect} from "@/components/common/KriAutoComplete";
|
|
import {useSelectedProject} from "@/hooks/useSelectedProject";
|
|
import {useProjectTopQuery, useResultCodesQuery} from "@/query/project";
|
|
|
|
type ResultCodeDialogProps = {
|
|
open?: boolean
|
|
onOpenChange?: (open: boolean) => void
|
|
changeCodeType: ResultCodeType[]
|
|
onChangeCodeType: (code: ResultCodeType[]) => void | Promise<void>
|
|
sourceArray: ResultCode[]
|
|
onSave?: (codes: ResultCode[]) => void | Promise<void>
|
|
}
|
|
|
|
const ResultCodeDialog = ({
|
|
open,
|
|
onOpenChange,
|
|
changeCodeType,
|
|
onChangeCodeType,
|
|
sourceArray,
|
|
onSave,
|
|
}: ResultCodeDialogProps) => {
|
|
const { selectedProject } = useSelectedProject();
|
|
const [importMode, setImportMode] = useState(false);
|
|
const [resultCodeType, setResultCodeType] = useState<ResultCodeType>(ResultCodeType.CONTACT);
|
|
const [resultCodeArray, setResultCodeArray] = useState<ResultCode[]>([]);
|
|
const [importProject, setImportProject] = useState<SelectedProject|undefined>(undefined);
|
|
|
|
const [searchKeyword, setSearchKeyword] = useState('');
|
|
const debouncedKeyword = useDebouncedValue(searchKeyword, 500);
|
|
|
|
const topQuery = useProjectTopQuery(PROJECT_SEARCH_COUNT, debouncedKeyword, !!open && importMode);
|
|
|
|
const projectArray = useMemo(() => {
|
|
const arr = topQuery.data ?? [];
|
|
return arr.filter((p) => p.prjctCd !== selectedProject?.prjctCd);
|
|
}, [topQuery.data, selectedProject?.prjctCd]);
|
|
|
|
const importPrjctIdx = importProject?.prjctIdx ?? 0;
|
|
const importCodesQuery = useResultCodesQuery(importPrjctIdx, !!open && importMode && importPrjctIdx > 0);
|
|
const importCodeArray = importCodesQuery.data ?? [];
|
|
|
|
const handleSelectProject = (info: ProjectInfo | null) => {
|
|
if (!info) return;
|
|
|
|
setImportProject(info);
|
|
};
|
|
|
|
const handleAddCode = () => {
|
|
if(!resultCodeArray || resultCodeArray.length === 0){
|
|
setResultCodeArray([]);
|
|
return;
|
|
}
|
|
|
|
const lastCode = resultCodeArray.at(-1)!.resultCd;
|
|
setResultCodeArray(prev => [
|
|
...prev,
|
|
{
|
|
resultCdUUID: crypto.randomUUID(),
|
|
prjctIdx: selectedProject!.prjctIdx,
|
|
resultCdTy: resultCodeType,
|
|
resultCd: lastCode + 1,
|
|
resultCdLbl: '',
|
|
isDefault: false,
|
|
}
|
|
])
|
|
}
|
|
|
|
const handleDeleteCode = (resultCd: number) => {
|
|
setResultCodeArray(prev =>
|
|
prev.filter(
|
|
rc => !(rc.resultCdTy === resultCodeType && rc.resultCd === resultCd),
|
|
),
|
|
);
|
|
};
|
|
|
|
const handleCodeChange = (resultCdUUID: string, field: keyof ResultCode, value: string) => {
|
|
let replaceValue:string|number = value;
|
|
|
|
if(field === 'resultCd'){
|
|
value = value.replace(/[^0-9]/g, '');
|
|
replaceValue = Number(value);
|
|
}
|
|
|
|
setResultCodeArray(prev => {
|
|
const idx = prev.findIndex(
|
|
rc => rc.resultCdUUID === resultCdUUID,
|
|
);
|
|
|
|
if (idx === -1) return prev; // 못 찾으면 그대로
|
|
|
|
const next = [...prev];
|
|
next[idx] = {
|
|
...next[idx],
|
|
[field]: replaceValue,
|
|
};
|
|
return next;
|
|
});
|
|
|
|
if (!changeCodeType.includes(resultCodeType)) {
|
|
onChangeCodeType([...changeCodeType, resultCodeType]);
|
|
}
|
|
}
|
|
|
|
const checkAllDuplicate = (): boolean => {
|
|
const map = new Map<string, ResultCode[]>();
|
|
|
|
for (const row of resultCodeArray) {
|
|
if (row.resultCd === undefined || row.resultCd === null) continue;
|
|
if (!row.resultCdTy) continue;
|
|
|
|
const key = `${row.resultCdTy}-${row.resultCd}`;
|
|
|
|
const list = map.get(key) ?? [];
|
|
list.push(row);
|
|
map.set(key, list);
|
|
}
|
|
|
|
// 타입별/코드별로 2개 이상 있는 것만 추려내기
|
|
const duplicatedList: string[] = [];
|
|
|
|
for (const [key, list] of map.entries()) {
|
|
if (list.length > 1) {
|
|
const [ty, cd] = key.split("-");
|
|
duplicatedList.push(`(${ty}) ${cd}`);
|
|
}
|
|
}
|
|
|
|
if (duplicatedList.length > 0) {
|
|
alert(`중복된 코드가 있습니다:\n${duplicatedList.join(", ")}`);
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
const checkDuplicate = (
|
|
resultCdUUID: string,
|
|
resultCd: number,
|
|
target: HTMLInputElement
|
|
) => {
|
|
const hasDup = resultCodeArray.some(
|
|
(rc) => rc.resultCdTy === resultCodeType && rc.resultCd === resultCd && rc.resultCdUUID !== resultCdUUID
|
|
);
|
|
if (hasDup) {
|
|
alert("코드가 중복됩니다.");
|
|
// 바로 focus()를 호출하면 blur 연쇄가 생길 수 있어 살짝 지연
|
|
setTimeout(() => {
|
|
target.focus();
|
|
target.select(); // 선택까지 해주면 바로 수정하기 편함
|
|
}, 50);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
const handleImportClick = () => {
|
|
setImportMode(true)
|
|
}
|
|
|
|
const handleImportApply = () => {
|
|
if (importProject) {
|
|
setResultCodeArray(structuredClone(importCodeArray.filter(rc => rc.resultCdTy === resultCodeType)));
|
|
|
|
if (!changeCodeType.includes(resultCodeType)) {
|
|
onChangeCodeType([...changeCodeType, resultCodeType]);
|
|
}
|
|
|
|
clearImport();
|
|
}
|
|
}
|
|
|
|
const handleImportCancel = () => {
|
|
clearImport();
|
|
}
|
|
|
|
const handleSave = () => {
|
|
const dupOk = checkAllDuplicate();
|
|
if (!dupOk) {
|
|
return;
|
|
}
|
|
|
|
onSave?.(resultCodeArray)
|
|
onOpenChange?.(false)
|
|
}
|
|
|
|
const handleClose = () => {
|
|
onOpenChange?.(false)
|
|
}
|
|
|
|
const clearImport = () => {
|
|
setImportMode(false);
|
|
setImportProject(undefined);
|
|
setSearchKeyword('');
|
|
}
|
|
|
|
useEffect(() => {
|
|
if(open){
|
|
setResultCodeType(ResultCodeType.CONTACT);
|
|
setResultCodeArray(sourceArray);
|
|
}
|
|
else{
|
|
clearImport();
|
|
}
|
|
|
|
}, [open]);
|
|
|
|
return (
|
|
<KriDialog
|
|
open={open ?? false}
|
|
onOpenChange={(nextOpen) => {
|
|
if (!nextOpen) handleClose()
|
|
}}
|
|
title={<div className="text-2xl font-bold">결과 코드 설정</div>}
|
|
contentClassName={[
|
|
'overflow-hidden',
|
|
'transition-all duration-200',
|
|
importMode ? 'sm:max-w-6xl' : 'sm:max-w-3xl',
|
|
'w-[calc(100vw-2rem)]',
|
|
'max-h-[85vh]',
|
|
'gap-2',
|
|
].join(' ')}
|
|
bodyClassName="min-h-0 p-0"
|
|
footerClassName="px-6 py-4 border-t bg-white"
|
|
footer={
|
|
importMode ? (
|
|
<>
|
|
<Button variant="outline" onClick={handleImportCancel}>
|
|
취소
|
|
</Button>
|
|
<Button onClick={handleImportApply} disabled={!importProject} className="gap-2">
|
|
<Download className="w-4 h-4" />
|
|
가져오기 적용
|
|
</Button>
|
|
</>
|
|
) : (
|
|
<>
|
|
<Button variant="outline" onClick={handleClose}>
|
|
취소
|
|
</Button>
|
|
<Button onClick={handleSave}>
|
|
저장
|
|
</Button>
|
|
</>
|
|
)
|
|
}
|
|
>
|
|
{/* importMode일 때 더 넓게(그리고 애니메이션으로 부드럽게) */}
|
|
|
|
{/* 상단 버튼 영역 */}
|
|
<div className="px-6 pt-1 pb-3 border-b bg-white flex justify-end">
|
|
{!importMode ? (
|
|
<Button
|
|
variant="outline"
|
|
onClick={handleImportClick}
|
|
className="h-9 px-3 gap-2"
|
|
>
|
|
<Download className="w-4 h-4" />
|
|
가져오기
|
|
</Button>
|
|
) : (
|
|
<div className="flex items-center gap-2 w-130">
|
|
<div className="relative flex-1">
|
|
<KriSingleSelect<ProjectInfo>
|
|
items={projectArray}
|
|
value={String(importProject?.prjctIdx || "")}
|
|
onChange={() => {}}
|
|
withInfo
|
|
onItemChange={handleSelectProject}
|
|
searchMode="inputOnly"
|
|
onSearchValueChange={setSearchKeyword}
|
|
placeholder="프로젝트 검색..."
|
|
widthClassName="w-full"
|
|
heightClassName="h-9"
|
|
preventDeselect
|
|
hideOptionCheck
|
|
badgeLabelClassName="max-w-[420px]"
|
|
getLabel={(item) => `${item.prjctCd}_${item.prjctName}`}
|
|
getValue={(item) => String(item.prjctIdx)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div className="px-6 py-2 flex flex-wrap items-center gap-1">
|
|
{RESULT_CODE_TYPE_OPTIONS
|
|
.filter((opt) => opt.value !== ResultCodeType.SURVEY)
|
|
.map((opt) => (
|
|
<Button
|
|
key={opt.value}
|
|
type="button"
|
|
variant={resultCodeType === opt.value ? "default" : "outline"}
|
|
size="sm"
|
|
onClick={() => setResultCodeType(opt.value)}
|
|
className="px-3"
|
|
>
|
|
{opt.label}
|
|
</Button>
|
|
))}
|
|
</div>
|
|
{/* 본문 */}
|
|
<div className={importMode ? 'grid grid-cols-1 lg:grid-cols-2' : 'block'}>
|
|
{/* 왼쪽: 결과 코드 */}
|
|
<div className="px-6 pb-6">
|
|
<div className="rounded-lg border bg-white overflow-hidden">
|
|
<div className="px-4 py-3 border-b bg-gray-50 flex items-center justify-between">
|
|
<div className="font-semibold text-sm text-gray-900">결과 코드 목록</div>
|
|
{!importMode && (
|
|
<Button onClick={handleAddCode} variant="outline" size="sm" className="gap-2">
|
|
<Plus className="w-4 h-4" />
|
|
추가
|
|
</Button>
|
|
)}
|
|
</div>
|
|
|
|
<div className="max-h-[420px] overflow-y-auto relative">
|
|
<table className="w-full text-sm">
|
|
<thead className="sticky top-0 bg-white border-b z-10">
|
|
<tr className="text-gray-600">
|
|
<th className="px-4 py-2 text-left w-36">코드</th>
|
|
<th className="px-4 py-2 text-left">레이블</th>
|
|
<th className="px-2 py-2 w-12" />
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y">
|
|
{resultCodeArray
|
|
.filter((rc) => rc.resultCdTy === resultCodeType)
|
|
.map((code, index) => {
|
|
const isLocked = code.isDefault;
|
|
return (
|
|
<tr key={index} className="hover:bg-gray-50">
|
|
<td className="px-4 py-2 align-middle">
|
|
<Input
|
|
value={code.resultCd}
|
|
onChange={(e) => handleCodeChange(code.resultCdUUID, 'resultCd', e.target.value)}
|
|
onBlur={(e) => checkDuplicate(code.resultCdUUID, Number(e.target.value), e.target)}
|
|
className="h-9"
|
|
disabled={importMode || isLocked}
|
|
/>
|
|
</td>
|
|
<td className="px-4 py-2 align-middle">
|
|
<Input
|
|
value={code.resultCdLbl}
|
|
onChange={(e) => handleCodeChange(code.resultCdUUID, 'resultCdLbl', e.target.value)}
|
|
className="h-9"
|
|
disabled={importMode || isLocked}
|
|
/>
|
|
</td>
|
|
<td className="px-2 py-2 align-middle">
|
|
{!importMode && !isLocked && (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={() => handleDeleteCode(code.resultCd)}
|
|
aria-label="delete"
|
|
>
|
|
<Trash2 className="w-4 h-4 text-red-600" />
|
|
</Button>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
{!importMode && (
|
|
<div className="px-4 py-3 border-t bg-gray-50 text-xs text-gray-500">
|
|
Tip: 코드 값은 중복되지 않게 유지해주세요.
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* 오른쪽: importMode일 때만 */}
|
|
{importMode && (
|
|
<div className="px-6 pb-6 border-t lg:border-t-0 lg:border-l bg-white">
|
|
{/* 미리보기 */}
|
|
<div className="rounded-lg border bg-white overflow-hidden">
|
|
<div className="px-4 py-3 border-b bg-gray-50 flex items-center justify-between">
|
|
<div className="font-semibold text-sm text-gray-900">미리보기</div>
|
|
</div>
|
|
|
|
<div className="max-h-[420px] overflow-y-auto relative">
|
|
{importProject ? (
|
|
<table className="w-full text-sm">
|
|
<thead className="sticky top-0 bg-white border-b z-10">
|
|
<tr className="text-gray-600">
|
|
<th className="px-4 py-2 text-left w-32">코드</th>
|
|
<th className="px-4 py-2 text-left">레이블</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y">
|
|
{importCodeArray
|
|
.filter((rc) => rc.resultCdTy === resultCodeType)
|
|
.map((code, idx) => (
|
|
<tr key={idx}>
|
|
<td className="px-4 py-2">
|
|
<div className="flex h-9 items-center">{code.resultCd}</div>
|
|
</td>
|
|
<td className="px-4 py-2">
|
|
<div className="flex h-9 items-center">{code.resultCdLbl}</div>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
) : (
|
|
<div className="p-4 text-sm text-gray-500">프로젝트를 선택하세요.</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
</KriDialog>
|
|
)
|
|
}
|
|
|
|
export default ResultCodeDialog
|