260722_01_ENH: 버그 수정 및 개선 사항 수정
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { Plus, X } from 'lucide-react'
|
||||
import { KriDialog } from '@/components/common/KriDialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
@@ -13,12 +13,15 @@ import {
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import { KriSingleSelect } from '@/components/common/KriAutoComplete'
|
||||
import ApplyFilterOption from './ListDialogOption/ApplyFilterOption'
|
||||
import ApplyFilterOption, { validateApplyFilterTarget } from './ListDialogOption/ApplyFilterOption'
|
||||
import { useDialog } from '@/hooks/useDialog'
|
||||
import type { DialogId } from '@/store/jotai/dialog'
|
||||
import type { FilterValueOption, ListFilterTarget } from '@/types/list'
|
||||
import { type ResultCode, ResultCodeType } from '@/types/project'
|
||||
import { type ProjectSurvey, type ResultCode, ResultCodeType } from '@/types/project'
|
||||
import { ListHeaderConst, LIST_BATCH_EDIT_FILE_DELETE_VALUE } from '@/const/listConst'
|
||||
import { COMMON_TEXT_REGEX } from '@/const/validationConst'
|
||||
import { validateValue } from '@/utils/validation'
|
||||
import { formatKoreanPhoneNumber } from '@/utils/phone'
|
||||
|
||||
export type BatchEditOption = {
|
||||
id: number
|
||||
@@ -33,11 +36,13 @@ export type BatchEditRow = {
|
||||
value: string
|
||||
}
|
||||
|
||||
export type BatchEditResultCodes = Partial<Record<ResultCodeType, number>>
|
||||
export type BatchEditResultCodes = Partial<Record<Exclude<ResultCodeType, ResultCodeType.SURVEY>, number>>
|
||||
export type BatchEditSurveyResultCodes = Record<number, number>
|
||||
|
||||
export type BatchEditPayload = {
|
||||
rows: BatchEditRow[]
|
||||
resultCodes: BatchEditResultCodes
|
||||
surveyResultCodes: { prjctSurveyIdx: number; resultCd: number }[]
|
||||
target: ListFilterTarget
|
||||
}
|
||||
|
||||
@@ -47,9 +52,11 @@ export type ListBatchEditDialogProps = {
|
||||
onOpenChange?: (open: boolean) => void
|
||||
headerOptions: BatchEditOption[]
|
||||
resultCodes: ResultCode[]
|
||||
surveys: ProjectSurvey[]
|
||||
filterSummary?: string[]
|
||||
initialRows?: BatchEditRow[]
|
||||
initialResultCodes?: BatchEditResultCodes
|
||||
initialSurveyResultCodes?: BatchEditSurveyResultCodes
|
||||
onConfirm: (payload: BatchEditPayload) => void | Promise<void>
|
||||
}
|
||||
|
||||
@@ -61,10 +68,9 @@ const createRow = (headerId: number, value = ''): BatchEditRow => ({
|
||||
|
||||
const NO_CHANGE = '__no_change__'
|
||||
|
||||
const resultCodeGroups: { type: ResultCodeType; label: string }[] = [
|
||||
const resultCodeGroups: { type: Exclude<ResultCodeType, ResultCodeType.SURVEY>; label: string }[] = [
|
||||
{ type: ResultCodeType.CONTACT, label: '컨택 결과' },
|
||||
{ type: ResultCodeType.INTERVIEW, label: '면접 결과' },
|
||||
{ type: ResultCodeType.SURVEY, label: '설문 결과' },
|
||||
]
|
||||
|
||||
const ListBatchEditDialog = ({
|
||||
@@ -73,21 +79,27 @@ const ListBatchEditDialog = ({
|
||||
onOpenChange,
|
||||
headerOptions,
|
||||
resultCodes,
|
||||
surveys,
|
||||
filterSummary,
|
||||
initialRows = [],
|
||||
initialResultCodes = {},
|
||||
initialSurveyResultCodes = {},
|
||||
onConfirm,
|
||||
}: ListBatchEditDialogProps) => {
|
||||
const { isDialogOpen, showDialog, hideDialog } = useDialog()
|
||||
const [rows, setRows] = useState<BatchEditRow[]>([])
|
||||
const [resultCodeSelections, setResultCodeSelections] = useState<BatchEditResultCodes>({})
|
||||
const [surveyResultSelections, setSurveyResultSelections] = useState<BatchEditSurveyResultCodes>({})
|
||||
const [target, setTarget] = useState<ListFilterTarget>('filtered')
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const [invalidRowId, setInvalidRowId] = useState<string | null>(null)
|
||||
const rowInputRefs = useRef<Map<string, HTMLInputElement>>(new Map())
|
||||
|
||||
const resolvedOpen = open ?? isDialogOpen(dialogId)
|
||||
const defaultHeaderId = headerOptions[0]?.id ?? 0
|
||||
|
||||
const isFileHeader = (headerId: number) => headerOptions.find((option) => option.id === headerId)?.listHderTy === ListHeaderConst.FILE
|
||||
const isTelHeader = (headerId: number) => headerOptions.find((option) => option.id === headerId)?.listHderTy === ListHeaderConst.TEL_NUM
|
||||
|
||||
useEffect(() => {
|
||||
if (!resolvedOpen) return
|
||||
@@ -99,8 +111,10 @@ const ListBatchEditDialog = ({
|
||||
setRows([])
|
||||
}
|
||||
setResultCodeSelections({ ...initialResultCodes })
|
||||
setSurveyResultSelections({ ...initialSurveyResultCodes })
|
||||
setTarget('filtered')
|
||||
}, [resolvedOpen, initialRows, initialResultCodes, defaultHeaderId])
|
||||
setInvalidRowId(null)
|
||||
}, [resolvedOpen, initialRows, initialResultCodes, initialSurveyResultCodes, defaultHeaderId])
|
||||
|
||||
const handleOpenChange = (nextOpen: boolean) => {
|
||||
onOpenChange?.(nextOpen)
|
||||
@@ -131,7 +145,7 @@ const ListBatchEditDialog = ({
|
||||
setRows((prev) => prev.map((row) => (row.id === id ? { ...row, value } : row)))
|
||||
}
|
||||
|
||||
const handleChangeResultCode = (type: ResultCodeType, value: string) => {
|
||||
const handleChangeResultCode = (type: Exclude<ResultCodeType, ResultCodeType.SURVEY>, value: string) => {
|
||||
setResultCodeSelections((prev) => {
|
||||
if (value === NO_CHANGE) {
|
||||
const next = { ...prev }
|
||||
@@ -142,17 +156,53 @@ const ListBatchEditDialog = ({
|
||||
})
|
||||
}
|
||||
|
||||
const handleChangeSurveyResultCode = (prjctSurveyIdx: number, value: string) => {
|
||||
setSurveyResultSelections((prev) => {
|
||||
if (value === NO_CHANGE) {
|
||||
const next = { ...prev }
|
||||
delete next[prjctSurveyIdx]
|
||||
return next
|
||||
}
|
||||
return { ...prev, [prjctSurveyIdx]: Number(value) }
|
||||
})
|
||||
}
|
||||
|
||||
const handleConfirm = async () => {
|
||||
setInvalidRowId(null)
|
||||
if (!validateApplyFilterTarget(target, filterSummary)) return
|
||||
|
||||
const normalized = rows.filter((row) => row.headerId > 0 && row.value.trim().length > 0)
|
||||
const hasResultCodeChange = Object.keys(resultCodeSelections).length > 0
|
||||
const surveyResultCodes = Object.entries(surveyResultSelections).map(([prjctSurveyIdx, resultCd]) => ({
|
||||
prjctSurveyIdx: Number(prjctSurveyIdx),
|
||||
resultCd,
|
||||
}))
|
||||
const hasResultCodeChange = Object.keys(resultCodeSelections).length > 0 || surveyResultCodes.length > 0
|
||||
if (normalized.length === 0 && !hasResultCodeChange) {
|
||||
alert('일괄 수정할 항목을 입력해주세요.')
|
||||
return
|
||||
}
|
||||
|
||||
// 직접 값을 타이핑하는 행(선택형/파일 삭제가 아닌 행)만 형식을 검증한다.
|
||||
for (const row of normalized) {
|
||||
const isFreeTextRow = !isFileHeader(row.headerId) && getValueOptions(row.headerId) === undefined
|
||||
if (!isFreeTextRow) continue
|
||||
|
||||
const valueCheck = validateValue(row.value, {
|
||||
maxLength: 2048,
|
||||
pattern: COMMON_TEXT_REGEX,
|
||||
patternMessage: '수정 값에 허용되지 않는 문자가 포함되어 있습니다.',
|
||||
})
|
||||
if (!valueCheck.valid) {
|
||||
alert(valueCheck.message)
|
||||
setInvalidRowId(row.id)
|
||||
rowInputRefs.current.get(row.id)?.focus()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
setSubmitting(true)
|
||||
try {
|
||||
await onConfirm({ rows: normalized, resultCodes: resultCodeSelections, target })
|
||||
await onConfirm({ rows: normalized, resultCodes: resultCodeSelections, surveyResultCodes, target })
|
||||
handleOpenChange(false)
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
@@ -215,6 +265,41 @@ const ListBatchEditDialog = ({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{surveys.length > 0 ? (
|
||||
<div className="rounded-md border p-4 space-y-3">
|
||||
<p className="font-semibold text-sm">설문별 결과 수정</p>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-2">
|
||||
{surveys.map((survey) => (
|
||||
<div key={survey.prjctSurveyIdx} className="space-y-1">
|
||||
<p className="text-sm text-muted-foreground">{survey.surveyName}</p>
|
||||
<Select
|
||||
value={
|
||||
survey.prjctSurveyIdx != null && surveyResultSelections[survey.prjctSurveyIdx] !== undefined
|
||||
? String(surveyResultSelections[survey.prjctSurveyIdx])
|
||||
: NO_CHANGE
|
||||
}
|
||||
onValueChange={(v) => survey.prjctSurveyIdx != null && handleChangeSurveyResultCode(survey.prjctSurveyIdx, v)}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="변경 안함" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={NO_CHANGE}>변경 안함</SelectItem>
|
||||
{resultCodes
|
||||
.filter((code) => code.resultCdTy === ResultCodeType.SURVEY && code.resultCd !== 0)
|
||||
.map((code) => (
|
||||
<SelectItem key={code.resultCdUUID} value={String(code.resultCd)}>
|
||||
{code.resultCdLbl}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<p className="font-semibold text-sm">항목값 수정</p>
|
||||
<div className="rounded-md border overflow-hidden">
|
||||
<div className="h-11 px-4 border-b bg-muted grid grid-cols-[1fr_2fr_64px] items-center text-sm font-semibold">
|
||||
@@ -263,11 +348,27 @@ const ListBatchEditDialog = ({
|
||||
widthClassName="w-full"
|
||||
className="w-full"
|
||||
/>
|
||||
) : isTelHeader(row.headerId) ? (
|
||||
<Input
|
||||
ref={(el) => {
|
||||
if (el) rowInputRefs.current.set(row.id, el)
|
||||
else rowInputRefs.current.delete(row.id)
|
||||
}}
|
||||
value={formatKoreanPhoneNumber(row.value)}
|
||||
onChange={(e) => handleChangeValue(row.id, e.target.value.replace(/\D/g, ''))}
|
||||
placeholder="수정할 값을 입력해주세요"
|
||||
aria-invalid={invalidRowId === row.id}
|
||||
/>
|
||||
) : (
|
||||
<Input
|
||||
ref={(el) => {
|
||||
if (el) rowInputRefs.current.set(row.id, el)
|
||||
else rowInputRefs.current.delete(row.id)
|
||||
}}
|
||||
value={row.value}
|
||||
onChange={(e) => handleChangeValue(row.id, e.target.value)}
|
||||
placeholder="수정할 값을 입력해주세요"
|
||||
aria-invalid={invalidRowId === row.id}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user