|
| 1 | +import { useState } from "react"; |
| 2 | +import { DataTable } from "@/components/data-table"; |
| 3 | +import { Badge } from "@/components/ui/badge"; |
| 4 | +import { Alert, AlertDescription } from "@/components/ui/alert"; |
| 5 | +import { Pagination } from "@/components/common/Pagination"; |
| 6 | +import { useTableState } from "@/hooks/use-table-state"; |
| 7 | +import { useApplications } from "@/hooks/use-applications"; |
| 8 | +import { applicationStatusLabel, applicationStatusVariant, formatDate } from "@/lib/format"; |
| 9 | +import { ApplicationSheet } from "./ApplicationSheet"; |
| 10 | +import type { ApplicationListItem } from "@/types/application"; |
| 11 | +import type { ColumnDef } from "@/types/data-table"; |
| 12 | + |
| 13 | +const PAGE_SIZE = 20; |
| 14 | +const FILTER_KEYS = ["applicantName", "applicantEmail", "track", "status"]; |
| 15 | + |
| 16 | +export function ApplicationsPage() { |
| 17 | + const { |
| 18 | + searchParams, page, sorts, getFilters, |
| 19 | + setSort, setFilter, setPage, setParam, deleteParam, |
| 20 | + } = useTableState(); |
| 21 | + |
| 22 | + const [selectedApplication, setSelectedApplication] = useState<ApplicationListItem | null>(null); |
| 23 | + const selectedId = searchParams.get("application"); |
| 24 | + const filters = getFilters(FILTER_KEYS); |
| 25 | + |
| 26 | + const filter: Record<string, unknown> = { |
| 27 | + page, |
| 28 | + size: PAGE_SIZE, |
| 29 | + ...(sorts.length > 0 && { sorts: sorts.map((s) => ({ field: s.field, order: s.direction })) }), |
| 30 | + ...(filters.applicantName && { applicantName: filters.applicantName }), |
| 31 | + ...(filters.applicantEmail && { applicantEmail: filters.applicantEmail }), |
| 32 | + ...(filters.track && { track: filters.track }), |
| 33 | + ...(filters.status && { status: filters.status }), |
| 34 | + }; |
| 35 | + |
| 36 | + const { data, isLoading, isError } = useApplications(filter); |
| 37 | + const totalPages = data ? Math.ceil(data.total / PAGE_SIZE) : 0; |
| 38 | + |
| 39 | + const columns: ColumnDef<ApplicationListItem>[] = [ |
| 40 | + { |
| 41 | + id: "applicantName", |
| 42 | + header: "이름", |
| 43 | + cell: (a) => <span className="font-medium">{a.applicantName}</span>, |
| 44 | + sortable: true, |
| 45 | + filterType: "text", |
| 46 | + filterParamKey: "applicantName", |
| 47 | + className: "w-[15%]", |
| 48 | + }, |
| 49 | + { |
| 50 | + id: "applicantEmail", |
| 51 | + header: "이메일", |
| 52 | + cell: (a) => <span className="break-all">{a.applicantEmail}</span>, |
| 53 | + sortable: true, |
| 54 | + filterType: "text", |
| 55 | + filterParamKey: "applicantEmail", |
| 56 | + className: "w-[25%]", |
| 57 | + }, |
| 58 | + { |
| 59 | + id: "track", |
| 60 | + header: "희망 트랙", |
| 61 | + cell: (a) => a.track, |
| 62 | + sortable: true, |
| 63 | + filterType: "text", |
| 64 | + filterParamKey: "track", |
| 65 | + className: "w-[15%]", |
| 66 | + }, |
| 67 | + { |
| 68 | + id: "submittedAt", |
| 69 | + header: "지원일", |
| 70 | + cell: (a) => formatDate(a.submittedAt), |
| 71 | + sortable: true, |
| 72 | + className: "w-[15%]", |
| 73 | + }, |
| 74 | + { |
| 75 | + id: "status", |
| 76 | + header: "상태", |
| 77 | + cell: (a) => ( |
| 78 | + <Badge variant={applicationStatusVariant(a.status)}> |
| 79 | + {applicationStatusLabel(a.status)} |
| 80 | + </Badge> |
| 81 | + ), |
| 82 | + sortable: true, |
| 83 | + filterType: "text", |
| 84 | + filterParamKey: "status", |
| 85 | + className: "w-[15%]", |
| 86 | + }, |
| 87 | + ]; |
| 88 | + |
| 89 | + return ( |
| 90 | + <div className="space-y-4"> |
| 91 | + <h1 className="text-2xl font-semibold">지원자 관리</h1> |
| 92 | + |
| 93 | + {isError && ( |
| 94 | + <Alert variant="destructive"> |
| 95 | + <AlertDescription>지원자 목록을 불러오지 못했습니다.</AlertDescription> |
| 96 | + </Alert> |
| 97 | + )} |
| 98 | + |
| 99 | + <DataTable |
| 100 | + columns={columns} |
| 101 | + data={data?.items} |
| 102 | + isLoading={isLoading} |
| 103 | + sorts={sorts} |
| 104 | + filters={filters} |
| 105 | + onSort={setSort} |
| 106 | + onFilterChange={setFilter} |
| 107 | + onRowClick={(a) => { |
| 108 | + setSelectedApplication(a); |
| 109 | + setParam("application", a.id); |
| 110 | + }} |
| 111 | + rowKey={(a) => a.id} |
| 112 | + emptyMessage="지원자가 없습니다." |
| 113 | + /> |
| 114 | + |
| 115 | + <ApplicationSheet |
| 116 | + application={selectedApplication} |
| 117 | + open={!!selectedId} |
| 118 | + onOpenChange={(open) => { |
| 119 | + if (!open) { |
| 120 | + deleteParam("application"); |
| 121 | + setSelectedApplication(null); |
| 122 | + } |
| 123 | + }} |
| 124 | + /> |
| 125 | + |
| 126 | + <Pagination page={page} totalPages={totalPages} onPageChange={setPage} /> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
0 commit comments