Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[최영선] sprint10 #290

Conversation

choi-youngsun
Copy link
Collaborator

@choi-youngsun choi-youngsun commented Aug 16, 2024

요구사항

기본

  • 자유 게시판 페이지 주소는 “/boards” 입니다.
  • 전체 게시글에서 드롭 다운으로 “최신 순” 또는 “좋아요 순”을 선택해서 정렬을 할 수 있습니다.
  • 게시글 목록 조회 api를 사용하여 베스트 게시글, 게시글을 구현합니다.
  • 게시글 title에 검색어가 일부 포함되면 검색이 됩니다.

심화

  • 반응형으로 보여지는 베스트 게시판 개수를 다르게 설정할때 서버에 보내는 pageSize값을 적절하게 설정합니다.
  • next의 prefetch 기능을 사용해봅니다.

주요 변경사항

  • 게시글 목록 페이지 완성
  • 검색 기능 구현
  • 게시글 등록 페이지 완성

스크린샷

스크린샷 2024-08-14 220619

image

image

멘토에게

@choi-youngsun choi-youngsun self-assigned this Aug 16, 2024
@choi-youngsun choi-youngsun added 매운맛🔥 뒤는 없습니다. 그냥 필터 없이 말해주세요. 책임은 제가 집니다. 미완성🫠 죄송합니다.. and removed 미완성🫠 죄송합니다.. labels Aug 16, 2024
Copy link
Collaborator

@arthurkimdev arthurkimdev left a comment

Choose a reason for hiding this comment

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

과제 제출하시느라 수고하셨습니다~ 👍

Comment on lines +1 to +12
import { ChangeEvent, useEffect, useRef, useState } from "react";
import {
StyledLabel,
StyledPreviewImg,
StyledFileInputBox,
StyledFileInputPlaceholder,
StyledFileInput,
StyledCancelButton,
StyledFileArea,
StyledImgArea,
} from "./FileInput.styled";

Copy link
Collaborator

Choose a reason for hiding this comment

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

지난번 리뷰 사항 반영 굳! 👍

<StyledFileInputBox>
<StyledFileInput
type="file"
accept="image/png, image/jpeg"
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

Comment on lines +93 to +104
async function getProducts() {
const query = {
orderBy: "like",
page: 1,
pageSize: 3,
};
const res = await axios.get(
`/articles?orderBy=${query.orderBy}&page=${query.page}&pageSize=${query.pageSize}`
);
const nextArticles = res.data.list;
setArticles(nextArticles);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 부분은 아래 코드에서도 추가적으로 2번 재사용이 되더라구요~ 아래처럼 모듈로 빼면 더 좋겠어요!

async function fetchArticles(query: ArticleQuery): Promise<Article[]> {
  const queryString = new URLSearchParams({
    orderBy: query.orderBy,
    page: query.page.toString(),
    pageSize: query.pageSize.toString(),
    ...(query.keyword && { keyword: query.keyword }),
  }).toString();

  const res = await axios.get<ArticleResponse>(`/articles?${queryString}`);
  return res.data.list;
}

Comment on lines +106 to +108
useEffect(() => {
getProducts();
}, []);
Copy link
Collaborator

Choose a reason for hiding this comment

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

그렇다면 위에 있는 공통 모듈 가져와서 아래처럼 변경할 수 있을거예요~
에러처리는 모듈에서는 최소한만 진행하고, 각 사용하는 곳에서 구체적인 에러처리 하는 컨셉이예요.

Suggested change
useEffect(() => {
getProducts();
}, []);
useEffect(() => {
async function getBestPosts() {
try {
const bestArticles = await fetchArticles({
orderBy: "like",
page: 1,
pageSize: 3,
});
setArticles(bestArticles);
} catch (error) {
// 에러 처리
}
}
getBestPosts();
}, []);

Comment on lines +59 to +79
export async function getServerSideProps(context: any) {
const { orderBy = "recent", keyword = "" } = context.query;

const query = {
orderBy,
page: 1,
pageSize: 30,
keyword,
};
const res = await axios.get(
`/articles?orderBy=${query.orderBy}&keyword=${query.keyword}`
);
const initialArticles = res.data.list;
return {
props: {
initialArticles,
initialOrderBy: orderBy,
initialSearchQuery: keyword,
},
};
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

공통 모듈 적용하면 아래처럼 getServerSideProps 에서도 동일하게 사용할 수 있어요~

try {
    const initialArticles = await fetchArticles({
      orderBy: orderBy as string,
      page: 1,
      pageSize: 30,
      keyword: keyword as string,
    });

    return {
      props: {
        initialArticles,
        initialOrderBy: orderBy,
        initialSearchQuery: keyword,
      },
    };
  } catch (error) {
    // 에러 처리
    return {
      notFound: true,
    };
  }

initialSearchQuery: string;
}

export async function getServerSideProps(context: any) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

import type { GetServerSidePropsContext } from 'next';

...


export async function getServerSideProps(context: GetServerSidePropsContext) {

이렇게 타입을 지정해줄 수 있어요~

@arthurkimdev arthurkimdev merged commit f8269ce into codeit-bootcamp-frontend:Next-최영선 Aug 20, 2024
@arthurkimdev arthurkimdev changed the title [최영선] sprint9 [최영선] sprint10 Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
매운맛🔥 뒤는 없습니다. 그냥 필터 없이 말해주세요. 책임은 제가 집니다.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants