page.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use client';
  2. import './style.scss';
  3. import { useState, useEffect } from 'react';
  4. import Link from 'next/link';
  5. import { fetchApi, throwError, formatDate } from '@/lib/utils/client';
  6. import type { MyPostsResponse } from '@/types/response/account/myPosts';
  7. import Loading from '@/app/component/Loading';
  8. import Pagination from '@/app/component/Pagination';
  9. import NavTabs from '../navTabs';
  10. export default function MyPosts()
  11. {
  12. const [error, setError] = useState<string>('');
  13. const [loading, setLoading] = useState<boolean>(true);
  14. const [page, setPage] = useState<number>(1);
  15. const [data, setData] = useState<MyPostsResponse>({
  16. total: 0,
  17. list: []
  18. });
  19. useEffect(() => {
  20. if (error) {
  21. alert(error);
  22. setError('');
  23. }
  24. }, [error]);
  25. useEffect(() => {
  26. setLoading(true);
  27. fetchApi<MyPostsResponse>(`/api/mypage/posts?page=${page}&perPage=20`).then((res) => {
  28. throwError(res);
  29. setData(res.data!);
  30. }).catch(err => {
  31. setError(err.message);
  32. }).finally(() => {
  33. setLoading(false);
  34. });
  35. }, [page]);
  36. return (
  37. <>
  38. <NavTabs />
  39. <div id="myPosts">
  40. { loading && <Loading /> }
  41. <h1>작성 게시글</h1>
  42. <table className="table-auto max-2xl:w-full 2xl:w-[1000px]">
  43. <caption>
  44. <div>
  45. 합계: {data.total}
  46. </div>
  47. </caption>
  48. <colgroup>
  49. <col width="8%"/>
  50. <col width="12%"/>
  51. <col />
  52. <col width="10%"/>
  53. <col width="10%"/>
  54. <col width="10%"/>
  55. <col width="15%"/>
  56. </colgroup>
  57. <thead>
  58. <tr>
  59. <th>번호</th>
  60. <th>게시판</th>
  61. <th>제목</th>
  62. <th>조회</th>
  63. <th>추천</th>
  64. <th>댓글</th>
  65. <th>작성일</th>
  66. </tr>
  67. </thead>
  68. <tbody>
  69. {data.list.length > 0 ? (
  70. data.list.map((row) => (
  71. <tr key={row.id} className="hover:bg-gray-100">
  72. <td>{row.num}</td>
  73. <td>{row.boardName}</td>
  74. <td className="text-left">
  75. <Link href={`/post/${row.id}`}>{row.subject}</Link>
  76. </td>
  77. <td>{row.views}</td>
  78. <td>{row.likes}</td>
  79. <td>{row.comments}</td>
  80. <td>{formatDate(row.createdAt)}</td>
  81. </tr>
  82. ))
  83. ) : (
  84. <tr>
  85. <td colSpan={7} className="border p-2 text-center text-gray-500">
  86. 작성한 게시글이 없습니다.
  87. </td>
  88. </tr>
  89. )}
  90. </tbody>
  91. {data.list.length > 0 && (
  92. <tfoot>
  93. <tr>
  94. <td colSpan={7}>
  95. <Pagination total={data.total} page={page} perPage={20} onChange={setPage} />
  96. </td>
  97. </tr>
  98. </tfoot>
  99. )}
  100. </table>
  101. </div>
  102. </>
  103. );
  104. }