251212_01_ENH : kri-editor 1차 완성, 프로젝트 정보 저장 api연동 완료
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo, useState } from 'react'
|
||||
import {useEffect, useMemo, useState} from 'react'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
@@ -11,104 +11,161 @@ import {
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import {Trash2, Plus, Download, Search, XIcon} from 'lucide-react'
|
||||
|
||||
export type ResultCode = {
|
||||
code: string
|
||||
label: string
|
||||
}
|
||||
|
||||
type Project = {
|
||||
prjctCd: string
|
||||
prjctName: string
|
||||
resultCodes: ResultCode[]
|
||||
}
|
||||
import {ProjectInfo, ResultCode} from "@/types/project";
|
||||
import {nextApi} from "@/lib/http-client";
|
||||
import {PROJECT_SEARCH_COUNT, RESULT_CODE_TYPE_OPTIONS, ResultCodeType} from "@/const/projectConst";
|
||||
import {useDebouncedValue} from "@/hooks/useDebouncedValue";
|
||||
import {SelectedProject} from "@/store/jotai/project";
|
||||
import {KriSingleSelect} from "@/components/common/KriAutoComplete";
|
||||
import {useSelectedProject} from "@/hooks/useSelectedProject";
|
||||
import {useMounted} from "@/hooks/useMounted";
|
||||
import _ from "lodash";
|
||||
|
||||
type ResultCodeDialogProps = {
|
||||
open?: boolean
|
||||
onOpenChange?: (open: boolean) => void
|
||||
onSave?: (codes: ResultCode[]) => void
|
||||
changeCodeType: ResultCodeType[]
|
||||
onChangeCodeType: (code: ResultCodeType[]) => void | Promise<void>
|
||||
sourceArray: ResultCode[]
|
||||
onSave?: (codes: ResultCode[]) => void | Promise<void>
|
||||
}
|
||||
|
||||
const ResultCodeDialog = ({ open, onOpenChange, onSave }: ResultCodeDialogProps) => {
|
||||
const ResultCodeDialog = ({
|
||||
open,
|
||||
onOpenChange,
|
||||
changeCodeType,
|
||||
onChangeCodeType,
|
||||
sourceArray,
|
||||
onSave,
|
||||
}: ResultCodeDialogProps) => {
|
||||
const mounted = useMounted();
|
||||
const { selectedProject } = useSelectedProject();
|
||||
const [importMode, setImportMode] = useState(false)
|
||||
const [selectedProject, setSelectedProject] = useState<string>('')
|
||||
const [resultCodeType, setResultCodeType] = useState<ResultCodeType>(ResultCodeType.CONTACT)
|
||||
const [resultCodeArray, setResultCodeArray] = useState<ResultCode[]>([])
|
||||
const [importCodeArray, setImportCodeArray] = useState<ResultCode[]>([])
|
||||
|
||||
const [resultCodes, setResultCodes] = useState<ResultCode[]>([
|
||||
{ code: '0', label: '컨택 성공' },
|
||||
{ code: '1', label: '통화 예약' },
|
||||
{ code: '2', label: '비수신' },
|
||||
{ code: '3', label: '부재' },
|
||||
{ code: '4', label: '휴가' },
|
||||
{ code: '5', label: '출장' },
|
||||
{ code: '6', label: '단순 거절' },
|
||||
{ code: '7', label: '강력 거절' },
|
||||
{ code: '8', label: '그런 사람 아님' },
|
||||
{ code: '9', label: '메일 발송' },
|
||||
{ code: '10', label: '독려' },
|
||||
{ code: '11', label: '퇴사' },
|
||||
{ code: '12', label: '기타' },
|
||||
{ code: '13', label: '조사 대상 아님' },
|
||||
{ code: '14', label: '수신 거부' },
|
||||
])
|
||||
const [searchKeyword, setSearchKeyword] = useState('')
|
||||
const debouncedKeyword = useDebouncedValue(searchKeyword, 500);
|
||||
const [projectArray, setProjectArray] = useState<SelectedProject[]>([]);
|
||||
const [importProject, setImportProject] = useState<SelectedProject|undefined>(undefined)
|
||||
|
||||
// 샘플 프로젝트 목록 (실제로는 API에서 가져옴)
|
||||
const projects: Project[] = [
|
||||
{
|
||||
prjctCd: '250701yd',
|
||||
prjctName: '250701yd',
|
||||
resultCodes: [
|
||||
{ code: '0', label: '완료' },
|
||||
{ code: '1', label: '거절' },
|
||||
{ code: '2', label: '진행중' },
|
||||
],
|
||||
},
|
||||
{
|
||||
prjctCd: '1509013',
|
||||
prjctName: '1509013_J2307KJC_KFB_저기 1차 카카오톡 AUTOAGI_#01250Group',
|
||||
resultCodes: [
|
||||
{ code: '0', label: '성공' },
|
||||
{ code: '1', label: '실패' },
|
||||
{ code: '2', label: '보류' },
|
||||
],
|
||||
},
|
||||
{
|
||||
prjctCd: '1503013',
|
||||
prjctName: '1503013_J2307KJC_KFB_저기 1차 카카오톡 AUTOAGI_그룹01테스트0',
|
||||
resultCodes: [
|
||||
{ code: '0', label: '승인' },
|
||||
{ code: '1', label: '반려' },
|
||||
],
|
||||
},
|
||||
]
|
||||
const searchTopProject = async (keyword: string) => {
|
||||
const res = await nextApi.get<ProjectInfo[]>(
|
||||
`/project/top/${PROJECT_SEARCH_COUNT}`,
|
||||
{ query: { keyword } }
|
||||
);
|
||||
|
||||
const [projectSearch, setProjectSearch] = useState('')
|
||||
const filteredProjects = useMemo(() => {
|
||||
const q = projectSearch.trim().toLowerCase()
|
||||
if (!q) return projects
|
||||
return projects.filter(p =>
|
||||
`${p.prjctCd} ${p.prjctName}`.toLowerCase().includes(q)
|
||||
)
|
||||
}, [projectSearch])
|
||||
setProjectArray(res.filter((prjct) => prjct.prjctCd !== selectedProject?.prjctCd));
|
||||
};
|
||||
|
||||
const selectedProjectData = useMemo(
|
||||
() => projects.find(p => p.prjctCd === selectedProject),
|
||||
[selectedProject]
|
||||
)
|
||||
const handleSelectProject = (info:SelectedProject|null) => {
|
||||
if(info === null) return;
|
||||
|
||||
setImportProject(info);
|
||||
}
|
||||
|
||||
const handleAddCode = () => {
|
||||
setResultCodes(prev => [...prev, { code: String(prev.length), label: '' }])
|
||||
if(resultCodeArray.length === 0) return setResultCodeArray([]);
|
||||
|
||||
const lastCode = resultCodeArray[resultCodeArray.length - 1].resultCd;
|
||||
setResultCodeArray(prev => [
|
||||
...prev,
|
||||
{
|
||||
resultCdUUID: crypto.randomUUID(),
|
||||
prjctIdx: selectedProject!.prjctIdx,
|
||||
resultCdTy: resultCodeType,
|
||||
resultCd: lastCode + 1,
|
||||
resultCdLbl: '',
|
||||
}
|
||||
])
|
||||
}
|
||||
|
||||
const handleDeleteCode = (index: number) => {
|
||||
setResultCodes(prev => prev.filter((_, i) => i !== index))
|
||||
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 handleCodeChange = (index: number, field: keyof ResultCode, value: string) => {
|
||||
setResultCodes(prev => {
|
||||
const next = [...prev]
|
||||
next[index] = { ...next[index], [field]: value }
|
||||
return next
|
||||
})
|
||||
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 = () => {
|
||||
@@ -116,38 +173,67 @@ const ResultCodeDialog = ({ open, onOpenChange, onSave }: ResultCodeDialogProps)
|
||||
}
|
||||
|
||||
const handleImportApply = () => {
|
||||
if (selectedProjectData) {
|
||||
setResultCodes(selectedProjectData.resultCodes)
|
||||
setImportMode(false)
|
||||
setSelectedProject('')
|
||||
setProjectSearch('')
|
||||
if (importProject) {
|
||||
setResultCodeArray(_.cloneDeep(importCodeArray.filter(rc => rc.resultCdTy === resultCodeType)));
|
||||
|
||||
if (!changeCodeType.includes(resultCodeType)) {
|
||||
onChangeCodeType([...changeCodeType, resultCodeType]);
|
||||
}
|
||||
|
||||
clearImport();
|
||||
}
|
||||
}
|
||||
|
||||
const handleImportCancel = () => {
|
||||
setImportMode(false)
|
||||
setSelectedProject('')
|
||||
setProjectSearch('')
|
||||
clearImport();
|
||||
}
|
||||
|
||||
const handleSave = () => {
|
||||
onSave?.(resultCodes)
|
||||
const dupOk = checkAllDuplicate();
|
||||
if (!dupOk) {
|
||||
return;
|
||||
}
|
||||
|
||||
onSave?.(resultCodeArray)
|
||||
onOpenChange?.(false)
|
||||
setImportMode(false)
|
||||
setSelectedProject('')
|
||||
setProjectSearch('')
|
||||
}
|
||||
|
||||
const handleClose = () => {
|
||||
onOpenChange?.(false)
|
||||
setImportMode(false)
|
||||
setSelectedProject('')
|
||||
setProjectSearch('')
|
||||
}
|
||||
|
||||
const clearImport = () => {
|
||||
setImportMode(false);
|
||||
setImportProject(undefined);
|
||||
setSearchKeyword('');
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if(open){
|
||||
setResultCodeType(ResultCodeType.CONTACT);
|
||||
setResultCodeArray(sourceArray);
|
||||
}
|
||||
else{
|
||||
clearImport();
|
||||
}
|
||||
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
if(importProject){
|
||||
nextApi.get<ResultCode[]>(`/project/result-code/${importProject.prjctIdx}`)
|
||||
.then((res) => setImportCodeArray(res));
|
||||
}
|
||||
}, [importProject]);
|
||||
|
||||
useEffect(() => {
|
||||
const keyword = debouncedKeyword.trim();
|
||||
searchTopProject(keyword);
|
||||
}, [debouncedKeyword]);
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleClose}>
|
||||
{/* ✅ importMode일 때 더 넓게(그리고 애니메이션으로 부드럽게) */}
|
||||
{/* importMode일 때 더 넓게(그리고 애니메이션으로 부드럽게) */}
|
||||
<DialogContent
|
||||
className={[
|
||||
'p-0 overflow-hidden',
|
||||
@@ -178,22 +264,47 @@ const ResultCodeDialog = ({ open, onOpenChange, onSave }: ResultCodeDialogProps)
|
||||
) : (
|
||||
<div className="flex items-center gap-2 w-130">
|
||||
<div className="relative flex-1">
|
||||
<Search className="w-4 h-4 text-gray-400 absolute left-3 top-1/2 -translate-y-1/2" />
|
||||
<Input
|
||||
value={projectSearch}
|
||||
onChange={(e) => setProjectSearch(e.target.value)}
|
||||
placeholder="프로젝트 코드/이름 검색..."
|
||||
className="h-9 pl-9"
|
||||
/>
|
||||
<KriSingleSelect
|
||||
<SelectedProject>
|
||||
items={projectArray}
|
||||
value={String(importProject?.prjctIdx || "")}
|
||||
onChange={() => {}}
|
||||
withInfo
|
||||
onItemChange={handleSelectProject}
|
||||
searchMode="inputOnly"
|
||||
onSearchValueChange={setSearchKeyword}
|
||||
placeholder="프로젝트 검색..."
|
||||
widthClassName="w-full"
|
||||
heightClassName="h-9"
|
||||
preventDeselect
|
||||
hideOptionCheck
|
||||
getLabel={(item) => `${item.prjctCd}_${item.prjctName}`}
|
||||
getValue={(item) => String(item.prjctIdx)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="px-6 pt-4 flex flex-wrap items-center gap-2">
|
||||
{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="p-6">
|
||||
<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>
|
||||
@@ -205,9 +316,9 @@ const ResultCodeDialog = ({ open, onOpenChange, onSave }: ResultCodeDialogProps)
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="max-h-[420px] overflow-y-auto">
|
||||
<div className="max-h-[420px] overflow-y-auto relative">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="sticky top-0 bg-white border-b">
|
||||
<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>
|
||||
@@ -215,20 +326,23 @@ const ResultCodeDialog = ({ open, onOpenChange, onSave }: ResultCodeDialogProps)
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y">
|
||||
{resultCodes.map((code, index) => (
|
||||
{resultCodeArray
|
||||
.filter((rc) => rc.resultCdTy === resultCodeType)
|
||||
.map((code, index) => (
|
||||
<tr key={index} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-2 align-middle">
|
||||
<Input
|
||||
value={code.code}
|
||||
onChange={(e) => handleCodeChange(index, 'code', e.target.value)}
|
||||
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}
|
||||
/>
|
||||
</td>
|
||||
<td className="px-4 py-2 align-middle">
|
||||
<Input
|
||||
value={code.label}
|
||||
onChange={(e) => handleCodeChange(index, 'label', e.target.value)}
|
||||
value={code.resultCdLbl}
|
||||
onChange={(e) => handleCodeChange(code.resultCdUUID, 'resultCdLbl', e.target.value)}
|
||||
className="h-9"
|
||||
disabled={importMode}
|
||||
/>
|
||||
@@ -268,20 +382,22 @@ const ResultCodeDialog = ({ open, onOpenChange, onSave }: ResultCodeDialogProps)
|
||||
<div className="font-semibold text-sm text-gray-900">미리보기</div>
|
||||
</div>
|
||||
|
||||
<div className="max-h-[180px] overflow-y-auto">
|
||||
{selectedProjectData ? (
|
||||
<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">
|
||||
<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">
|
||||
{selectedProjectData.resultCodes.map((code, idx) => (
|
||||
{importCodeArray
|
||||
.filter((rc) => rc.resultCdTy === resultCodeType)
|
||||
.map((code, idx) => (
|
||||
<tr key={idx}>
|
||||
<td className="px-4 py-2">{code.code}</td>
|
||||
<td className="px-4 py-2">{code.label}</td>
|
||||
<td className="px-4 py-2">{code.resultCd}</td>
|
||||
<td className="px-4 py-2">{code.resultCdLbl}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
@@ -301,7 +417,7 @@ const ResultCodeDialog = ({ open, onOpenChange, onSave }: ResultCodeDialogProps)
|
||||
<Button variant="outline" onClick={handleImportCancel}>
|
||||
취소
|
||||
</Button>
|
||||
<Button onClick={handleImportApply} disabled={!selectedProject} className="gap-2">
|
||||
<Button onClick={handleImportApply} disabled={!importProject} className="gap-2">
|
||||
<Download className="w-4 h-4" />
|
||||
가져오기 적용
|
||||
</Button>
|
||||
|
||||
Reference in New Issue
Block a user