-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Common: paging model 수정 - Feed: MSW Mock에 error 대응 추가 Ref #54
- Loading branch information
Showing
7 changed files
with
204 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { styled } from '@nolmungshemung/ui-kits'; | ||
|
||
type PaginationProps = { | ||
totalPages: number; | ||
displayAmount: number; | ||
currentPage: number; | ||
setPage: (page: number) => void; | ||
}; | ||
|
||
const StyledPageButton = styled('button', { | ||
'&.currentPage': { | ||
fontWeight: 'bold', | ||
}, | ||
}); | ||
|
||
export function Pagination({ | ||
totalPages, | ||
displayAmount, | ||
currentPage, | ||
setPage, | ||
}: PaginationProps) { | ||
const pagesStart = | ||
Math.floor((currentPage - 1) / displayAmount) * displayAmount + 1; | ||
|
||
const pagesArray = () => { | ||
if ( | ||
Math.floor(totalPages / displayAmount) === | ||
Math.floor((currentPage - 1) / displayAmount) | ||
) { | ||
const lastArraySize = Math.floor(totalPages % displayAmount); | ||
return [ | ||
...Array(lastArraySize) | ||
.fill(0) | ||
.map((_, i) => pagesStart + i), | ||
]; | ||
} else { | ||
return [ | ||
...Array(displayAmount) | ||
.fill(0) | ||
.map((_, i) => pagesStart + i), | ||
]; | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<nav> | ||
<button onClick={() => setPage(1)} disabled={currentPage === 1}> | ||
<< | ||
</button> | ||
<button | ||
onClick={() => setPage(currentPage - 1)} | ||
disabled={currentPage === 1} | ||
> | ||
< | ||
</button> | ||
{pagesArray().map((targetPage) => ( | ||
<StyledPageButton | ||
key={targetPage} | ||
onClick={() => setPage(targetPage)} | ||
className={targetPage === currentPage ? 'currentPage' : ''} | ||
> | ||
{targetPage} | ||
</StyledPageButton> | ||
))} | ||
<button | ||
onClick={() => setPage(currentPage + 1)} | ||
disabled={currentPage === totalPages} | ||
> | ||
> | ||
</button> | ||
<button | ||
onClick={() => setPage(totalPages)} | ||
disabled={currentPage === totalPages} | ||
> | ||
>> | ||
</button> | ||
</nav> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Pagination'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useState } from 'react'; | ||
import { PagingParams } from '~/data/services/services.model'; | ||
|
||
export function usePagination<T extends PagingParams>(initialState: T) { | ||
const [page, setPage] = useState<T>(initialState); | ||
const handleSetPage = (_page: number) => { | ||
if (_page !== page.page) { | ||
setPage((prev) => ({ ...prev, page: _page })); | ||
} | ||
}; | ||
return { | ||
page, | ||
handleSetPage, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
export const DEFAULT_SEARCH_RANGE = 20; | ||
export const DEFAULT_FEED_COUNT = 8; | ||
export const GRID_COLUMN_COUNT = 4; | ||
export const DEFAULT_START_PAGE = 1; |