diff --git a/src/components/common/table/Table.tsx b/src/components/common/table/Table.tsx new file mode 100644 index 00000000..d6537526 --- /dev/null +++ b/src/components/common/table/Table.tsx @@ -0,0 +1,164 @@ +import { useEffect, useState } from 'react'; +import TableStatus from './TableStatus'; +import TableButtons from './TableButtons'; +import Pagination from '../Pagination'; +import formatWorkTime from '@/utils/formatWorkTime'; +import { + getNoticeApplications, + getUserApplications, +} from '@/api/applicationApi'; + +interface UserProps { + className?: string; + mode: 'user'; + userId: string; +} + +interface NoticeProps { + className?: string; + mode: 'notice'; + shopId: string; + noticeId: string; +} + +export default function Table(props: UserProps | NoticeProps) { + const { className, mode } = props; + const headers = + mode === 'notice' + ? ['신청자', '소개', '전화번호', '상태'] + : ['가게', '일자', '시급', '상태']; + const [datas, setDatas] = useState<(string | undefined)[][]>( + Array(5).fill(['', '', '', '']), + ); + const [page, setPage] = useState(1); + const [totalPage, setTotalPage] = useState(1); + + useEffect(() => { + (async () => { + try { + if (mode === 'notice') { + const { items, count } = await getNoticeApplications( + props.shopId, + props.noticeId, + { + offset: (page - 1) * 5, + limit: 5, + }, + ); + + setTotalPage(count ? Math.ceil(count / 5) : 1); + setDatas( + [...items, ...[null, null, null, null, null]] + .slice(0, 5) + .map((element) => { + if (!element) return ['', '', '', '']; + + return [ + element.item.user.item.name, + element.item.user.item.bio, + element.item.user.item.phone, + element.item.status, + element.item.id, + ]; + }), + ); + } else { + const { items, count } = await getUserApplications(props.userId, { + offset: (page - 1) * 5, + limit: 5, + }); + + setTotalPage(count ? Math.ceil(count / 5) : 1); + setDatas( + [...items, ...[null, null, null, null, null]] + .slice(0, 5) + .map((element) => { + if (!element) return ['', '', '', '']; + + return [ + element.item.shop.item.name, + `${formatWorkTime({ + startsAt: element.item.notice.item.startsAt, + workhour: element.item.notice.item.workhour, + })} (${element.item.notice.item.workhour}시간)`, + element.item.notice.item.hourlyPay.toLocaleString('ko-KR') + + '원', + element.item.status, + ]; + }), + ); + } + } catch { + setDatas(Array(5).fill(['', '', '', ''])); + setPage(1); + setTotalPage(1); + } + })(); + }, [mode, page]); + + return ( +
| + {headers[0]} + | ++ {headers[1]} + | ++ {headers[2]} + | ++ {headers[3]} + | +
|---|---|---|---|
|
+
+ {data[0]}
+
+ |
+
+
+ {data[1]}
+
+ |
+
+
+ {data[2]}
+
+ |
+
+
+ {mode === 'notice' && data[3] === 'pending' ? (
+
+ |
+