LatestPosts.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. 'use client';
  2. import './style.scss';
  3. import '../../board/_component/style.scss';
  4. import Link from 'next/link';
  5. import { useSearchParams } from 'next/navigation';
  6. import Image, { ImageProps } from 'next/image';
  7. import { useState, useEffect } from 'react';
  8. import { clsx } from 'clsx';
  9. import { BoardListMeta } from '@/types/forum/boardMeta';
  10. import { fetchLatestPosts } from '@/lib/api/forum/board';
  11. import LatestPostsRequest from '@/dtos/request/forum/board/latestPostsRequest'
  12. import LatestPostsResponse from '@/dtos/response/forum/board/latestPostsResponse'
  13. import { throwError, isNewPost, isHotPost, formatDate } from '@/lib/utils/client';
  14. import Loading from '@/app/component/Loading';
  15. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
  16. import { faComment, faThumbsUp, faEye, faClock } from '@fortawesome/free-regular-svg-icons';
  17. import { BoardLayout } from '@/constants/forum';
  18. type Props = {
  19. boardListMeta: BoardListMeta;
  20. boardID: number;
  21. boardCode: string;
  22. postID?: number|null;
  23. }
  24. function ImageWithFallback({
  25. src,
  26. fallbackSrc = '/resources/no-image.png',
  27. alt,
  28. ...option
  29. }: ImageProps & { fallbackSrc?: string }) {
  30. const [imgSrc, setImgSrc] = useState(src);
  31. return (
  32. <Image
  33. {...option}
  34. src={imgSrc}
  35. alt={alt}
  36. onError={() => setImgSrc(fallbackSrc)}
  37. />
  38. );
  39. }
  40. export default function LatestPosts(params : Props)
  41. {
  42. const searchParams = useSearchParams();
  43. const [error, setError] = useState<string>('');
  44. const [loading, setLoading] = useState<boolean>(false);
  45. const [latestPosts, setLatestPosts] = useState<LatestPostsResponse>({
  46. list: []
  47. });
  48. const query = Object.fromEntries(searchParams.entries());
  49. useEffect(() => {
  50. if (error) {
  51. alert(error);
  52. setError('');
  53. }
  54. }, [error]);
  55. useEffect(() => {
  56. (async () => {
  57. setLoading(true);
  58. const page = searchParams.get('page');
  59. const perPage = searchParams.get('perPage');
  60. const boardPrefixID = searchParams.get('boardPrefixID');
  61. fetchLatestPosts({
  62. boardID: params.boardID,
  63. boardCode: params.boardCode,
  64. postID: params.postID,
  65. page: page ? Number(page) : null,
  66. perPage: perPage ? Number(perPage) : null,
  67. boardPrefixID: boardPrefixID ? Number(boardPrefixID) : null,
  68. sort: searchParams.get('sort'),
  69. search: searchParams.get('search'),
  70. keyword: searchParams.get('keyword'),
  71. } as LatestPostsRequest).then((res) => {
  72. throwError(res);
  73. setLatestPosts(res.data as LatestPostsResponse);
  74. }).catch((err) => {
  75. setError(err.message);
  76. }).finally(() => {
  77. setLoading(false);
  78. });
  79. })();
  80. }, []);
  81. const handleClick = (boardPrefixID: number|null) => {
  82. const querys = new URLSearchParams(searchParams.toString());
  83. if (boardPrefixID) {
  84. querys.set('prefix', String(boardPrefixID));
  85. }
  86. window.location.href = `/board/${params.boardCode}?${querys.toString()}`;
  87. }
  88. if (latestPosts.list.length <= 0) {
  89. return;
  90. }
  91. return (
  92. <>
  93. <div id="latestPosts">
  94. {loading && <Loading />}
  95. <article>
  96. {params.boardListMeta.layout == BoardLayout.Default && (
  97. <section className="default-list-layout" aria-label='일반 게시판'>
  98. <article>
  99. <ul>
  100. <li>번호</li>
  101. <li>제목</li>
  102. <li>작성자</li>
  103. <li>작성일</li>
  104. <li>조회수</li>
  105. <li>좋아요</li>
  106. </ul>
  107. </article>
  108. <article>
  109. {/* 일반 글 */}
  110. {(latestPosts.list.map((row) => {
  111. const query = Object.fromEntries(searchParams.entries());
  112. const isNew = isNewPost(params.boardListMeta.isNewIcon, row);
  113. const isHot = isHotPost(params.boardListMeta.isHotIcon, row);
  114. const createdAt = formatDate(row.createdAt);
  115. return (
  116. <section key={row.id} className={clsx({ active: row.isActive })}>
  117. {/* PC */}
  118. <ol>
  119. <li>
  120. <small>{row.no}</small>
  121. </li>
  122. <li>
  123. {row.boardPrefixID && (
  124. <button type="button" onClick={() => handleClick(row.boardPrefixID)}>
  125. [{row.boardPrefixName}]
  126. </button>
  127. )}
  128. <Link href={{
  129. pathname: '/post/' + row.id,
  130. query: {
  131. ...query
  132. }
  133. }}
  134. >
  135. <em>{row.subject}</em>
  136. {isNew && <span><img src='/resources/new.gif' alt='NEW'/></span>}
  137. {isHot && <span><img src='/resources/hot.gif' alt='HOT'/></span>}
  138. </Link>
  139. </li>
  140. <li>{row.name || row.sid}</li>
  141. <li>{createdAt}</li>
  142. <li>{row.views}</li>
  143. <li>{row.likes}</li>
  144. </ol>
  145. {/* Mobile */}
  146. <dl hidden>
  147. <dt>
  148. <Link href={{
  149. pathname: '/post/' + row.id,
  150. query: {
  151. ...query
  152. }
  153. }}
  154. >
  155. <em>{row.subject}</em>
  156. {isNew && <span><img src='/resources/new.gif' alt='NEW'/></span>}
  157. {isHot && <span><img src='/resources/hot.gif' alt='HOT'/></span>}
  158. </Link>
  159. </dt>
  160. <dd>
  161. <ul>
  162. <li>{row.name || row.sid}</li>
  163. <li><FontAwesomeIcon icon={faComment} /> {row.comments}</li>
  164. <li><FontAwesomeIcon icon={faEye} /> {row.views}</li>
  165. <li><FontAwesomeIcon icon={faThumbsUp} /> {row.likes}</li>
  166. <li>{createdAt}</li>
  167. </ul>
  168. </dd>
  169. </dl>
  170. </section>
  171. );
  172. })
  173. )}
  174. </article>
  175. </section>
  176. )}
  177. {/* 1:1문의 */}
  178. {params.boardListMeta.layout == BoardLayout.QnA && (
  179. <section className="qna-list-layout" aria-label='일반 게시판'>
  180. <article>
  181. <ul>
  182. <li>번호</li>
  183. <li>제목</li>
  184. <li>작성자</li>
  185. <li>답변 여부</li>
  186. <li>작성일</li>
  187. </ul>
  188. </article>
  189. <article>
  190. {(latestPosts.list.map((row) => {
  191. const query = Object.fromEntries(searchParams.entries());
  192. const isNew = isNewPost(params.boardListMeta.isNewIcon, row);
  193. const createdAt = formatDate(row.createdAt);
  194. return (
  195. <section key={row.id} className={clsx({ active: row.isActive })}>
  196. {/* PC */}
  197. <ol>
  198. <li>
  199. <small>{row.no}</small>
  200. </li>
  201. <li>
  202. {row.boardPrefixID && (
  203. <button type="button" onClick={() => handleClick(row.boardPrefixID)}>
  204. [{row.boardPrefixName}]
  205. </button>
  206. )}
  207. <Link href={{
  208. pathname: '/post/' + row.id,
  209. query: {
  210. ...query
  211. }
  212. }}
  213. >
  214. <em>{row.subject} {row.comments > 0 && (<span>[{row.comments}]</span>)}</em>
  215. {isNew && <span><img src='/resources/new.gif' alt='NEW'/></span>}
  216. </Link>
  217. </li>
  218. <li>{row.name || row.sid}</li>
  219. <li>
  220. {!row.isReply ? (
  221. <span className='inline-flex items-center rounded-md bg-yellow-50 px-2 py-1 text-xs font-medium text-yellow-800 ring-1 ring-yellow-600/20 ring-inset'>
  222. 대기
  223. </span>
  224. ) : (
  225. <span className='inline-flex items-center rounded-md bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-green-600/20 ring-inset'>
  226. 완료
  227. </span>
  228. )}
  229. </li>
  230. <li>{createdAt}</li>
  231. </ol>
  232. {/* Mobile */}
  233. <dl hidden>
  234. <dt>
  235. <Link href={{
  236. pathname: '/post/' + row.id,
  237. query: {
  238. ...query
  239. }
  240. }}
  241. >
  242. <em>{row.subject}</em>
  243. {isNew && <span><img src='/resources/new.gif' alt='NEW'/></span>}
  244. </Link>
  245. </dt>
  246. <dd>
  247. <ul>
  248. <li>{row.name || row.sid}</li>
  249. <li><FontAwesomeIcon icon={faComment} /> {row.comments}</li>
  250. <li>
  251. {!row.isReply ? (
  252. <span className='inline-flex items-center rounded-md bg-yellow-50 px-2 py-1 text-xs font-medium text-yellow-800 ring-1 ring-yellow-600/20 ring-inset'>
  253. 대기
  254. </span>
  255. ) : (
  256. <span className='inline-flex items-center rounded-md bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-green-600/20 ring-inset'>
  257. 완료
  258. </span>
  259. )}
  260. </li>
  261. <li>{createdAt}</li>
  262. </ul>
  263. </dd>
  264. </dl>
  265. </section>
  266. );
  267. })
  268. )}
  269. </article>
  270. </section>
  271. )}
  272. {/* 사진/동영상 글 */}
  273. {params.boardListMeta.layout == BoardLayout.Media && (
  274. <section aria-label='사진/동영상 게시판'>
  275. <article>
  276. <div className="album-list-layout">
  277. {(latestPosts.list.map((row) => {
  278. const query = Object.fromEntries(searchParams.entries());
  279. const isNew = isNewPost(params.boardListMeta.isNewIcon, row);
  280. const isHot = isHotPost(params.boardListMeta.isHotIcon, row);
  281. const createdAt = formatDate(row.createdAt);
  282. // 사진/동영상 게시판
  283. const href = `/post/${row.id}${window.location.search}`;
  284. return (
  285. <div key={row.id} className={clsx({ active: row.isActive })}>
  286. <figure>
  287. <article>
  288. <Link href={href}>
  289. {row.thumbnail ? (
  290. <ImageWithFallback src={row.thumbnail} alt={row.subject} fill style={{ objectFit: 'cover' }} loading="lazy" />
  291. ) : (
  292. <Image src='/resources/no-image.png' alt={row.subject} loading='lazy' fill />
  293. )}
  294. </Link>
  295. </article>
  296. <dl>
  297. <dt>
  298. {row.boardPrefixID && (
  299. <button type="button" onClick={() => handleClick(row.boardPrefixID)}>
  300. [{row.boardPrefixName}]
  301. </button>
  302. )}
  303. <Link href={{
  304. pathname: '/post/' + row.id,
  305. query: {
  306. ...query
  307. }
  308. }}
  309. >
  310. <em>{row.subject} {row.comments > 0 && (<span>[{row.comments}]</span>)}</em>
  311. {isNew && <span><img src='/resources/new.gif' alt='NEW'/></span>}
  312. {isHot && <span><img src='/resources/hot.gif' alt='HOT'/></span>}
  313. </Link>
  314. </dt>
  315. <dd>{row.name}</dd>
  316. </dl>
  317. <figcaption>
  318. <ul>
  319. <li><FontAwesomeIcon icon={faThumbsUp} /> {row.likes}</li>
  320. <li><FontAwesomeIcon icon={faEye} /> {row.views}</li>
  321. <li><FontAwesomeIcon icon={faClock} /> {createdAt}</li>
  322. </ul>
  323. </figcaption>
  324. </figure>
  325. </div>
  326. );
  327. })
  328. )}
  329. </div>
  330. </article>
  331. </section>
  332. )}
  333. </article>
  334. <article>
  335. <Link href={{
  336. pathname: '/board/' + params.boardCode,
  337. query: {
  338. ...query
  339. }
  340. }} className='btn btn-default'>목록으로</Link>
  341. </article>
  342. </div>
  343. </>
  344. );
  345. }