Commit 7071d972 authored by tom's avatar tom

add page num to query

parent b385265d
...@@ -22,11 +22,12 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -22,11 +22,12 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
const paginationFields = PAGINATION_FIELDS[queryName]; const paginationFields = PAGINATION_FIELDS[queryName];
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const router = useRouter(); const router = useRouter();
const [ page, setPage ] = React.useState(1); const [ page, setPage ] = React.useState<number>(router.query.page && !Array.isArray(router.query.page) ? Number(router.query.page) : 1);
const currPageParams = pick(router.query, paginationFields); const currPageParams = pick(router.query, paginationFields);
const [ pageParams, setPageParams ] = React.useState<Array<PaginationParams<QueryName>>>([ ]); const [ pageParams, setPageParams ] = React.useState<Array<PaginationParams<QueryName>>>([ ]);
const fetch = useFetch(); const fetch = useFetch();
const isMounted = React.useRef(false); const isMounted = React.useRef(false);
const canGoBackwards = React.useRef(!router.query.page);
const queryKey = [ queryName, ...(queryIds || []), { page, filters } ]; const queryKey = [ queryName, ...(queryIds || []), { page, filters } ];
...@@ -60,6 +61,8 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -60,6 +61,8 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
} }
const nextPageQuery = { ...router.query }; const nextPageQuery = { ...router.query };
Object.entries(nextPageParams).forEach(([ key, val ]) => nextPageQuery[key] = val.toString()); Object.entries(nextPageParams).forEach(([ key, val ]) => nextPageQuery[key] = val.toString());
nextPageQuery.page = String(page + 1);
router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true }) router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true })
.then(() => { .then(() => {
animateScroll.scrollToTop({ duration: 0 }); animateScroll.scrollToTop({ duration: 0 });
...@@ -70,13 +73,14 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -70,13 +73,14 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
const onPrevPageClick = useCallback(() => { const onPrevPageClick = useCallback(() => {
// returning to the first page // returning to the first page
// we dont have pagination params for the first page // we dont have pagination params for the first page
let nextPageQuery: typeof router.query; let nextPageQuery: typeof router.query = {};
if (page === 2) { if (page === 2) {
nextPageQuery = omit(router.query, paginationFields); nextPageQuery = omit(router.query, paginationFields, 'page');
canGoBackwards.current = true;
} else { } else {
const nextPageParams = { ...pageParams[page - 2] }; const nextPageParams = { ...pageParams[page - 2] };
nextPageQuery = { ...router.query };
nextPageParams && Object.entries(nextPageParams).forEach(([ key, val ]) => nextPageQuery[key] = val.toString()); nextPageParams && Object.entries(nextPageParams).forEach(([ key, val ]) => nextPageQuery[key] = val.toString());
nextPageQuery.page = String(page - 1);
} }
router.query = nextPageQuery; router.query = nextPageQuery;
router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true }) router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true })
...@@ -89,10 +93,11 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -89,10 +93,11 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
const resetPage = useCallback(() => { const resetPage = useCallback(() => {
queryClient.clear(); queryClient.clear();
router.push({ pathname: router.pathname, query: omit(router.query, paginationFields) }, undefined, { shallow: true }).then(() => { router.push({ pathname: router.pathname, query: omit(router.query, paginationFields, 'page') }, undefined, { shallow: true }).then(() => {
animateScroll.scrollToTop({ duration: 0 }); animateScroll.scrollToTop({ duration: 0 });
setPage(1); setPage(1);
setPageParams([ ]); setPageParams([ ]);
canGoBackwards.current = true;
}); });
}, [ queryClient, router, paginationFields ]); }, [ queryClient, router, paginationFields ]);
...@@ -106,6 +111,7 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -106,6 +111,7 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
hasPaginationParams, hasPaginationParams,
resetPage, resetPage,
hasNextPage: nextPageParams ? Object.keys(nextPageParams).length > 0 : false, hasNextPage: nextPageParams ? Object.keys(nextPageParams).length > 0 : false,
canGoBackwards: canGoBackwards.current,
}; };
React.useEffect(() => { React.useEffect(() => {
...@@ -118,7 +124,9 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -118,7 +124,9 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
}, [ queryName ]); }, [ queryName ]);
React.useEffect(() => { React.useEffect(() => {
isMounted.current = true; window.setTimeout(() => {
isMounted.current = true;
}, 0);
}, []); }, []);
return { ...queryResult, pagination }; return { ...queryResult, pagination };
......
...@@ -11,9 +11,10 @@ export type Props = { ...@@ -11,9 +11,10 @@ export type Props = {
hasNextPage: boolean; hasNextPage: boolean;
hasPaginationParams?: boolean; hasPaginationParams?: boolean;
className?: string; className?: string;
canGoBackwards: boolean;
} }
const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNextPage, hasPaginationParams, className }: Props) => { const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNextPage, hasPaginationParams, className, canGoBackwards }: Props) => {
return ( return (
<Flex <Flex
...@@ -37,8 +38,8 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNext ...@@ -37,8 +38,8 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNext
aria-label="Next page" aria-label="Next page"
w="36px" w="36px"
icon={ <Icon as={ arrowIcon } w={ 5 } h={ 5 }/> } icon={ <Icon as={ arrowIcon } w={ 5 } h={ 5 }/> }
mr={ 6 } mr={ 3 }
disabled={ page === 1 } disabled={ !canGoBackwards || page === 1 }
/> />
<Button <Button
variant="outline" variant="outline"
...@@ -59,7 +60,7 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNext ...@@ -59,7 +60,7 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNext
aria-label="Next page" aria-label="Next page"
w="36px" w="36px"
icon={ <Icon as={ arrowIcon } w={ 5 } h={ 5 } transform="rotate(180deg)"/> } icon={ <Icon as={ arrowIcon } w={ 5 } h={ 5 } transform="rotate(180deg)"/> }
ml={ 6 } ml={ 3 }
disabled={ !hasNextPage } disabled={ !hasNextPage }
/> />
{ /* not implemented yet */ } { /* not implemented yet */ }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment