Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/components/board-best-list.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
border-radius: 8px;
padding: 0 24px;
height: 170px;
transition: all 0.2s;
}
.board_best_box:hover {
background-color: #f3f4f6;
transform: translateY(-12px);
}
.board_best_box > a {
position: relative;
Expand Down
7 changes: 7 additions & 0 deletions src/components/board-list.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
margin-bottom: 24px;
padding-bottom: 24px;
border-bottom: 1px solid #e5e7eb;
transition: all 0.2s;
}
.board_common_box:hover {
padding: 12px;
background-color: #f3f4f6;
border-radius: 12px;
}
.board_common_box > a {
display: flex;
Expand All @@ -15,6 +21,7 @@
font-weight: 600;
min-height: 72px;
margin-bottom: 16px;
padding-right: 24px;
}
.board_common_box .left .bottom {
display: flex;
Expand Down
5 changes: 3 additions & 2 deletions src/lib/fetch-board-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Article, BoardListResponse } from "../../types";
export default async function fetchBoardList(
page = 1,
pageSize = 10,
orderBy = "recent"
orderBy = "recent",
keyword = ""
): Promise<Article[]> {
const url = `https://panda-market-api.vercel.app/articles?page=${page}&pageSize=${pageSize}&orderBy=${orderBy}`;
const url = `https://panda-market-api.vercel.app/articles?page=${page}&pageSize=${pageSize}&orderBy=${orderBy}&keyword=${keyword}`;
try {
const response = await fetch(url);
if (!response.ok) {
Expand Down
42 changes: 23 additions & 19 deletions src/pages/boards/index.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import fetchBoardList from "@/lib/fetch-board-list";
import { InferGetServerSidePropsType } from "next";
import styles from "./board.module.css";
import BoardBestList from "@/components/board-best-list";
import BoardList from "@/components/board-list";
import Link from "next/link";
import { useState } from "react";
import { useEffect, useState } from "react";
import { Article } from "../../../types";

export const getServerSideProps = async () => {
const list = await fetchBoardList(1, 3, "like");
const commonList = await fetchBoardList(1, 10, "recent");
return {
props: {
list,
commonList,
},
};
};

export default function Page({
list,
commonList,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
export default function Page() {
const [sortState, setSortState] = useState(false);
const [order, setOrder] = useState("recent");
const [list, setList] = useState<Article[]>([]);
const [commonList, setCommonList] = useState<Article[]>([]);

const onSortToggle = () => {
setSortState(!sortState);
};

const fetchData = async () => {
try {
const bestResponse = await fetchBoardList(1, 3, "like");
const commonResponse = await fetchBoardList(1, 10, order);

setList(bestResponse);
setCommonList(commonResponse);
} catch (error) {
console.error("데이터 가져오기 실패:", error);
}
};

useEffect(() => {
fetchData();
}, [order]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런식으로 작성하려면 fetchData를 useCallback으로 만들고 useEffect의 의존성 배열에 fetchData를 집어넣어야해요


const sortChange = (state: string) => {
if (state !== order) {
setOrder(state);
Expand All @@ -40,7 +44,7 @@ export default function Page({
<div className="common_title">베스트 게시글</div>
<ul className={styles.board_best}>
{list.length === 0 ? (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lodash같은 라이브러리를 사용해보는건 어떤가요?

<p>데이터가 없습니다.</p>
<p></p>
) : (
list.map((el) => <BoardBestList key={el.id} {...el} />)
)}
Expand Down Expand Up @@ -81,7 +85,7 @@ export default function Page({

<ul className={styles.board_common}>
{commonList.length === 0 ? (
<p>데이터가 없습니다.</p>
<p></p>
) : (
commonList.map((el) => <BoardList key={el.id} {...el} />)
)}
Expand Down