# Conflicts: # package-lock.json # package.json # src/components/dialog/MessageHistoryDialog.tsx # src/components/list-contact/ListContactEventPanel.tsx
467 lines
16 KiB
TypeScript
467 lines
16 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { RefreshCw, Trash2 } from 'lucide-react'
|
|
import { Button } from '@/components/ui/button'
|
|
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
|
|
import { KriDateTimePicker } from '@/components/common/KriDateTimePicker'
|
|
import CollapsibleHistorySection from '@/components/list-contact/CollapsibleHistorySection'
|
|
import { useAddListEventMutation, useDeleteListEventMutation, useListEventHistoryQuery, useReserveMutation } from '@/query/list'
|
|
import { useSendResultBySidQuery } from '@/query/message'
|
|
import { useInternalInfoQuery } from '@/query/internal'
|
|
import { clientEnv } from '@/lib/env.client'
|
|
import { ChannelType, isSendCancelable } from '@kri/common/noti-hub'
|
|
import { MESSAGE_TYPE_LABEL } from '@/const/messageTemplateConst'
|
|
import type { SendResultInfo } from '@kri/common/noti-hub'
|
|
import { useSendCancel } from '@/hooks/useSendCancel'
|
|
import { formatKst } from '@kri/common'
|
|
import { type ResultCode, ResultCodeType } from '@/types/project'
|
|
import { CONTACT_INTERVIEWER_ROLE, FIELD_INTERVIEWER_ROLE } from '@/const/userConst'
|
|
import { RESULT_CODE_RESERVATION, RESULT_CODE_UNUSED } from '@/const/projectConst'
|
|
import { cn } from '@/lib/utils'
|
|
import { getNearestFutureDateTime } from '@/lib/datetime'
|
|
import { useCtiLastCallHistId } from '@/hooks/useCtiLastCallHistId'
|
|
import { getCurrentPositionMeta } from '@kri/common/browser'
|
|
import type { listHeader, ListSearchWithFullResult } from '@/types/list'
|
|
import { buildListContactVariableValues } from '@/components/list-contact/listContactVariables'
|
|
import { escapeHtml, substituteVariables } from '@/lib/variableSubstitution'
|
|
import { sanitizeHtml } from '@/components/kri-editor/utils/sanitizeHtml'
|
|
import { isInternalManager } from '@/utils/userRole'
|
|
|
|
const RefreshIconButton = ({ onRefresh, isFetching }: { onRefresh: () => void; isFetching: boolean }) => (
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
aria-label="새로고침"
|
|
disabled={isFetching}
|
|
onClick={(e) => {
|
|
e.stopPropagation()
|
|
onRefresh()
|
|
}}
|
|
>
|
|
<RefreshCw className={cn('h-4 w-4', isFetching && 'animate-spin')} />
|
|
</Button>
|
|
)
|
|
|
|
type ContactEventSectionProps = {
|
|
title: string
|
|
prjctIdx: number
|
|
sid: string
|
|
resultCodeType: ResultCodeType
|
|
resultCodes: ResultCode[]
|
|
showReservation?: boolean
|
|
showInput?: boolean
|
|
submitting: boolean
|
|
onSubmit: (resultCd: number, memo: string, resveDt: string, listCallHistIdx: number | null) => Promise<void>
|
|
canDelete: boolean
|
|
}
|
|
|
|
const EventHistoryList = ({
|
|
isLoading,
|
|
history,
|
|
resultCodeType,
|
|
resultCodes,
|
|
canDelete = false,
|
|
onDelete,
|
|
isDeleting = false,
|
|
}: {
|
|
isLoading: boolean
|
|
history: ReturnType<typeof useListEventHistoryQuery>['data']
|
|
resultCodeType: ResultCodeType
|
|
resultCodes: ResultCode[]
|
|
canDelete?: boolean
|
|
onDelete?: (listEventHistIdx: number) => void
|
|
isDeleting?: boolean
|
|
}) => {
|
|
const filteredHistory = (history ?? [])
|
|
.filter((item) => item.eventTy === resultCodeType)
|
|
.sort((a, b) => new Date(b.regDt).getTime() - new Date(a.regDt).getTime())
|
|
|
|
const resolveLabel = (resultCd: number) =>
|
|
resultCodes.find((code) => code.resultCdTy === resultCodeType && code.resultCd === resultCd)?.resultCdLbl ??
|
|
String(resultCd)
|
|
|
|
const handleDelete = (listEventHistIdx: number) => {
|
|
if (!confirm('이력을 삭제하시겠습니까?')) return
|
|
onDelete?.(listEventHistIdx)
|
|
}
|
|
|
|
if (isLoading) {
|
|
return <div className="rounded-md border p-4 text-center text-sm text-muted-foreground">불러오는 중...</div>
|
|
}
|
|
|
|
if (filteredHistory.length === 0) {
|
|
return <div className="rounded-md border p-4 text-center text-sm text-muted-foreground">이력이 없습니다.</div>
|
|
}
|
|
|
|
return (
|
|
<div className="max-h-60 overflow-y-auto rounded-md border">
|
|
<table className="w-full text-sm">
|
|
<thead className="sticky top-0 bg-muted">
|
|
<tr>
|
|
<th className="p-2 text-left">결과</th>
|
|
<th className="p-2 text-left">메모</th>
|
|
<th className="p-2 text-left">등록자</th>
|
|
<th className="p-2 text-left">일시</th>
|
|
{canDelete ? <th className="p-2 text-left">삭제</th> : null}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{filteredHistory.map((item) => (
|
|
<tr key={item.listEventHistIdx} className="border-t">
|
|
<td className="p-2">
|
|
{item.resultCd}.{resolveLabel(item.resultCd)}
|
|
</td>
|
|
<td className="p-2">{item.memo || '-'}</td>
|
|
<td className="p-2">{item.regUser}</td>
|
|
<td className="p-2">{formatKst(item.regDt, 'dateHourMin')}</td>
|
|
{canDelete ? (
|
|
<td className="p-2">
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
aria-label="이력 삭제"
|
|
disabled={isDeleting}
|
|
onClick={() => handleDelete(item.listEventHistIdx)}
|
|
>
|
|
<Trash2 className="h-4 w-4" />
|
|
</Button>
|
|
</td>
|
|
) : null}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const ContactEventSection = ({
|
|
title,
|
|
prjctIdx,
|
|
sid,
|
|
resultCodeType,
|
|
resultCodes,
|
|
showReservation = false,
|
|
showInput = true,
|
|
submitting,
|
|
onSubmit,
|
|
canDelete,
|
|
}: ContactEventSectionProps) => {
|
|
const [resultCd, setResultCd] = useState('')
|
|
const [memo, setMemo] = useState('')
|
|
const [resveDt, setResveDt] = useState('')
|
|
|
|
const codes = resultCodes.filter((code) => code.resultCdTy === resultCodeType && code.resultCd !== RESULT_CODE_UNUSED)
|
|
const isReservationSelected = resultCd === String(RESULT_CODE_RESERVATION)
|
|
const isContactType = resultCodeType === ResultCodeType.CONTACT
|
|
|
|
const historyQuery = useListEventHistoryQuery(prjctIdx, sid, true)
|
|
const ctiLastCallHistId = useCtiLastCallHistId(prjctIdx, sid)
|
|
const listCallHistIdx = isContactType ? ctiLastCallHistId : null
|
|
const deleteEventMutation = useDeleteListEventMutation(prjctIdx, sid)
|
|
|
|
const handleResultCdChange = (next: string) => {
|
|
setResultCd(next)
|
|
if (next === String(RESULT_CODE_RESERVATION) && !resveDt) {
|
|
setResveDt(getNearestFutureDateTime())
|
|
}
|
|
}
|
|
|
|
const handleAdd = async () => {
|
|
if (!resultCd) {
|
|
alert('결과 코드를 선택해주세요.')
|
|
return
|
|
}
|
|
if (isReservationSelected && !resveDt) {
|
|
alert('예약 날짜/시간을 선택해주세요.')
|
|
return
|
|
}
|
|
if (isContactType && !listCallHistIdx) {
|
|
if (!confirm('통화 기록이 없습니다. 계속 진행하시겠습니까?')) return
|
|
}
|
|
|
|
await onSubmit(Number(resultCd), memo, isReservationSelected ? resveDt : '', listCallHistIdx)
|
|
setMemo('')
|
|
setResveDt('')
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-3 rounded-lg border bg-white p-4">
|
|
<h2 className="text-lg font-bold">{title}</h2>
|
|
|
|
{showInput ? (
|
|
<>
|
|
<RadioGroup value={resultCd} onValueChange={handleResultCdChange} className="grid grid-cols-2 gap-2">
|
|
{codes.map((code) => (
|
|
<label key={code.resultCdUUID} className="flex items-center gap-2 text-sm font-bold cursor-pointer">
|
|
<RadioGroupItem value={String(code.resultCd)} />
|
|
{code.resultCd}.{code.resultCdLbl}
|
|
</label>
|
|
))}
|
|
</RadioGroup>
|
|
|
|
<textarea
|
|
placeholder="메모"
|
|
value={memo}
|
|
onChange={(e) => setMemo(e.target.value)}
|
|
className="flex min-h-[70px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
|
|
/>
|
|
|
|
{showReservation && isReservationSelected ? (
|
|
<KriDateTimePicker value={resveDt} onChange={setResveDt} startHour={9} endHour={20} placeholder="예약 날짜/시간" />
|
|
) : null}
|
|
|
|
<Button type="button" className="w-full" disabled={submitting} onClick={handleAdd}>
|
|
{title} 추가
|
|
</Button>
|
|
</>
|
|
) : null}
|
|
|
|
<CollapsibleHistorySection
|
|
title={`${title}(최근순)`}
|
|
defaultOpen
|
|
showDivider={showInput}
|
|
headerExtra={<RefreshIconButton onRefresh={() => historyQuery.refetch()} isFetching={historyQuery.isFetching} />}
|
|
>
|
|
<EventHistoryList
|
|
isLoading={historyQuery.isLoading}
|
|
history={historyQuery.data}
|
|
resultCodeType={resultCodeType}
|
|
resultCodes={resultCodes}
|
|
canDelete={canDelete}
|
|
isDeleting={deleteEventMutation.isPending}
|
|
onDelete={(listEventHistIdx) => deleteEventMutation.mutate(listEventHistIdx)}
|
|
/>
|
|
</CollapsibleHistorySection>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const SendResultTable = ({ isLoading, results }: { isLoading: boolean; results: SendResultInfo[] }) => {
|
|
const { canCancel: canCancelSend, cancel, isPending: cancelPending } = useSendCancel()
|
|
|
|
if (isLoading) {
|
|
return <div className="rounded-md border p-4 text-center text-sm text-muted-foreground">불러오는 중...</div>
|
|
}
|
|
|
|
if (results.length === 0) {
|
|
return <div className="rounded-md border p-4 text-center text-sm text-muted-foreground">발송 이력이 없습니다.</div>
|
|
}
|
|
|
|
return (
|
|
<div className="max-h-60 overflow-y-auto rounded-md border">
|
|
<table className="w-full text-sm">
|
|
<thead className="sticky top-0 bg-muted">
|
|
<tr>
|
|
<th className="p-2 text-left">유형</th>
|
|
<th className="p-2 text-left">수신처</th>
|
|
<th className="p-2 text-left">결과</th>
|
|
<th className="p-2 text-left">발송일시</th>
|
|
{canCancelSend ? <th className="p-2 text-left"><span className="sr-only">예약 취소</span></th> : null}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{results.map((item) => {
|
|
const { sendIdx } = item
|
|
// 버튼은 서버의 cancelable로만 판단한다(값이 없는 구버전 응답은 취소 불가). 최종 결과는 취소 API 응답 건수로 안내한다.
|
|
const canCancel = isSendCancelable(item) && !!sendIdx
|
|
return (
|
|
<tr key={sendIdx ?? item.cmid} className="border-t">
|
|
<td className="p-2">{MESSAGE_TYPE_LABEL[item.sendTy] ?? String(item.sendTy)}</td>
|
|
<td className="p-2">{item.destEml || item.destTel || '-'}</td>
|
|
<td className="p-2">{item.resultViewLabel || item.resultLabel || item.resultCode}</td>
|
|
<td className="p-2">{formatKst(item.sendDt, 'dateHourMin')}</td>
|
|
{canCancelSend ? (
|
|
<td className="p-2 text-right">
|
|
{canCancel && sendIdx ? (
|
|
<Button
|
|
type="button"
|
|
size="sm"
|
|
variant="outline"
|
|
disabled={cancelPending}
|
|
onClick={() => cancel({ type: 'single', sendIdx })}
|
|
>
|
|
예약 취소
|
|
</Button>
|
|
) : null}
|
|
</td>
|
|
) : null}
|
|
</tr>
|
|
)
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const SendResultHistory = ({ sid, prjctCd }: { sid: string; prjctCd: string }) => {
|
|
const [tab, setTab] = useState<'email' | 'sms' | 'kko'>('email')
|
|
|
|
const emailQuery = useSendResultBySidQuery(sid, prjctCd, clientEnv.REQUEST_SVC, ChannelType.EMAIL, tab === 'email')
|
|
const smsQuery = useSendResultBySidQuery(sid, prjctCd, clientEnv.REQUEST_SVC, ChannelType.SMS, tab === 'sms')
|
|
const kkoQuery = useSendResultBySidQuery(sid, prjctCd, clientEnv.REQUEST_SVC, ChannelType.KKO, tab === 'kko')
|
|
const activeQuery = tab === 'email' ? emailQuery : tab === 'sms' ? smsQuery : kkoQuery
|
|
|
|
const handleTabClick = (e: React.MouseEvent, next: 'email' | 'sms' | 'kko') => {
|
|
e.stopPropagation()
|
|
setTab(next)
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-2 rounded-lg border bg-white p-4">
|
|
<CollapsibleHistorySection
|
|
title="발송 현황(최근순)"
|
|
defaultOpen
|
|
showDivider={false}
|
|
headerExtra={
|
|
<div className="flex items-center gap-1">
|
|
<Button
|
|
type="button"
|
|
size="sm"
|
|
variant={tab === 'email' ? 'default' : 'outline'}
|
|
onClick={(e) => handleTabClick(e, 'email')}
|
|
>
|
|
이메일
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
size="sm"
|
|
variant={tab === 'sms' ? 'default' : 'outline'}
|
|
onClick={(e) => handleTabClick(e, 'sms')}
|
|
>
|
|
문자
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
size="sm"
|
|
variant={tab === 'kko' ? 'default' : 'outline'}
|
|
onClick={(e) => handleTabClick(e, 'kko')}
|
|
>
|
|
알림톡
|
|
</Button>
|
|
<RefreshIconButton onRefresh={() => activeQuery.refetch()} isFetching={activeQuery.isFetching} />
|
|
</div>
|
|
}
|
|
>
|
|
<SendResultTable isLoading={activeQuery.isLoading} results={activeQuery.data ?? []} />
|
|
</CollapsibleHistorySection>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export type ListContactEventPanelProps = {
|
|
prjctIdx: number
|
|
sid: string
|
|
prjctCd: string
|
|
userId: string
|
|
userRole: number
|
|
resultCodes: ResultCode[]
|
|
useFF: boolean
|
|
useContactSenario: boolean
|
|
contactSenario?: string
|
|
listHeaders: listHeader[]
|
|
detail?: ListSearchWithFullResult
|
|
}
|
|
|
|
const ListContactEventPanel = ({
|
|
prjctIdx,
|
|
sid,
|
|
prjctCd,
|
|
userId,
|
|
userRole,
|
|
resultCodes,
|
|
useFF,
|
|
useContactSenario,
|
|
contactSenario,
|
|
listHeaders,
|
|
detail,
|
|
}: ListContactEventPanelProps) => {
|
|
const addListEventMutation = useAddListEventMutation(prjctIdx, sid)
|
|
const reserveMutation = useReserveMutation(prjctIdx, sid)
|
|
const internalInfoQuery = useInternalInfoQuery()
|
|
const showContactInput = userRole !== FIELD_INTERVIEWER_ROLE
|
|
const showInterviewInput = userRole !== CONTACT_INTERVIEWER_ROLE
|
|
const canDeleteEvent = isInternalManager(userRole)
|
|
const contactSenarioValues = Object.fromEntries(
|
|
Object.entries(buildListContactVariableValues(listHeaders, detail, sid)).map(([key, value]) => [
|
|
key,
|
|
escapeHtml(value),
|
|
]),
|
|
)
|
|
const resolvedContactSenario = contactSenario
|
|
? sanitizeHtml(substituteVariables(contactSenario, contactSenarioValues))
|
|
: ''
|
|
|
|
const handleAddEvent =
|
|
(resultCodeType: ResultCodeType) =>
|
|
async (resultCd: number, memo: string, resveDt: string, listCallHistIdx: number | null) => {
|
|
const geoMeta = await getCurrentPositionMeta()
|
|
|
|
await addListEventMutation.mutateAsync({
|
|
prjctIdx,
|
|
sid,
|
|
resultCodeType,
|
|
resultCd,
|
|
listCallHistIdx,
|
|
meta: {
|
|
...geoMeta,
|
|
remoteAddr: internalInfoQuery.data?.clientIp,
|
|
},
|
|
memo,
|
|
})
|
|
|
|
if (resveDt) {
|
|
await reserveMutation.mutateAsync({ prjctIdx, sid, userId, resveDt })
|
|
}
|
|
|
|
alert('등록되었습니다.')
|
|
}
|
|
|
|
return (
|
|
<div className="h-[80vh] space-y-4 overflow-y-auto pr-1">
|
|
<ContactEventSection
|
|
title="컨택결과"
|
|
prjctIdx={prjctIdx}
|
|
sid={sid}
|
|
resultCodeType={ResultCodeType.CONTACT}
|
|
resultCodes={resultCodes}
|
|
showReservation
|
|
showInput={showContactInput}
|
|
submitting={addListEventMutation.isPending || reserveMutation.isPending}
|
|
onSubmit={handleAddEvent(ResultCodeType.CONTACT)}
|
|
canDelete={canDeleteEvent}
|
|
/>
|
|
|
|
<SendResultHistory sid={sid} prjctCd={prjctCd} />
|
|
|
|
{useFF &&
|
|
<ContactEventSection
|
|
title="면접 확인사항"
|
|
prjctIdx={prjctIdx}
|
|
sid={sid}
|
|
resultCodeType={ResultCodeType.INTERVIEW}
|
|
resultCodes={resultCodes}
|
|
showInput={showInterviewInput}
|
|
submitting={addListEventMutation.isPending}
|
|
onSubmit={handleAddEvent(ResultCodeType.INTERVIEW)}
|
|
canDelete={canDeleteEvent}
|
|
/>
|
|
}
|
|
|
|
|
|
{useContactSenario && resolvedContactSenario ? (
|
|
<div className="space-y-2 rounded-lg border bg-white p-4">
|
|
<h2 className="text-lg font-bold border-b pb-2">컨택 시나리오</h2>
|
|
<div className="text-base leading-relaxed" dangerouslySetInnerHTML={{ __html: resolvedContactSenario }} />
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default ListContactEventPanel
|