-
-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Add current page parameter to the route in the listing and se…
- Loading branch information
Showing
5 changed files
with
20 additions
and
192 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
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,59 +1,25 @@ | ||
import React, { useRef, useEffect } from 'react'; | ||
import { useHistory, useLocation } from 'react-router-dom'; | ||
import qs from 'query-string'; | ||
import { useSelector } from 'react-redux'; | ||
import { slugify } from '@plone/volto/helpers/Utils/Utils'; | ||
|
||
/** | ||
* @function useCreatePageQueryStringKey | ||
* @description A hook that creates a key with an id if there are multiple blocks with pagination. | ||
* @returns {string} Example: page || page_012345678 | ||
*/ | ||
const useCreatePageQueryStringKey = (id) => { | ||
const blockTypesWithPagination = ['search', 'listing']; | ||
const blocks = useSelector((state) => state?.content?.data?.blocks) || []; | ||
const blocksLayout = | ||
useSelector((state) => state?.content?.data?.blocks_layout?.items) || []; | ||
const displayedBlocks = blocksLayout?.map((item) => blocks[item]); | ||
const hasMultiplePaginations = | ||
displayedBlocks.filter((item) => | ||
blockTypesWithPagination.includes(item['@type']), | ||
).length > 1 || false; | ||
|
||
return hasMultiplePaginations ? slugify(`page-${id}`) : 'page'; | ||
}; | ||
import React from 'react'; | ||
import { isEqual } from 'lodash'; | ||
import { usePrevious } from './usePrevious'; | ||
import useDeepCompareEffect from 'use-deep-compare-effect'; | ||
|
||
/** | ||
* A pagination helper that tracks the query and resets pagination in case the | ||
* query changes. | ||
*/ | ||
export const usePagination = (id = null, defaultPage = 1) => { | ||
const location = useLocation(); | ||
const history = useHistory(); | ||
const pageQueryStringKey = useCreatePageQueryStringKey(id); | ||
const pageQueryParam = | ||
qs.parse(location.search)[pageQueryStringKey] || defaultPage; | ||
const [currentPage, setCurrentPage] = React.useState( | ||
parseInt(pageQueryParam), | ||
); | ||
const queryRef = useRef(qs.parse(location.search)?.query); | ||
export const usePagination = (query, defaultPage = 1) => { | ||
const previousQuery = usePrevious(query); | ||
const [currentPage, setCurrentPage] = React.useState(defaultPage); | ||
|
||
useEffect(() => { | ||
if (queryRef.current !== qs.parse(location.search)?.query) { | ||
setCurrentPage(defaultPage); | ||
queryRef.current = qs.parse(location.search)?.query; | ||
} | ||
const newParams = { | ||
...qs.parse(location.search), | ||
[pageQueryStringKey]: currentPage, | ||
}; | ||
history.replace({ | ||
search: qs.stringify(newParams), | ||
}); | ||
}, [currentPage, defaultPage, location.search, history, pageQueryStringKey]); | ||
useDeepCompareEffect(() => { | ||
setCurrentPage(defaultPage); | ||
}, [query, previousQuery, defaultPage]); | ||
|
||
return { | ||
currentPage, | ||
currentPage: | ||
previousQuery && !isEqual(previousQuery, query) | ||
? defaultPage | ||
: currentPage, | ||
setCurrentPage, | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.