-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(snippet): implement pagination navigation on client side - wip
- Loading branch information
1 parent
815f18d
commit 2f79443
Showing
3 changed files
with
133 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState } from 'react'; | ||
|
||
type Page = { | ||
nextToken: string | null; | ||
previousToken: string | null; | ||
}; | ||
|
||
export const usePaginationToken = (nextToken: string | null) => { | ||
const [pages, setPages] = useState<Page[]>([{ nextToken, previousToken: null }]); | ||
const [canGoForward, setCanGoForward] = useState(!!nextToken); | ||
const [canGoBack, setCanGoBack] = useState(false); | ||
|
||
const getPage = (): Page | undefined => { | ||
return pages.pop(); | ||
}; | ||
|
||
const addPageFromNext = (token: string | null) => { | ||
const lastPage = pages.length > 0 ? pages[pages.length - 1] : undefined; | ||
|
||
const newPage: Page = { | ||
nextToken: token, | ||
previousToken: lastPage?.nextToken ?? null, | ||
}; | ||
|
||
setPages([...pages, newPage]); | ||
|
||
setCanGoBack(!!newPage.previousToken); | ||
setCanGoForward(!!newPage.nextToken); | ||
}; | ||
|
||
const addPageFromPrevious = (token: string | null) => { | ||
// should do nothing ??? | ||
const lastPage = pages.length > 0 ? pages[pages.length - 1] : undefined; | ||
|
||
setCanGoBack(!!lastPage?.previousToken); | ||
setCanGoForward(!!lastPage?.nextToken); | ||
}; | ||
|
||
return { | ||
addPageFromNext, | ||
addPageFromPrevious, | ||
canGoBack, | ||
canGoForward, | ||
getPage, | ||
}; | ||
}; |
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