-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PageNavigation Index 5 미만 시 발생하는 레이아웃 문제, 쿠키 없을 경우 발생하는 인증인가 문제 해결 (#656
- Loading branch information
1 parent
7e4c51d
commit fe4efb2
Showing
3 changed files
with
25 additions
and
12 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
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,47 +1,55 @@ | ||
import { useState } from 'react'; | ||
import { useCallback, useState } from 'react'; | ||
|
||
import type { CommunityTripsData } from '@type/trips'; | ||
|
||
import { TRIP_INDEX_UNIT_LENGTH } from '@constants/ui'; | ||
|
||
export const useTripPageIndex = (data: CommunityTripsData, page: number) => { | ||
export const useTripPageIndex = (data: CommunityTripsData, page: number, maxIndex: number) => { | ||
const countUnit = maxIndex <= TRIP_INDEX_UNIT_LENGTH ? maxIndex : TRIP_INDEX_UNIT_LENGTH; | ||
|
||
const [pageIndexDatas, setPageIndexDatas] = useState<number[]>( | ||
Array.from({ length: TRIP_INDEX_UNIT_LENGTH }, (_, index) => index) | ||
Array.from({ length: countUnit }, (_, index) => index) | ||
); | ||
|
||
const centerIndex = Math.floor(TRIP_INDEX_UNIT_LENGTH / 2); | ||
const centerIndex = Math.floor(countUnit / 2); | ||
const isCurrentPageAfterCenter = page > centerIndex; | ||
const isCurrentPageBeforeCenter = page >= data.lastPageIndex - centerIndex; | ||
const isCurrentPageCenter = page < data.lastPageIndex - centerIndex; | ||
|
||
const changeNavigationDatas = () => { | ||
const changeNavigationDatas = useCallback(() => { | ||
if (isCurrentPageAfterCenter) { | ||
setPageIndexDatas(Array.from({ length: TRIP_INDEX_UNIT_LENGTH }, (_, index) => index)); | ||
setPageIndexDatas(Array.from({ length: countUnit }, (_, index) => index)); | ||
} | ||
|
||
if (isCurrentPageBeforeCenter) { | ||
const lastIndex = data.lastPageIndex; | ||
|
||
const refreshedIndexDatas = Array.from( | ||
{ length: TRIP_INDEX_UNIT_LENGTH }, | ||
{ length: countUnit }, | ||
(_, index) => lastIndex - index | ||
).reverse(); | ||
|
||
setPageIndexDatas(refreshedIndexDatas); | ||
} | ||
|
||
if (isCurrentPageCenter) { | ||
const halfLength = Math.floor(TRIP_INDEX_UNIT_LENGTH / 2); | ||
const halfLength = Math.floor(countUnit / 2); | ||
const startPage = Math.max(page - halfLength, 1); | ||
|
||
const refreshedIndexDatas = Array.from( | ||
{ length: TRIP_INDEX_UNIT_LENGTH }, | ||
{ length: countUnit }, | ||
(_, index) => startPage + index | ||
); | ||
|
||
setPageIndexDatas(refreshedIndexDatas); | ||
} | ||
}; | ||
}, [ | ||
data.lastPageIndex, | ||
isCurrentPageAfterCenter, | ||
isCurrentPageBeforeCenter, | ||
isCurrentPageCenter, | ||
page, | ||
]); | ||
|
||
return { pageIndexDatas, changeNavigationDatas }; | ||
}; |
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