|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useCallback, useEffect, useRef } from 'react'; |
| 4 | +import { Table, TableHeader, TableColumn, TableBody, TableRow, TableCell, Chip } from '@nextui-org/react'; |
| 5 | + |
| 6 | +import UserDetailsModal from '@/components/admin/UserDetailsModal'; |
| 7 | +import AdminTableTopContent from '@/components/admin/AdminTableTopContent'; |
| 8 | +import AdminTableBottomContent from '@/components/admin/AdminTableBottomContent'; |
| 9 | +import { useAuthenticatedApi } from '@/hooks/useAuthenticatedApi'; |
| 10 | + |
| 11 | +const columns = [ |
| 12 | + { name: 'NAME', uid: 'name' }, |
| 13 | + { name: 'MAJOR / ID', uid: 'major' }, |
| 14 | + { name: 'TEAM', uid: 'team' }, |
| 15 | + { name: 'EMAIL', uid: 'email' }, |
| 16 | + { name: 'PHONE', uid: 'phone' }, |
| 17 | +]; |
| 18 | + |
| 19 | +export default function CoreAdminPage() { |
| 20 | + const { apiClient } = useAuthenticatedApi(); |
| 21 | + |
| 22 | + const [page, setPage] = React.useState(1); |
| 23 | + const [modalOpen, setModalOpen] = React.useState(false); |
| 24 | + const modalClosing = useRef(false); |
| 25 | + |
| 26 | + const [selectedUser, setSelectedUser] = React.useState(null); |
| 27 | + const [searchValue, setSearchValue] = React.useState(''); |
| 28 | + const [query, setQuery] = React.useState(''); |
| 29 | + const [loading, setLoading] = React.useState(false); |
| 30 | + const [error, setError] = React.useState(''); |
| 31 | + const [currentUsers, setCurrentUsers] = React.useState([]); |
| 32 | + const [totalUsers, setTotalUsers] = React.useState(0); |
| 33 | + const [totalPages, setTotalPages] = React.useState(0); |
| 34 | + |
| 35 | + const rowsPerPage = 10; |
| 36 | + |
| 37 | + const fetchApplicants = useCallback(async () => { |
| 38 | + setLoading(true); |
| 39 | + setError(''); |
| 40 | + try { |
| 41 | + const params = { |
| 42 | + page: page - 1, |
| 43 | + size: rowsPerPage, |
| 44 | + sort: 'createdAt', |
| 45 | + dir: 'DESC', |
| 46 | + question: query || undefined, |
| 47 | + }; |
| 48 | + const res = await apiClient.get('/core-recruit/applicants', { params }); |
| 49 | + const list = Array.isArray(res?.data?.data) ? res.data.data : []; |
| 50 | + const total = res?.data?.meta?.totalElements ?? list.length; |
| 51 | + const computedTotalPages = Math.max(1, Math.ceil(total / rowsPerPage)); |
| 52 | + |
| 53 | + setCurrentUsers(list); |
| 54 | + setTotalUsers(total); |
| 55 | + setTotalPages(computedTotalPages); |
| 56 | + } catch (err) { |
| 57 | + setError(String(err?.message || 'failed to load applicants')); |
| 58 | + setCurrentUsers([]); |
| 59 | + setTotalUsers(0); |
| 60 | + } finally { |
| 61 | + setLoading(false); |
| 62 | + } |
| 63 | + }, [apiClient, page, rowsPerPage, query]); |
| 64 | + |
| 65 | + const renderCell = useCallback((user, columnKey) => { |
| 66 | + const value = user[columnKey]; |
| 67 | + switch (columnKey) { |
| 68 | + case 'name': |
| 69 | + return <span className='text-white'>{user?.name ?? ''}</span>; |
| 70 | + case 'major': |
| 71 | + return ( |
| 72 | + <div className='flex flex-col'> |
| 73 | + <p className='text-white text-bold text-sm capitalize'>{user?.major ?? ''}</p> |
| 74 | + <p className='text-bold text-sm capitalize text-default-400'>{user?.studentId ?? ''}</p> |
| 75 | + </div> |
| 76 | + ); |
| 77 | + case 'team': { |
| 78 | + const teamLabel = user?.team ?? ''; |
| 79 | + const teamColorMap = { |
| 80 | + HR: '#EA4335', |
| 81 | + BD: '#34A853', |
| 82 | + TECH: '#4285F4', |
| 83 | + 'PR/DESIGN': '#F9AB00', |
| 84 | + }; |
| 85 | + const color = teamColorMap[teamLabel] || '#9CA3AF'; |
| 86 | + return ( |
| 87 | + <Chip |
| 88 | + size='sm' |
| 89 | + variant='bordered' |
| 90 | + style={{ |
| 91 | + borderColor: color, |
| 92 | + color, |
| 93 | + }} |
| 94 | + > |
| 95 | + {teamLabel} |
| 96 | + </Chip> |
| 97 | + ); |
| 98 | + } |
| 99 | + case 'email': |
| 100 | + return <span className='text-white'>{user?.email ?? ''}</span>; |
| 101 | + case 'phone': |
| 102 | + return <span className='text-white'>{user?.phone ?? ''}</span>; |
| 103 | + case 'createdAt': |
| 104 | + return <span className='text-white'>{user?.createdAt ? new Date(user.createdAt).toLocaleString() : ''}</span>; |
| 105 | + default: |
| 106 | + return value; |
| 107 | + } |
| 108 | + }, []); |
| 109 | + |
| 110 | + const handleRowClick = async (user) => { |
| 111 | + if (modalClosing.current) return; |
| 112 | + try { |
| 113 | + const id = user?.id; |
| 114 | + if (!id) throw new Error('지원자 ID 없음'); |
| 115 | + const res = await apiClient.get(`/core-recruit/applicants/${id}`); |
| 116 | + const detail = res?.data?.data ?? null; |
| 117 | + if (!detail) throw new Error('상세 정보 없음'); |
| 118 | + setSelectedUser(detail); |
| 119 | + setModalOpen(true); |
| 120 | + } catch (e) { |
| 121 | + alert('상세 정보를 불러오는 중 오류가 발생했습니다.'); |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + const handleSearch = () => { |
| 126 | + setPage(1); |
| 127 | + setQuery((searchValue || '').trim()); |
| 128 | + }; |
| 129 | + |
| 130 | + const handleCloseModal = () => { |
| 131 | + modalClosing.current = true; |
| 132 | + setModalOpen(false); |
| 133 | + setTimeout(() => { |
| 134 | + modalClosing.current = false; |
| 135 | + }, 300); |
| 136 | + }; |
| 137 | + |
| 138 | + useEffect(() => { |
| 139 | + if (searchValue === '' && query !== '') { |
| 140 | + setPage(1); |
| 141 | + setQuery(''); |
| 142 | + } |
| 143 | + }, [searchValue, query]); |
| 144 | + |
| 145 | + useEffect(() => { |
| 146 | + fetchApplicants(); |
| 147 | + }, [fetchApplicants]); |
| 148 | + |
| 149 | + return ( |
| 150 | + <div> |
| 151 | + <Table |
| 152 | + className='dark text-white py-[30px] px-[96px] mobile:px-[10px]' |
| 153 | + aria-label='Core applicants table' |
| 154 | + bottomContent={ |
| 155 | + <div className='relative'> |
| 156 | + <AdminTableBottomContent |
| 157 | + page={page} |
| 158 | + totalPages={totalPages} |
| 159 | + totalUsers={totalUsers} |
| 160 | + onChangePage={(newPage) => setPage(newPage)} |
| 161 | + /> |
| 162 | + </div> |
| 163 | + } |
| 164 | + topContent={ |
| 165 | + <AdminTableTopContent searchValue={searchValue} setSearchValue={setSearchValue} onSearch={handleSearch} /> |
| 166 | + } |
| 167 | + > |
| 168 | + <TableHeader columns={columns}> |
| 169 | + {(column) => ( |
| 170 | + <TableColumn key={column.uid} align='start'> |
| 171 | + {column.name} |
| 172 | + </TableColumn> |
| 173 | + )} |
| 174 | + </TableHeader> |
| 175 | + <TableBody items={currentUsers} isLoading={loading} emptyContent={loading ? '불러오는 중...' : '데이터가 없습니다.'}> |
| 176 | + {(item) => ( |
| 177 | + <TableRow className='hover:bg-[#35353b99] cursor-pointer text-white' key={item.id} onClick={() => handleRowClick(item)}> |
| 178 | + {(columnKey) => ( |
| 179 | + <TableCell> |
| 180 | + {renderCell(item, columnKey)} |
| 181 | + </TableCell> |
| 182 | + )} |
| 183 | + </TableRow> |
| 184 | + )} |
| 185 | + </TableBody> |
| 186 | + </Table> |
| 187 | + |
| 188 | + {/* 기존 UserDetailsModal 재사용: 핵심 질문/자유문항은 response 배열로 표시됨 */} |
| 189 | + <UserDetailsModal user={selectedUser} isOpen={modalOpen} onClose={handleCloseModal} preventClose /> |
| 190 | + </div> |
| 191 | + ); |
| 192 | +} |
| 193 | + |
| 194 | + |
0 commit comments