-
Notifications
You must be signed in to change notification settings - Fork 685
bug: fix pagination double fetch #1381
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
792f79e
Fix pagination by adding an initial page value for the current page s…
sirugh 3977aea
Fix pagination tests
sirugh 5e1093c
Move pagination state and query parameter management from pagination …
sirugh b90f37e
push todo
sirugh 850608f
Add initialTotalPages param to usePagination hook
sirugh 7f1ee93
Add TODOs for pageSize default value from storeConfig
sirugh 74b9b2c
Refactor react router out of pagination component and fix tests
sirugh 97975e8
fix snap
sirugh 57dcc54
Get initial page value from queryparam or use default
sirugh 3ef0ed2
Pass location
sirugh 2222cf2
Update retry logic. Only render error or loading indicator in specifi…
sirugh 6cb6bf6
Merge branch 'develop' into bug/double-loaded-category-images
sirugh 72a32a6
Refactor tile and navigation to not use inline functions. Update useP…
sirugh ce63b32
Merge branch 'develop' into bug/double-loaded-category-images
supernova-at d45336e
Merge branch 'develop' into bug/double-loaded-category-images
devpatil7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,14 +1,95 @@ | ||
| import { useMemo, useState } from 'react'; | ||
| import { useCallback, useMemo, useState } from 'react'; | ||
| import { getSearchParam } from './useSearchParam'; | ||
|
|
||
| export const usePagination = () => { | ||
| const [currentPage, setCurrentPage] = useState(0); | ||
| const [totalPages, setTotalPages] = useState(null); | ||
| /** | ||
| * Sets a query parameter in history. Attempt to use React Router if provided | ||
| * otherwise fallback to builtins. | ||
| */ | ||
| const setQueryParam = ({ location, history, parameter, value }) => { | ||
| const { search } = location; | ||
| const queryParams = new URLSearchParams(search); | ||
| queryParams.set(parameter, value); | ||
|
|
||
| if (history.push) { | ||
| history.push({ search: queryParams.toString() }); | ||
| } else { | ||
| // Use the native pushState. See https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method | ||
| history.pushState({ search: queryParams.toString() }, ''); | ||
| } | ||
| }; | ||
|
|
||
| const defaultInitialPage = 1; | ||
|
|
||
| /** | ||
| * `usePagination` provides a pagination state with `currentPage` and | ||
| * `totalPages` as well as an API for interacting with the state. | ||
| * | ||
| * @param {Object} location the location object, like window.location, or from react router | ||
| * @param {Object} history the history object, like window.history, or from react router | ||
| * @param {String} namespace the namespace to apply to the pagination query | ||
| * @param {String} parameter the name of the query parameter to use for page | ||
| * @param {Number} initialPage the initial current page value | ||
| * @param {Number} intialTotalPages the total pages expected to be usable by this hook | ||
| * | ||
| * TODO update with defaults | ||
| * | ||
| * @returns {[PaginationState, PaginationApi]} | ||
| */ | ||
| export const usePagination = ({ | ||
| location = window.location, | ||
| history = window.history, | ||
| namespace = '', | ||
| parameter = 'page', | ||
| initialPage, | ||
| initialTotalPages = 1 | ||
| } = {}) => { | ||
| const searchParam = namespace ? `${namespace}_${parameter}` : parameter; | ||
| if (!initialPage) { | ||
| // We need to synchronously fetch the initial page value from the query | ||
| // param otherwise we would initialize this value twice. | ||
| initialPage = parseInt( | ||
| getSearchParam(searchParam, location) || defaultInitialPage | ||
| ); | ||
| } | ||
|
|
||
| const [currentPage, setCurrentPage] = useState(initialPage); | ||
| const [totalPages, setTotalPages] = useState(initialTotalPages); | ||
|
|
||
| const setPage = useCallback( | ||
| page => { | ||
| // Update the query parameter. | ||
| setQueryParam({ | ||
| location, | ||
| history, | ||
| parameter: searchParam, | ||
| value: page | ||
| }); | ||
|
|
||
| // Update the state object. | ||
| setCurrentPage(page); | ||
| }, | ||
| [history, location, searchParam] | ||
| ); | ||
|
|
||
| /** | ||
| * @typedef PaginationState | ||
| * @property {Number} currentPage the current page | ||
| * @property {Number} totalPages the total pages | ||
| */ | ||
| const paginationState = { currentPage, totalPages }; | ||
| const api = useMemo(() => ({ setCurrentPage, setTotalPages }), [ | ||
| setCurrentPage, | ||
| setTotalPages | ||
| ]); | ||
|
|
||
| /** | ||
| * @typedef PaginationApi | ||
| * @property {Function} setCurrentPage | ||
| * @property {Function} setTotalPages | ||
| */ | ||
| const api = useMemo( | ||
| () => ({ | ||
| setCurrentPage: setPage, | ||
| setTotalPages | ||
| }), | ||
| [setPage, setTotalPages] | ||
| ); | ||
|
|
||
| return [paginationState, api]; | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
26 changes: 26 additions & 0 deletions
26
...s/venia-concept/src/components/Pagination/__tests__/__snapshots__/pagination.spec.js.snap
This file contains hidden or 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,26 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`renders nothing when there is only 1 page 1`] = `null`; | ||
|
|
||
| exports[`renders when there is more than 1 page 1`] = ` | ||
| <div | ||
| className="root" | ||
| > | ||
| <button | ||
| onClick={[Function]} | ||
| > | ||
| <div /> | ||
| 1 | ||
| </button> | ||
| <button | ||
| onClick={[Function]} | ||
| > | ||
| 2 | ||
| </button> | ||
| <button | ||
| onClick={[Function]} | ||
| > | ||
| 3 | ||
| </button> | ||
| </div> | ||
| `; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only includes an
_if namespace is provided. It was incorrectly using a param of_page.