|
1 | | -import { useRef } from 'react'; |
| 1 | +import { useRef, useState } from 'react'; |
2 | 2 | import Button from '@/components/ui/Button/Button'; |
3 | 3 | import { Card, CardTitle } from '@/components/ui/Card/Card'; |
4 | 4 | import { ModalHandle } from '@/components/ui/Modal/Modal'; |
5 | 5 | import InviteDashboard from './InviteDashboard'; |
| 6 | +import { useParams } from 'next/navigation'; |
| 7 | +import { useDashboardInvitationsQuery, useDashboardMutation } from '@/apis/dashboards/queries'; |
| 8 | +import { getErrorMessage } from '@/utils/errorMessage'; |
| 9 | +import useAlert from '@/hooks/useAlert'; |
| 10 | +import { Table, TableBody, TableCell, TableCol, TableColGroup, TableHead, TableHeadCell, TableRow } from '@/components/ui/Table/Table'; |
| 11 | +import PaginationWithCounter from '@/components/pagination/PaginationWithCounter'; |
| 12 | +import { isAxiosError } from 'axios'; |
| 13 | + |
| 14 | +const PAGE_SIZE = 5; |
6 | 15 |
|
7 | 16 | export default function DetailInvited() { |
| 17 | + const { id } = useParams<{ id: string }>(); |
| 18 | + const [page, setPage] = useState(1); |
| 19 | + const { data, error, isLoading } = useDashboardInvitationsQuery(Number(id), page, PAGE_SIZE); |
| 20 | + const { cancel } = useDashboardMutation(); |
| 21 | + const alert = useAlert(); |
8 | 22 | const inviteModalRef = useRef<ModalHandle | null>(null); |
9 | 23 |
|
| 24 | + const cancelInvite = async (invitationId: number) => { |
| 25 | + try { |
| 26 | + await cancel({ dashboardId: Number(id), invitationId }); |
| 27 | + alert('์ด๋๋ฅผ ์ทจ์ํ์ต๋๋ค.'); |
| 28 | + } catch (error) { |
| 29 | + const message = getErrorMessage(error); |
| 30 | + alert(message); |
| 31 | + } |
| 32 | + }; |
| 33 | + const notAllowed = isAxiosError(error) && error.status === 403; |
| 34 | + |
10 | 35 | return ( |
11 | 36 | <Card> |
12 | 37 | <CardTitle> |
13 | 38 | ์ด๋๋ด์ญ |
14 | 39 | <div className='leading-none'> |
15 | | - <Button size='sm' onClick={() => inviteModalRef.current?.open()}> |
16 | | - ์ด๋ํ๊ธฐ |
17 | | - </Button> |
| 40 | + <PaginationWithCounter // |
| 41 | + totalCount={data?.totalCount || 0} |
| 42 | + page={page} |
| 43 | + setPage={setPage} |
| 44 | + pageSize={PAGE_SIZE} |
| 45 | + /> |
18 | 46 | </div> |
19 | 47 | </CardTitle> |
20 | | - <div>๋์๋ณด๋ ์ด๋๋ด์ญ(์์
ํ์)</div> |
| 48 | + <Table className='mb-4'> |
| 49 | + <TableColGroup> |
| 50 | + <TableCol /> |
| 51 | + <TableCol className='w-24' /> |
| 52 | + </TableColGroup> |
| 53 | + <TableHead> |
| 54 | + <TableRow> |
| 55 | + <TableHeadCell>์ด๋ฉ์ผ</TableHeadCell> |
| 56 | + <TableHeadCell></TableHeadCell> |
| 57 | + </TableRow> |
| 58 | + </TableHead> |
| 59 | + <TableBody> |
| 60 | + {isLoading && ( |
| 61 | + <TableRow> |
| 62 | + <TableCell colSpan={2}> |
| 63 | + <div className='p-4 text-center'>์ด๋๋ชฉ๋ก์ ๊ฐ์ ธ์ค๋ ์ค์
๋๋ค.</div> |
| 64 | + </TableCell> |
| 65 | + </TableRow> |
| 66 | + )} |
| 67 | + {notAllowed && ( |
| 68 | + <TableRow> |
| 69 | + <TableCell colSpan={2}> |
| 70 | + <div className='p-4 text-center'>๊ถํ์ด ์์ต๋๋ค.</div> |
| 71 | + </TableCell> |
| 72 | + </TableRow> |
| 73 | + )} |
| 74 | + {data?.invitations.length === 0 && ( |
| 75 | + <TableRow> |
| 76 | + <TableCell colSpan={2}> |
| 77 | + <div className='p-4 text-center'>์ด๋ ๋ด์ญ์ด ์์ต๋๋ค.</div> |
| 78 | + </TableCell> |
| 79 | + </TableRow> |
| 80 | + )} |
| 81 | + {data?.invitations.map((item) => ( |
| 82 | + <TableRow key={item.id}> |
| 83 | + <TableCell>{item.invitee.email}</TableCell> |
| 84 | + <TableCell> |
| 85 | + <Button variant='outline' size='sm' onClick={() => cancelInvite(item.id)}> |
| 86 | + ์ทจ์ |
| 87 | + </Button> |
| 88 | + </TableCell> |
| 89 | + </TableRow> |
| 90 | + ))} |
| 91 | + </TableBody> |
| 92 | + </Table> |
| 93 | + <Button className='w-full' onClick={() => inviteModalRef.current?.open()}> |
| 94 | + ์ด๋ํ๊ธฐ |
| 95 | + </Button> |
21 | 96 |
|
22 | 97 | {/* ์ด๋๋ชจ๋ฌ */} |
23 | 98 | <InviteDashboard ref={inviteModalRef} /> |
|
0 commit comments