|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import Image from "next/image"; |
| 3 | +import Link from "next/link"; |
| 4 | +import { format } from "date-fns"; |
| 5 | +import MedalIcon from "@/public/images/ic_medal.svg"; |
| 6 | +import { Article, ArticleList } from "@/types/Types"; |
| 7 | + |
| 8 | +const BestArticleCard = ({ article }: { article: Article }) => { |
| 9 | + const formatDate = format(article.createdAt, "yyyy. MM. dd"); |
| 10 | + |
| 11 | + return ( |
| 12 | + <> |
| 13 | + <Link href={`/board/${article.id}`}> |
| 14 | + <div> |
| 15 | + <MedalIcon alt="베스트 게시글" /> |
| 16 | + Best |
| 17 | + </div> |
| 18 | + |
| 19 | + <div> |
| 20 | + <div> |
| 21 | + <h3>{article.title}</h3> |
| 22 | + {article.image && ( |
| 23 | + <div> |
| 24 | + <Image |
| 25 | + fill |
| 26 | + src={article.image} |
| 27 | + alt={`${article.id}번 게시글 이미지`} |
| 28 | + style={{ objectFit: "contain" }} |
| 29 | + /> |
| 30 | + </div> |
| 31 | + )} |
| 32 | + </div> |
| 33 | + |
| 34 | + <div> |
| 35 | + <div>{article.writer.nickname}</div> |
| 36 | + {/* 하트 */} |
| 37 | + </div> |
| 38 | + <div>{formatDate}</div> |
| 39 | + </div> |
| 40 | + </Link> |
| 41 | + </> |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +const getPageSize = (width: number): number => { |
| 46 | + if (width < 768) { |
| 47 | + // 모바일 |
| 48 | + return 1; |
| 49 | + } else if (width < 1280) { |
| 50 | + // 태블릿 |
| 51 | + return 2; |
| 52 | + } else { |
| 53 | + // PC |
| 54 | + return 3; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +// 너비 추적 |
| 59 | +const useViewport = () => { |
| 60 | + const [width, setWidth] = useState(0); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + const handleWindowResize = () => setWidth(window.innerWidth); |
| 64 | + handleWindowResize(); |
| 65 | + window.addEventListener("resize", handleWindowResize); |
| 66 | + return () => window.removeEventListener("resize", handleWindowResize); |
| 67 | + }, []); |
| 68 | + |
| 69 | + return width; |
| 70 | +}; |
| 71 | + |
| 72 | +const BestArticle = () => { |
| 73 | + const [articles, setArticles] = useState<Article[]>([]); |
| 74 | + const [pageSize, setPageSize] = useState<number | null>(null); |
| 75 | + |
| 76 | + const viewportWidth = useViewport(); |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + if (viewportWidth === 0) return; |
| 80 | + |
| 81 | + const newPageSize = getPageSize(viewportWidth); |
| 82 | + |
| 83 | + if (newPageSize !== pageSize) { |
| 84 | + setPageSize(newPageSize); |
| 85 | + |
| 86 | + const fetchArticles = async (size: number) => { |
| 87 | + try { |
| 88 | + const response = await fetch( |
| 89 | + `https://panda-market-api.vercel.app/articles?orderBy=like&pageSize=${size}` |
| 90 | + ); |
| 91 | + const data: ArticleList = await response.json(); |
| 92 | + setArticles(data.list); |
| 93 | + } catch (error) { |
| 94 | + console.error("실패:", error); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + fetchArticles(newPageSize); |
| 99 | + } |
| 100 | + }, [viewportWidth, pageSize]); |
| 101 | + |
| 102 | + return ( |
| 103 | + <div> |
| 104 | + <div> |
| 105 | + <h2>베스트 게시글</h2> |
| 106 | + </div> |
| 107 | + |
| 108 | + <div> |
| 109 | + {articles.map((article) => ( |
| 110 | + <BestArticleCard |
| 111 | + key={`best-article-${article.id}`} |
| 112 | + article={article} |
| 113 | + /> |
| 114 | + ))} |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + ); |
| 118 | +}; |
| 119 | + |
| 120 | +export default BestArticle; |
0 commit comments