92 lines
2.9 KiB
TypeScript
92 lines
2.9 KiB
TypeScript
'use client'
|
|
|
|
import { KriDialog } from '@/components/common/KriDialog'
|
|
import { useDialog } from '@/hooks/useDialog'
|
|
import type { DialogId } from '@/store/jotai/dialog'
|
|
import type { ListReserve } from '@/types/list'
|
|
import { formatKst } from '@kri/common'
|
|
|
|
export type ReservationListDialogProps = {
|
|
dialogId?: DialogId
|
|
open?: boolean
|
|
onOpenChange?: (open: boolean) => void
|
|
reservations: ListReserve[]
|
|
onSelectReservation: (sid: string) => void
|
|
}
|
|
|
|
// 오늘과 가장 가까운(과거/미래 무관) 예약의 sid를 찾는다.
|
|
export const findClosestReservationSid = (reservations: ListReserve[], now: Date = new Date()): string | null => {
|
|
if (reservations.length === 0) return null
|
|
|
|
return reservations.reduce((closest, current) => {
|
|
const closestDiff = Math.abs(new Date(closest.resveDt).getTime() - now.getTime())
|
|
const currentDiff = Math.abs(new Date(current.resveDt).getTime() - now.getTime())
|
|
return currentDiff < closestDiff ? current : closest
|
|
}, reservations[0]).sid
|
|
}
|
|
|
|
const ReservationListDialog = ({
|
|
dialogId = 'reservationListDialog',
|
|
open,
|
|
onOpenChange,
|
|
reservations,
|
|
onSelectReservation,
|
|
}: ReservationListDialogProps) => {
|
|
const { isDialogOpen, showDialog, hideDialog } = useDialog()
|
|
const resolvedOpen = open ?? isDialogOpen(dialogId)
|
|
const closestSid = findClosestReservationSid(reservations)
|
|
|
|
const handleOpenChange = (nextOpen: boolean) => {
|
|
onOpenChange?.(nextOpen)
|
|
if (nextOpen) {
|
|
showDialog(dialogId)
|
|
return
|
|
}
|
|
hideDialog(dialogId)
|
|
}
|
|
|
|
const handleSelect = (sid: string) => {
|
|
onSelectReservation(sid)
|
|
handleOpenChange(false)
|
|
}
|
|
|
|
return (
|
|
<KriDialog
|
|
open={resolvedOpen}
|
|
onOpenChange={handleOpenChange}
|
|
title={<span className="text-xl">예약리스트 조회</span>}
|
|
contentClassName="sm:max-w-[480px]"
|
|
showDefaultFooter
|
|
>
|
|
<div className="max-h-80 overflow-auto rounded-md border">
|
|
{reservations.length === 0 ? (
|
|
<div className="p-6 text-center text-sm text-muted-foreground">예약된 리스트가 없습니다.</div>
|
|
) : (
|
|
<table className="w-full text-sm">
|
|
<thead className="sticky top-0 bg-muted">
|
|
<tr>
|
|
<th className="p-2 text-left">SID</th>
|
|
<th className="p-2 text-left">예약 일시</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{reservations.map((item) => (
|
|
<tr
|
|
key={item.listResveIdx}
|
|
className={`cursor-pointer border-t hover:bg-muted/50 ${item.sid === closestSid ? 'bg-accent' : ''}`}
|
|
onClick={() => handleSelect(item.sid)}
|
|
>
|
|
<td className="p-2">{item.sid}</td>
|
|
<td className="p-2">{formatKst(item.resveDt, 'dateHourMin')}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
</div>
|
|
</KriDialog>
|
|
)
|
|
}
|
|
|
|
export default ReservationListDialog
|