page.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 { MyCommentsResponse } from '@/types/response/account/myComments';
  7. import Loading from '@/app/component/Loading';
  8. import Pagination from '@/app/component/Pagination';
  9. import NavTabs from '../navTabs';
  10. export default function MyComments()
  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<MyCommentsResponse>({
  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<MyCommentsResponse>(`/api/mypage/comments?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="myComments">
  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 width="20%"/>
  52. <col />
  53. <col width="10%"/>
  54. <col width="15%"/>
  55. </colgroup>
  56. <thead>
  57. <tr>
  58. <th>번호</th>
  59. <th>게시판</th>
  60. <th>게시글</th>
  61. <th>댓글 내용</th>
  62. <th>추천</th>
  63. <th>작성일</th>
  64. </tr>
  65. </thead>
  66. <tbody>
  67. {data.list.length > 0 ? (
  68. data.list.map((row) => (
  69. <tr key={row.id} className="hover:bg-gray-100">
  70. <td>{row.num}</td>
  71. <td>{row.boardName}</td>
  72. <td className="text-left">
  73. <Link href={`/post/${row.postID}`}>{row.postSubject}</Link>
  74. </td>
  75. <td className="text-left">{row.content}</td>
  76. <td>{row.likes}</td>
  77. <td>{formatDate(row.createdAt)}</td>
  78. </tr>
  79. ))
  80. ) : (
  81. <tr>
  82. <td colSpan={6} className="border p-2 text-center text-gray-500">
  83. 작성한 댓글이 없습니다.
  84. </td>
  85. </tr>
  86. )}
  87. </tbody>
  88. {data.list.length > 0 && (
  89. <tfoot>
  90. <tr>
  91. <td colSpan={6}>
  92. <Pagination total={data.total} page={page} perPage={20} onChange={setPage} />
  93. </td>
  94. </tr>
  95. </tfoot>
  96. )}
  97. </table>
  98. </div>
  99. </>
  100. );
  101. }