Skip to content

Commit

Permalink
Merge pull request #22 from Help-M-Ssaem/feat/#21
Browse files Browse the repository at this point in the history
[Feat] Pagination, SearchBar 공통 컴포넌트
  • Loading branch information
uiop5809 authored Aug 9, 2024
2 parents 9b2d96a + 3208c02 commit 476f15d
Show file tree
Hide file tree
Showing 14 changed files with 610 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Preview } from '@storybook/react'
import '../src/app/globals.css'
import '../src/styles/globals.css'

const preview: Preview = {
parameters: {
Expand Down
34 changes: 27 additions & 7 deletions src/app/board/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import Board from '@/components/board/Board'
import MbtiCategories from '@/components/board/MbtiCategories'
import Button from '@/components/common/Button'
import Container from '@/components/common/Container'
import Pagination from '@/components/common/Pagination'
import SearchBar from '@/components/common/SearchBar'
import { useBoardList } from '@/service/board/useBoardService'
import { useState } from 'react'

const BoardPage = () => {
const [mbti, setMbti] = useState<string>('all')
// const [page, setPage] = useState<number>(0)
const [page, setPage] = useState<number>(1)
const pageSize = 6

const { data } = useBoardList(mbti, 0, 6)
const { data: boardList } = useBoardList(mbti, page - 1, pageSize)

const handlePageChange = (newPage: number) => {
setPage(newPage)
}

return (
<>
Expand All @@ -29,13 +36,26 @@ const BoardPage = () => {
/>
</div>
<div className="h-[1px] bg-main" />
{data &&
data.result.map((board) => (
<>
<Board key={board.id} board={board} />
{boardList &&
boardList.result.map((board) => (
<div key={board.id}>
<Board board={board} />
<div className="h-[1px] bg-main" />
</>
</div>
))}

{boardList && (
<div className="mt-5">
<Pagination
pagesCount={boardList.totalSize}
currentPage={page}
onPageChange={handlePageChange}
/>
</div>
)}
<div className="my-7.5">
<SearchBar onSearch={() => {}} />
</div>
</Container>
</>
)
Expand Down
38 changes: 29 additions & 9 deletions src/app/discussion/page.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
'use client'

import DiscussionBoard from '@/components/discussion/DiscussionBoard'
import Pagination from '@/components/common/Pagination'
import { useDiscussionList } from '@/service/discussion/useDiscussionService'
import { useState } from 'react'
import SearchBar from '@/components/common/SearchBar'

const DiscussionPage = () => {
const { data: discussionList } = useDiscussionList(0, 10)
const [page, setPage] = useState<number>(1)
const pageSize = 6

const { data: discussionList } = useDiscussionList(page - 1, pageSize)
const handlePageChange = (newPage: number) => {
setPage(newPage)
}

return (
<>
<div className="text-title3 text-maindark font-semibold my-5">
MBTI 과몰입 토론
</div>
<div className="flex flex-col gap-2.5 sm:gap-16.5">
<div className="flex flex-col">
{discussionList &&
discussionList.result.map((discussion) => (
<>
<DiscussionBoard
key={discussion.id}
discussionBoard={discussion}
/>
<div className="h-[1px] bg-gray4" />
</>
<div key={discussion.id} className="flex flex-col">
<DiscussionBoard discussionBoard={discussion} />
<div className="h-[1px] bg-gray4 my-2.5 sm:my-12.5" />
</div>
))}
</div>

{discussionList && (
<div className="mt-5">
<Pagination
pagesCount={discussionList.totalSize}
currentPage={page}
onPageChange={handlePageChange}
/>
</div>
)}

<div className="my-7.5">
<SearchBar onSearch={() => {}} />
</div>
</>
)
}
Expand Down
58 changes: 48 additions & 10 deletions src/app/worry/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,45 @@
import Container from '@/components/common/Container'
import MbtiSelect from '@/components/worry/MbtiSelect'
import WorryBoard from '@/components/worry/WorryBoard'
import Pagination from '@/components/common/Pagination'
import {
useSolvedWorryList,
useWaitingWorryList,
} from '@/service/worry/useWorryService'
import { useState } from 'react'
import SearchBar from '@/components/common/SearchBar'

const WorryPage = () => {
const [waitingPage, setWaitingPage] = useState<number>(1)
const [solvedPage, setSolvedPage] = useState<number>(1)

const [waitingStrFromMbti, setWaitingStrFromMbti] = useState('ALL')
const [waitingStrToMbti, setWaitingStrToMbti] = useState('ALL')
const [solvedStrFromMbti, setSolvedStrFromMbti] = useState('ALL')
const [solvedStrToMbti, setSolvedStrToMbti] = useState('ALL')

const pageSize = 6

const { data: waitingWorryList } = useWaitingWorryList(
0,
10,
waitingPage - 1,
pageSize,
waitingStrFromMbti,
waitingStrToMbti,
)
const { data: solvedWorryList } = useSolvedWorryList(
0,
10,
solvedPage - 1,
pageSize,
solvedStrFromMbti,
solvedStrToMbti,
)

const handleWaitingPageChange = (newPage: number) => {
setWaitingPage(newPage)
}
const handleSolvedPageChange = (newPage: number) => {
setSolvedPage(newPage)
}

return (
<div className="flex flex-col">
<div className="text-title3 text-maindark font-semibold my-5">
Expand All @@ -43,11 +57,21 @@ const WorryPage = () => {
<div className="h-[1px] bg-main" />
{waitingWorryList &&
waitingWorryList.result.map((worry) => (
<>
<WorryBoard key={worry.id} worryBoard={worry} />
<div key={worry.id}>
<WorryBoard worryBoard={worry} />
<div className="h-[1px] bg-main" />
</>
</div>
))}

{waitingWorryList && (
<div className="mt-5">
<Pagination
pagesCount={waitingWorryList.totalSize}
currentPage={waitingPage}
onPageChange={handleWaitingPageChange}
/>
</div>
)}
</Container>

<div className="text-title3 text-maindark font-semibold my-5">
Expand All @@ -63,11 +87,25 @@ const WorryPage = () => {
<div className="h-[1px] bg-main" />
{solvedWorryList &&
solvedWorryList.result.map((worry) => (
<>
<WorryBoard key={worry.id} worryBoard={worry} />
<div key={worry.id}>
<WorryBoard worryBoard={worry} />
<div className="h-[1px] bg-main" />
</>
</div>
))}

{solvedWorryList && (
<div className="mt-5">
<Pagination
pagesCount={solvedWorryList.totalSize}
currentPage={solvedPage}
onPageChange={handleSolvedPageChange}
/>
</div>
)}

<div className="my-7.5">
<SearchBar onSearch={() => {}} />
</div>
</Container>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/chatting/ChattingProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import ChattingProfileI from '@/model/Chatting'
import Image from 'next/image'
import { ChattingProfileI } from '@/model/Chatting'
import Button from '../common/Button'

export interface ChattingProfileProps {
Expand Down
36 changes: 36 additions & 0 deletions src/components/common/Pagination.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useState } from 'react'
import Pagination, { PaginationProps } from '@/components/common/Pagination'
import { Meta, StoryFn } from '@storybook/react'

export default {
title: 'Common/Pagination',
component: Pagination,
} as Meta

const Template: StoryFn<PaginationProps> = ({
pagesCount,
currentPage: initialCurrentPage,
onPageChange,
}: PaginationProps) => {
const [currentPage, setCurrentPage] = useState(initialCurrentPage)

const handlePageChange = (page: number) => {
setCurrentPage(page)
onPageChange(page)
}

return (
<Pagination
pagesCount={pagesCount}
currentPage={currentPage}
onPageChange={handlePageChange}
/>
)
}

export const Primary = Template.bind({})
Primary.args = {
pagesCount: 5,
currentPage: 1,
onPageChange: () => {},
}
101 changes: 101 additions & 0 deletions src/components/common/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React, { useState, useEffect } from 'react'

export interface PaginationProps {
pagesCount: number
currentPage: number
onPageChange: (page: number) => void
}

const Pagination = ({
pagesCount,
currentPage,
onPageChange,
}: PaginationProps) => {
const [currentPageGroup, setCurrentPageGroup] = useState<number>(1)
const pagesPerGroup = 5

const totalGroups = Math.ceil(pagesCount / pagesPerGroup)

useEffect(() => {
const newPageGroup = Math.ceil(currentPage / pagesPerGroup)
setCurrentPageGroup(newPageGroup)
}, [currentPage])

const startPage = (currentPageGroup - 1) * pagesPerGroup + 1
const endPage = Math.min(currentPageGroup * pagesPerGroup, pagesCount)

const pages = Array.from(
{ length: endPage - startPage + 1 },
(_, i) => startPage + i,
)

const handlePageChange = (page: number) => {
onPageChange(page)
window.scrollTo({ top: 0, behavior: 'smooth' }) // 화면을 최상단으로 부드럽게 스크롤
}

const handlePreviousGroup = () => {
if (currentPageGroup > 1) {
const newPageGroup = currentPageGroup - 1
setCurrentPageGroup(newPageGroup)
handlePageChange((newPageGroup - 1) * pagesPerGroup + 1)
}
}

const handleNextGroup = () => {
if (currentPageGroup < totalGroups) {
const newPageGroup = currentPageGroup + 1
setCurrentPageGroup(newPageGroup)
handlePageChange((newPageGroup - 1) * pagesPerGroup + 1)
}
}

return (
<nav>
<ul className="flex justify-center items-center gap-3">
<li className={`page-item ${currentPageGroup === 1 ? 'disabled' : ''}`}>
<button
type="button"
className="text-headline text-gray2 font-semibold"
onClick={handlePreviousGroup}
disabled={currentPageGroup === 1}
>
이전
</button>
</li>
{pages.map((page) => (
<li key={page} className={`${page === currentPage ? 'active' : ''}`}>
<button
type="button"
className={`text-headline text-gray2 font-semibold w-8 h-8 border rounded-[3px] ${
page === currentPage
? 'border-pointcolor1 bg-main4 text-maindark'
: 'border-transparent'
}`}
onClick={() => handlePageChange(page)}
aria-current={page === currentPage ? 'page' : undefined}
>
{page}
</button>
</li>
))}
<li
className={`page-item ${
currentPageGroup === totalGroups ? 'disabled' : ''
}`}
>
<button
type="button"
className="text-headline text-gray2 font-semibold"
onClick={handleNextGroup}
disabled={currentPageGroup === totalGroups}
>
다음
</button>
</li>
</ul>
</nav>
)
}

export default Pagination
19 changes: 19 additions & 0 deletions src/components/common/SearchBar.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import SearchBar, { SearchBarProps } from '@/components/common/SearchBar'
import { Meta, StoryFn } from '@storybook/react'

export default {
title: 'Common/SearchBar',
component: SearchBar,
} as Meta<SearchBarProps>

const Template: StoryFn<SearchBarProps> = (args: SearchBarProps) => (
<SearchBar {...args} />
)

export const Primary = Template.bind({})
Primary.args = {
onSearch: (filter, query) => {
console.log('filter:', filter)
console.log('query:', query)
},
}
Loading

0 comments on commit 476f15d

Please sign in to comment.