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 #261

Merged
Show file tree
Hide file tree
Changes from all commits
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: 4 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const nextConfig = {
reactStrictMode: true,

images: {
domains: ['sprint-fe-project.s3.ap-northeast-2.amazonaws.com'],
domains: [
'sprint-fe-project.s3.ap-northeast-2.amazonaws.com',
'example.com',
],
},
}

Expand Down
10 changes: 10 additions & 0 deletions public/svgs/comment.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions public/svgs/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions public/svgs/seemore.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/components/Button/Button.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
transition: background-color 0.3s ease;

&:hover {
background-color: #0056b3;
background-color: var(--dark-blue);
}
}
4 changes: 2 additions & 2 deletions src/components/DropDown/DropDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ const DropDown: React.FC<DropDownProps> = ({ options, setOption }) => {

{isDrop && (
<div className={styles.dropDownBox}>
{Object.keys(options).map((key) => (
{Object.entries(options).map(([key, value]) => (
<div
key={key}
className={styles.dropDownItem}
onClick={() => handleOptionClick(key)}
>
{key}
{value}
Copy link
Collaborator

Choose a reason for hiding this comment

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

여기서 value로 바꿔줬기때문에 지금 dropdown에서 영어로 나오고 있어요!
만약 value로 여기를 바꾸면 내려오는 options도 다 바꿔줘야합니다!

</div>
))}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchBar/SearchBar.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}

&::placeholder {
color: var(--gray-200);
color: var(--gray-400);
font-size: 16px;
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/components/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useState, useCallback, useEffect } from 'react'
import Image from 'next/image'
import styles from './SearchBar.module.scss'
import searchIcon from '@/public/svgs/search.svg'
Expand All @@ -9,6 +9,23 @@ interface SearchBarProps {

const SearchBar: React.FC<SearchBarProps> = ({ keyword }) => {
const [searchTerm, setSearchTerm] = useState('')
const [debouncedSearchTerm, setDebouncedSearchTerm] = useState('')

useEffect(() => {
const timer = setTimeout(() => {
setDebouncedSearchTerm(searchTerm)
}, 2000)

return () => {
clearTimeout(timer)
}
}, [searchTerm])

useEffect(() => {
if (debouncedSearchTerm) {
keyword(debouncedSearchTerm)
}
}, [debouncedSearchTerm, keyword])

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchTerm(e.target.value)
Expand Down
4 changes: 2 additions & 2 deletions src/components/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Image from 'next/image'
import spinnerImg from '@/public/images/spinner.png'
import styles from './Spiner.module.scss'
import styles from './Spinner.module.scss'

export default function Spiner({ className = '' }) {
export default function Spinner({ className = '' }) {
return (
<div className={styles.container}>
<Image
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Image from 'next/image'
import Link from 'next/link'
import styles from './Article.module.scss'
import formatDate from '@/src/utils/formaDate'
import { Article } from '@/src/interfaces/Article.interface'
Expand All @@ -11,25 +12,27 @@ export default function Article({ article }: { article: Article }) {

return (
<div className={styles.article}>
<div className={styles.contentSection}>
<div className={styles.title}>{article.title}</div>
<div
className={styles.image}
style={{
border: articleImage === '' ? 'none' : '1px solid var(--gray-200)',
}}
>
{articleImage && (
<Image
src={articleImage}
alt="게시글 이미지"
width={50}
height={50}
/>
)}
<Link href={`/boards/${article.id}`}>
<div className={styles.contentSection}>
<div className={styles.title}>{article.title}</div>
<div
className={styles.image}
style={{
border:
articleImage === '' ? 'none' : '1px solid var(--gray-200)',
}}
>
{articleImage && (
<Image
src={articleImage}
alt="게시글 이미지"
width={50}
height={50}
/>
)}
</div>
</div>
</div>

</Link>
<div className={styles.subContentSection}>
<div className={styles.writerSection}>
<div className={styles.nickname}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from 'react'
import { useState, useMemo } from 'react'
import Link from 'next/link'
import useFetechData from '@/src/hooks/useFetechData'
import { ArticleListResponse } from '@/src/interfaces/Article.interface'
import Article from './Article'
Expand All @@ -9,14 +10,19 @@ import Button from '@/src/components/Button/Button'
import styles from './Article.module.scss'

export default function ArticleList() {
const [orderBy, setOrderBy] = useState('recent')
type OrderOption = 'recent' | 'like'
Copy link
Collaborator

Choose a reason for hiding this comment

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

여기서 dropdown으로 내려주는 친구들이있네요!
우선 보통 type은 컴포넌트 밖에서 선언을 해줘요.
그리고 oderOption에 해당하는 값들은 상수로 선언해주면 더 보기도 편하고 유지보수도 좋을 것 같아요!

const ODER_OPTION = {
  recent: '최신순',
  like: '인기순',
} as const

type OrderOption = keyof typeof ODER_OPTION

이렇게 key에 영어를 넣고 value에 한글을 넣으면 드롭다운에서 value로 보여줄 때 한글로 잘보일거에요!

Copy link
Collaborator

Choose a reason for hiding this comment

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

아마 여기를 이렇게 바꾸면 타입에러가 조금씩 발생할 거에요. 그부분은 한번 고쳐서 타입세이프하게 만들어보세요 😃

const [orderBy, setOrderBy] = useState<OrderOption>('recent')
const [searchTitle, setSearchTitle] = useState('')
const fetechArticles = useFetechData<ArticleListResponse>(
`articles?page=1`,
10,
orderBy,
searchTitle
)

const url = useMemo(() => {
let baseUrl = `articles?page=1&pageSize=10&orderBy=${orderBy}`
if (searchTitle) {
baseUrl += `&keyword=${searchTitle}`
}
return baseUrl
}, [orderBy, searchTitle])
Comment on lines +17 to +23
Copy link
Collaborator

Choose a reason for hiding this comment

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

useMemo를 사용하여 url변화값을 잘 메모해주셨어요!

한가지 종종사용되는 부분을 추가적으로 말씀드리자면 orderByt나 keyword같이 검색에 필요한 파라미터들은 router를 이용하여 실제 url query에 넣기도합니다.
그렇게 했을 때 좋은 점은 실제로 유저의 행동을 history에 기억할 수 있다는 점이에요!
예를들어서 무언가를 검색하고 다른페이지로 넘어갔을 때, 뒤로가기를 누르면 그 검색한 키워드를 기억 할 수 있겠죠?!


const fetechArticles = useFetechData<ArticleListResponse>(url)
const { data: ArticleList, isLoading } = fetechArticles
Comment on lines +25 to 26
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
const fetechArticles = useFetechData<ArticleListResponse>(url)
const { data: ArticleList, isLoading } = fetechArticles
const { data: articleList, isLoading } = useFetchData<ArticleListResponse>(url)

하나로 합칠 수 있을 것 같아요!
그리고 변수이름은 camelCase로 해주는 것이 일반적입니다~ 오타도 하나 있네요!

const orderOptions = {
최신순: 'recent',
Expand All @@ -34,15 +40,23 @@ export default function ArticleList() {
return <Spiner />
}
if (ArticleList?.totalCount === 0) {
return <>게시글이 없습니다.</>
return (
<>
<SearchBar keyword={handleSearchTitle} />
<br />
게시글이 없습니다.
</>
)
}

return (
<div className={styles.container}>
<div className={styles.containerTitleSection}>
<div className={styles.containerTitle}>게시글</div>

<Button>글쓰기</Button>
<Link href="/boards/create">
<Button>글쓰기</Button>
</Link>
</div>

<div className={styles.controlSection}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Image from 'next/image'
import Link from 'next/link'
import styles from './BestArticle.module.scss'
import formatDate from '@/src/utils/formaDate'
import { Article } from '@/src/interfaces/Article.interface'
Expand All @@ -12,28 +13,29 @@ export default function BestArticle({ article }: { article: Article }) {
return (
<div className={styles.article}>
<Image src={badgeIcon} alt="베스트 게시글 배지" width={102} height={30} />

<div className={styles.contentSection}>
<div className={article.title}>{article.title}</div>
<div
className={styles.image}
style={{
border: articleImage === '' ? 'none' : '1px solid var(--gray-200)',
backgroundColor:
articleImage === '' ? 'var(--gray-50)' : 'var(--white)',
}}
>
{articleImage && (
<Image
src={articleImage}
alt="게시글 이미지"
width={50}
height={50}
/>
)}
<Link href={`/boards/${article.id}`}>
<div className={styles.contentSection}>
<div className={article.title}>{article.title}</div>
<div
className={styles.image}
style={{
border:
articleImage === '' ? 'none' : '1px solid var(--gray-200)',
backgroundColor:
articleImage === '' ? 'var(--gray-50)' : 'var(--white)',
}}
>
{articleImage && (
<Image
src={articleImage}
alt="게시글 이미지"
width={50}
height={50}
/>
)}
</div>
</div>
</div>

</Link>
<div className={styles.subContentSection}>
<div className={styles.writerSection}>
<div className={styles.nickname}>{article.writer.nickname}</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'react'
import { useState, useEffect, useMemo } from 'react'
import useFetechData from '@/src/hooks/useFetechData'
import { ArticleListResponse } from '@/src/interfaces/Article.interface'
import BestArticle from './BestArticle'
Expand All @@ -19,11 +19,11 @@ const getPageSize = () => {
export default function BestArticleList() {
const [pageSize, setPageSize] = useState<number>(getPageSize())

const fetechArticles = useFetechData<ArticleListResponse>(
`articles?page=1`,
pageSize,
'like'
const url = useMemo(
() => `articles?page=1&pageSize=${pageSize}&orderBy=like`,
[pageSize]
)
const fetechArticles = useFetechData<ArticleListResponse>(url)
const { data: ArticleList, isLoading } = fetechArticles

useEffect(() => {
Expand Down
Loading
Loading