Commit 77f42ef9 authored by tom's avatar tom

fix pagination and tests

parent 9a7b089c
...@@ -151,14 +151,12 @@ export default function useQueryWithPages<Resource extends PaginatedResources>({ ...@@ -151,14 +151,12 @@ export default function useQueryWithPages<Resource extends PaginatedResources>({
}); });
}, [ router, resource.paginationFields, resource.filterFields, scrollToTop ]); }, [ router, resource.paginationFields, resource.filterFields, scrollToTop ]);
const hasPaginationParams = Object.keys(currPageParams || {}).length > 0;
const nextPageParams = data?.next_page_params; const nextPageParams = data?.next_page_params;
const pagination = { const pagination = {
page, page,
onNextPageClick, onNextPageClick,
onPrevPageClick, onPrevPageClick,
hasPaginationParams,
resetPage, resetPage,
hasNextPage: nextPageParams ? Object.keys(nextPageParams).length > 0 : false, hasNextPage: nextPageParams ? Object.keys(nextPageParams).length > 0 : false,
canGoBackwards: canGoBackwards.current, canGoBackwards: canGoBackwards.current,
......
...@@ -37,7 +37,6 @@ const SearchResultsPageContent = () => { ...@@ -37,7 +37,6 @@ const SearchResultsPageContent = () => {
if (isLoading) { if (isLoading) {
return ( return (
<Box> <Box>
<Skeleton h={ 6 } w="280px" borderRadius="full" mb={ 6 }/>
<Show below="lg"> <Show below="lg">
<SkeletonList/> <SkeletonList/>
</Show> </Show>
...@@ -48,36 +47,11 @@ const SearchResultsPageContent = () => { ...@@ -48,36 +47,11 @@ const SearchResultsPageContent = () => {
); );
} }
const num = pagination.page > 1 ? 50 : data.items.length; if (data.items.length === 0) {
const text = ( return null;
<Box mb={ isPaginationVisible ? 0 : 6 } lineHeight="32px">
<span>Found </span>
<chakra.span fontWeight={ 700 }>{ num }{ data.next_page_params || pagination.page > 1 ? '+' : '' }</chakra.span>
<span> matching results for </span>
<chakra.span fontWeight={ 700 }>{ debouncedSearchTerm }</chakra.span>
</Box>
);
const bar = (() => {
if (!isPaginationVisible) {
return text;
} }
return ( return (
<>
<Box display={{ base: 'block', lg: 'none' }}>{ text }</Box>
<ActionBar mt={{ base: 0, lg: -6 }}>
<Box display={{ base: 'none', lg: 'block' }}>{ text }</Box>
<Pagination { ...pagination }/>
</ActionBar>
</>
);
})();
return (
<>
{ bar }
{ data.items.length > 0 && (
<> <>
<Show below="lg" ssr={ false }> <Show below="lg" ssr={ false }>
{ data.items.map((item, index) => <SearchResultListItem key={ index } data={ item } searchTerm={ debouncedSearchTerm }/>) } { data.items.map((item, index) => <SearchResultListItem key={ index } data={ item } searchTerm={ debouncedSearchTerm }/>) }
...@@ -97,7 +71,40 @@ const SearchResultsPageContent = () => { ...@@ -97,7 +71,40 @@ const SearchResultsPageContent = () => {
</Table> </Table>
</Hide> </Hide>
</> </>
) } );
})();
const bar = (() => {
if (isError) {
return null;
}
const text = isLoading ? (
<Skeleton h={ 6 } w="280px" borderRadius="full" mb={ isPaginationVisible ? 0 : 6 }/>
) : (
(
<Box mb={ isPaginationVisible ? 0 : 6 } lineHeight="32px">
<span>Found </span>
<chakra.span fontWeight={ 700 }>
{ pagination.page > 1 ? 50 : data.items.length }{ data.next_page_params || pagination.page > 1 ? '+' : '' }
</chakra.span>
<span> matching results for </span>
<chakra.span fontWeight={ 700 }>{ debouncedSearchTerm }</chakra.span>
</Box>
)
);
if (!isPaginationVisible) {
return text;
}
return (
<>
<Box display={{ base: 'block', lg: 'none' }}>{ text }</Box>
<ActionBar mt={{ base: 0, lg: -6 }} alignItems="center">
<Box display={{ base: 'none', lg: 'block' }}>{ text }</Box>
<Pagination { ...pagination }/>
</ActionBar>
</> </>
); );
})(); })();
...@@ -126,6 +133,7 @@ const SearchResultsPageContent = () => { ...@@ -126,6 +133,7 @@ const SearchResultsPageContent = () => {
return ( return (
<Page renderHeader={ renderHeader }> <Page renderHeader={ renderHeader }>
<PageTitle text="Search results"/> <PageTitle text="Search results"/>
{ bar }
{ content } { content }
</Page> </Page>
); );
......
...@@ -9,12 +9,11 @@ export type Props = { ...@@ -9,12 +9,11 @@ export type Props = {
onPrevPageClick: () => void; onPrevPageClick: () => void;
resetPage: () => void; resetPage: () => void;
hasNextPage: boolean; hasNextPage: boolean;
hasPaginationParams?: boolean;
className?: string; className?: string;
canGoBackwards: boolean; canGoBackwards: boolean;
} }
const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNextPage, hasPaginationParams, className, canGoBackwards }: Props) => { const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNextPage, className, canGoBackwards }: Props) => {
return ( return (
<Flex <Flex
...@@ -26,7 +25,7 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNext ...@@ -26,7 +25,7 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNext
variant="outline" variant="outline"
size="sm" size="sm"
onClick={ resetPage } onClick={ resetPage }
disabled={ !hasPaginationParams } disabled={ page === 1 }
mr={ 4 } mr={ 4 }
> >
First First
...@@ -49,7 +48,6 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNext ...@@ -49,7 +48,6 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasNext
fontWeight={ 400 } fontWeight={ 400 }
h={ 8 } h={ 8 }
cursor="unset" cursor="unset"
disabled={ hasPaginationParams && page === 1 }
> >
{ page } { page }
</Button> </Button>
......
import { Box, Text } from '@chakra-ui/react'; import { Text } from '@chakra-ui/react';
import type { UseQueryResult } from '@tanstack/react-query'; import type { UseQueryResult } from '@tanstack/react-query';
import React from 'react'; import React from 'react';
...@@ -23,7 +23,7 @@ const SearchBarSuggest = ({ query, searchTerm }: Props) => { ...@@ -23,7 +23,7 @@ const SearchBarSuggest = ({ query, searchTerm }: Props) => {
} }
if (query.isError) { if (query.isError) {
return <Box>Something went wrong. Try refreshing the page or come back later.</Box>; return <Text>Something went wrong. Try refreshing the page or come back later.</Text>;
} }
const num = query.data.next_page_params ? '50+' : query.data.items.length; const num = query.data.next_page_params ? '50+' : query.data.items.length;
......
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