Files
contact-front/src/components/dialog/ListDownloadDialog.tsx

197 lines
7.5 KiB
TypeScript

'use client'
import { useState } from 'react'
import { Download } from 'lucide-react'
import { KriDialog } from '@/components/common/KriDialog'
import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
import { Label } from '@/components/ui/label'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { useDialog } from '@/hooks/useDialog'
import type { DialogId } from '@/store/jotai/dialog'
import ApplyFilterOption, { validateApplyFilterTarget } from './ListDialogOption/ApplyFilterOption'
import KriDateRangePicker from '@/components/common/KriDateRangePicker'
import {
type ContactHistoryDownloadType,
ListEventHistoryOrder,
type ListDownloadMode,
type ListDownloadPayload,
type ListFilterTarget,
} from '@/types/list'
export type ListDownloadDialogProps = {
dialogId?: DialogId
open?: boolean
onOpenChange?: (open: boolean) => void
mode: ListDownloadMode
selectedSids: string[]
filterSummary?: string[]
onDownload: (payload: ListDownloadPayload) => void | Promise<void>
}
const titleMap: Record<ListDownloadMode, string> = {
list: '리스트 다운로드',
'contact-history': '컨택 이력 다운로드',
'list-change-history': '리스트 수정 이력 다운로드',
'upload-file': '업로드 파일 다운로드',
}
const ListDownloadDialog = ({
dialogId = 'listDownloadDialog',
open,
onOpenChange,
mode,
selectedSids,
filterSummary,
onDownload,
}: ListDownloadDialogProps) => {
const { isDialogOpen, showDialog, hideDialog } = useDialog()
const [target, setTarget] = useState<ListFilterTarget>('filtered')
const [contactHistoryDateFrom, setContactHistoryDateFrom] = useState('')
const [contactHistoryDateTo, setContactHistoryDateTo] = useState('')
const [contactHistoryType, setContactHistoryType] = useState<ContactHistoryDownloadType>('all')
const [historyOrder, setHistoryOrder] = useState<ListEventHistoryOrder>(ListEventHistoryOrder.DESC)
const [includeResult, setIncludeResult] = useState(true)
const [submitting, setSubmitting] = useState(false)
const resolvedOpen = open ?? isDialogOpen(dialogId)
const handleOpenChange = (nextOpen: boolean) => {
onOpenChange?.(nextOpen)
if (nextOpen) {
showDialog(dialogId)
return
}
hideDialog(dialogId)
}
const handleDownload = async () => {
if (!validateApplyFilterTarget(target, { selectedCount: selectedSids.length, filterSummary })) return
if (mode === 'contact-history') {
const hasFrom = contactHistoryDateFrom.length > 0
const hasTo = contactHistoryDateTo.length > 0
if (hasFrom !== hasTo) {
alert('조회 기간은 시작일과 종료일을 모두 입력해주세요.')
return
}
if (hasFrom && hasTo && contactHistoryDateFrom > contactHistoryDateTo) {
alert('조회 시작일은 종료일보다 이후일 수 없습니다.')
return
}
}
setSubmitting(true)
try {
await onDownload({
mode,
target,
onlyFiltered: target === 'filtered',
includeResult,
contactHistoryDateFrom: mode === 'contact-history' ? contactHistoryDateFrom || undefined : undefined,
contactHistoryDateTo: mode === 'contact-history' ? contactHistoryDateTo || undefined : undefined,
contactHistoryType: mode === 'contact-history' ? contactHistoryType : undefined,
historyOrder: mode === 'contact-history' ? historyOrder : undefined,
})
handleOpenChange(false)
} finally {
setSubmitting(false)
}
}
return (
<KriDialog
open={resolvedOpen}
onOpenChange={handleOpenChange}
title={<span className="text-2xl font-bold">{titleMap[mode]}</span>}
contentClassName="sm:max-w-[560px]"
bodyClassName="space-y-4"
footer={
<>
<Button type="button" variant="outline" onClick={() => handleOpenChange(false)} disabled={submitting}>
취소
</Button>
<Button type="button" onClick={handleDownload} disabled={submitting} className="gap-2">
<Download className="w-4 h-4" />
다운로드
</Button>
</>
}
>
{mode === 'contact-history' ? (
<div className="rounded-md border p-4 space-y-4">
<p className="text-lg font-semibold">조회 기간</p>
<KriDateRangePicker
value={{ from: contactHistoryDateFrom, to: contactHistoryDateTo }}
onChange={(range) => {
setContactHistoryDateFrom(range.from)
setContactHistoryDateTo(range.to)
}}
/>
<p className="text-xs text-muted-foreground">비워두면 전체 기간으로 다운로드합니다.</p>
</div>
) : null}
<div className="rounded-md border p-4 space-y-4">
<p className="text-lg font-semibold">다운로드 범위</p>
<ApplyFilterOption
value={target}
onValueChange={setTarget}
idPrefix="download-scope-target"
selectedSids={selectedSids}
filterSummary={filterSummary}
/>
</div>
{mode === 'list' || mode === 'contact-history' ? (
<div className="flex items-center gap-2 rounded-md border p-4">
<Checkbox
id="download-include-result"
checked={includeResult}
onCheckedChange={(checked) => setIncludeResult(checked === true)}
/>
<Label className="text-base" htmlFor="download-include-result">결과값 포함</Label>
</div>
) : null}
{mode === 'contact-history' ? (
<div className="rounded-md border p-4 space-y-4">
<p className="text-lg font-semibold">이력 기준</p>
<RadioGroup
value={contactHistoryType}
onValueChange={(v) => setContactHistoryType(v as ContactHistoryDownloadType)}
className="flex items-center gap-6 flex-wrap"
>
<div className="flex items-center gap-2">
<RadioGroupItem value="all" id="download-contact-history-all" />
<Label className="text-base" htmlFor="download-contact-history-all">전체 이력</Label>
</div>
<div className="flex items-center gap-2">
<RadioGroupItem value="latest" id="download-contact-history-latest" />
<Label className="text-base" htmlFor="download-contact-history-latest">마지막 이력만</Label>
</div>
</RadioGroup>
</div>
) : null}
{mode === 'contact-history' && contactHistoryType === 'all' ? (
<div className="rounded-md border p-4 space-y-4">
<p className="text-lg font-semibold">이력 순서</p>
<RadioGroup
value={historyOrder}
onValueChange={(value) => setHistoryOrder(value as ListEventHistoryOrder)}
className="flex items-center gap-6 flex-wrap"
>
<div className="flex items-center gap-2">
<RadioGroupItem value={ListEventHistoryOrder.DESC} id="download-history-order-desc" />
<Label className="text-base" htmlFor="download-history-order-desc">최근 이력이 왼쪽부터</Label>
</div>
<div className="flex items-center gap-2">
<RadioGroupItem value={ListEventHistoryOrder.ASC} id="download-history-order-asc" />
<Label className="text-base" htmlFor="download-history-order-asc">오래된 이력이 왼쪽부터</Label>
</div>
</RadioGroup>
</div>
) : null}
</KriDialog>
)
}
export default ListDownloadDialog