page.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use client';
  2. import './style.scss';
  3. import { useState, useEffect } from 'react';
  4. import Link from 'next/link';
  5. import { fetchApi, getDateTime } 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. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
  11. import { faThumbsUp } from '@fortawesome/free-regular-svg-icons';
  12. export default function MyComments()
  13. {
  14. const [error, setError] = useState<string>('');
  15. const [loading, setLoading] = useState<boolean>(true);
  16. const [page, setPage] = useState<number>(1);
  17. const [data, setData] = useState<MyCommentsResponse>({
  18. total: 0,
  19. list: []
  20. });
  21. useEffect(() => {
  22. if (error) {
  23. alert(error);
  24. setError('');
  25. }
  26. }, [error]);
  27. useEffect(() => {
  28. setLoading(true);
  29. fetchApi<MyCommentsResponse>(`/api/mypage/comments?page=${page}&perPage=20`).then((res) => {
  30. setData(res.data!);
  31. }).catch(err => {
  32. setError(err.message);
  33. }).finally(() => {
  34. setLoading(false);
  35. });
  36. }, [page]);
  37. return (
  38. <>
  39. <NavTabs />
  40. <div id="myComments">
  41. { loading && <Loading /> }
  42. <h1>작성 댓글</h1>
  43. <div className="summary">합계: {data.total}</div>
  44. <section className="mypage-list">
  45. <article>
  46. <ul>
  47. <li>번호</li>
  48. <li>게시판</li>
  49. <li>게시글</li>
  50. <li>댓글 내용</li>
  51. <li>추천</li>
  52. <li>작성일</li>
  53. </ul>
  54. </article>
  55. <article>
  56. {data.list.length > 0 ? (
  57. data.list.map((row) => (
  58. <section key={row.id}>
  59. {/* PC */}
  60. <ol>
  61. <li>{row.num}</li>
  62. <li>{row.boardName}</li>
  63. <li>
  64. <Link href={`/post/${row.postID}`}>
  65. <em>{row.postSubject}</em>
  66. </Link>
  67. </li>
  68. <li className="comment-content">{row.content}</li>
  69. <li>{row.likes}</li>
  70. <li>{getDateTime(row.createdAt)}</li>
  71. </ol>
  72. {/* Mobile */}
  73. <dl hidden>
  74. <dt>{row.content}</dt>
  75. <dd>
  76. <ul>
  77. <li>
  78. <Link href={`/post/${row.postID}`}>
  79. [{row.boardName}] {row.postSubject}
  80. </Link>
  81. </li>
  82. <li><FontAwesomeIcon icon={faThumbsUp} /> {row.likes}</li>
  83. <li>{getDateTime(row.createdAt)}</li>
  84. </ul>
  85. </dd>
  86. </dl>
  87. </section>
  88. ))
  89. ) : (
  90. <p className="empty">작성한 댓글이 없습니다.</p>
  91. )}
  92. </article>
  93. </section>
  94. {data.list.length > 0 && (
  95. <Pagination total={data.total} page={page} perPage={20} onChange={setPage} />
  96. )}
  97. </div>
  98. </>
  99. );
  100. }