diff --git a/src/components/common/table/Table.tsx b/src/components/common/table/Table.tsx index d6537526..5f53ceba 100644 --- a/src/components/common/table/Table.tsx +++ b/src/components/common/table/Table.tsx @@ -2,10 +2,12 @@ import { useEffect, useState } from 'react'; import TableStatus from './TableStatus'; import TableButtons from './TableButtons'; import Pagination from '../Pagination'; +import Modal from '../Modal'; import formatWorkTime from '@/utils/formatWorkTime'; import { getNoticeApplications, getUserApplications, + putNoticeApplications, } from '@/api/applicationApi'; interface UserProps { @@ -32,6 +34,53 @@ export default function Table(props: UserProps | NoticeProps) { ); const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); + const [modal, setModal] = useState<{ + isOpen: boolean; + status: 'accepted' | 'rejected'; + dataIndex: number; + }>({ + isOpen: false, + status: 'accepted', + dataIndex: 0, + }); + + const closeModal = () => + setModal((prev) => { + return { ...prev, isOpen: false }; + }); + + const clickButton = (dataIndex: number, status: 'accepted' | 'rejected') => { + setModal({ + isOpen: true, + status, + dataIndex, + }); + }; + + const handleModalClick = async () => { + closeModal(); + if (mode === 'user') return; + + setDatas((prev) => { + prev[modal.dataIndex][3] = modal.status; + return prev; + }); + try { + await putNoticeApplications( + props.shopId, + props.noticeId, + datas[modal.dataIndex][4] ?? '', + { + status: modal.status, + }, + ); + } catch { + setDatas((prev) => { + prev[modal.dataIndex][3] = 'pending'; + return prev; + }); + } + }; useEffect(() => { (async () => { @@ -97,68 +146,77 @@ export default function Table(props: UserProps | NoticeProps) { }, [mode, page]); return ( -
- - - - - - - - - - - {datas.map((data, index) => ( - - - - - + <> +
+
- {headers[0]} - - {headers[1]} - - {headers[2]} - - {headers[3]} -
-
- {data[0]} -
-
-
- {data[1]} -
-
-
- {data[2]} -
-
-
- {mode === 'notice' && data[3] === 'pending' ? ( - - ) : ( - - )} -
-
+ + + + + + - ))} - -
+ {headers[0]} + + {headers[1]} + + {headers[2]} + + {headers[3]} +
-
- + + + {datas.map((data, index) => ( + + +
+ {data[0]} +
+ + +
+ {data[1]} +
+ + +
+ {data[2]} +
+ + +
+ {mode === 'notice' && data[3] === 'pending' ? ( + + ) : ( + + )} +
+ + + ))} + + +
+ +
-
+ {modal.isOpen && ( + + 신청을 {modal.status === 'accepted' ? '승인' : '거절'}하시겠어요? + + )} + ); } diff --git a/src/components/common/table/TableButtons.tsx b/src/components/common/table/TableButtons.tsx index 8a6f241c..bc0e1830 100644 --- a/src/components/common/table/TableButtons.tsx +++ b/src/components/common/table/TableButtons.tsx @@ -1,56 +1,14 @@ -import { useEffect, useState, type MouseEvent } from 'react'; +import { useEffect, useState } from 'react'; import Button from '../Button'; -import TableStatus from './TableStatus'; -import { putNoticeApplications } from '@/api/applicationApi'; -import Modal from '../Modal'; interface Props { - shopId: string; - noticeId: string; - applicaitonId: string; + index: number; + click: (dataIndex: number, status: 'accepted' | 'rejected') => void; } -export default function TableButtons({ - shopId, - noticeId, - applicaitonId, -}: Props) { - const [status, setStatus] = useState<'pending' | 'accepted' | 'rejected'>( - 'pending', - ); - const [modal, setModal] = useState<{ - isOpen: boolean; - status: 'accepted' | 'rejected'; - }>({ - isOpen: false, - status: 'accepted', - }); +export default function TableButtons({ index, click }: Props) { const [isMobile, setIsMobile] = useState(window.innerWidth < 768); - const closeModal = () => - setModal((prev) => { - return { ...prev, isOpen: false }; - }); - - const handleModalClick = async () => { - closeModal(); - setStatus(modal.status); - try { - await putNoticeApplications(shopId, noticeId, applicaitonId, { - status: modal.status, - }); - } catch { - setStatus('pending'); - } - }; - - const handleClick = (e: MouseEvent) => { - setModal({ - isOpen: true, - status: e.currentTarget.value as 'accepted' | 'rejected', - }); - }; - useEffect(() => { const handleResize = () => { setIsMobile(window.innerWidth < 768); @@ -59,45 +17,30 @@ export default function TableButtons({ return () => window.removeEventListener('resize', handleResize); }, []); - return status === 'pending' ? ( - <> -
- - -
- {modal.isOpen && ( - - 신청을 {modal.status === 'accepted' ? '승인' : '거절'}하시겠어요? - - )} - - ) : ( - + return ( +
+ + +
); }