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,56 +47,64 @@ const SearchResultsPageContent = () => { ...@@ -48,56 +47,64 @@ 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> return (
<span> matching results for </span> <>
<chakra.span fontWeight={ 700 }>{ debouncedSearchTerm }</chakra.span> <Show below="lg" ssr={ false }>
</Box> { data.items.map((item, index) => <SearchResultListItem key={ index } data={ item } searchTerm={ debouncedSearchTerm }/>) }
</Show>
<Hide below="lg" ssr={ false }>
<Table variant="simple" size="md" fontWeight={ 500 }>
<Thead top={ isPaginationVisible ? 80 : 0 }>
<Tr>
<Th width="50%">Search Result</Th>
<Th width="50%"/>
<Th width="150px">Category</Th>
</Tr>
</Thead>
<Tbody>
{ data.items.map((item, index) => <SearchResultTableItem key={ index } data={ item } searchTerm={ debouncedSearchTerm }/>) }
</Tbody>
</Table>
</Hide>
</>
); );
})();
const bar = (() => {
if (isError) {
return null;
}
const bar = (() => { const text = isLoading ? (
if (!isPaginationVisible) { <Skeleton h={ 6 } w="280px" borderRadius="full" mb={ isPaginationVisible ? 0 : 6 }/>
return text; ) : (
} (
<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>
)
);
return ( if (!isPaginationVisible) {
<> return text;
<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 ( return (
<> <>
{ bar } <Box display={{ base: 'block', lg: 'none' }}>{ text }</Box>
{ data.items.length > 0 && ( <ActionBar mt={{ base: 0, lg: -6 }} alignItems="center">
<> <Box display={{ base: 'none', lg: 'block' }}>{ text }</Box>
<Show below="lg" ssr={ false }> <Pagination { ...pagination }/>
{ data.items.map((item, index) => <SearchResultListItem key={ index } data={ item } searchTerm={ debouncedSearchTerm }/>) } </ActionBar>
</Show>
<Hide below="lg" ssr={ false }>
<Table variant="simple" size="md" fontWeight={ 500 }>
<Thead top={ isPaginationVisible ? 80 : 0 }>
<Tr>
<Th width="50%">Search Result</Th>
<Th width="50%"/>
<Th width="150px">Category</Th>
</Tr>
</Thead>
<Tbody>
{ data.items.map((item, index) => <SearchResultTableItem key={ index } data={ item } searchTerm={ debouncedSearchTerm }/>) }
</Tbody>
</Table>
</Hide>
</>
) }
</> </>
); );
})(); })();
...@@ -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