Commit e467c1d8 authored by isstuev's avatar isstuev

fixes

parent e26e1557
...@@ -15,7 +15,7 @@ const BlockTxs = () => { ...@@ -15,7 +15,7 @@ const BlockTxs = () => {
// onPrevPageClick={ () => {} } // onPrevPageClick={ () => {} }
// /> // />
// eslint-disable-next-line react/jsx-no-bind // eslint-disable-next-line react/jsx-no-bind
<TxsWithSort txs={ [] } setSorting={ () => {} }/> <TxsWithSort txs={ [] } sort={ () => () => {} }/>
); );
}; };
......
...@@ -54,7 +54,7 @@ const BlocksContent = ({ type }: Props) => { ...@@ -54,7 +54,7 @@ const BlocksContent = ({ type }: Props) => {
<Show above="lg" key="content-desktop"><BlocksTable data={ data.items }/></Show> <Show above="lg" key="content-desktop"><BlocksTable data={ data.items }/></Show>
<Box mx={{ base: 0, lg: 6 }} my={{ base: 6, lg: 3 }}> <Box mx={{ base: 0, lg: 6 }} my={{ base: 6, lg: 3 }}>
{ /* eslint-disable-next-line react/jsx-no-bind */ } { /* eslint-disable-next-line react/jsx-no-bind */ }
<Pagination currentPage={ 1 } onNextPageClick={ () => {} } onPrevPageClick={ () => {} }/> <Pagination currentPage={ 1 } onNextPageClick={ () => {} } onPrevPageClick={ () => {} } hasNextPage/>
</Box> </Box>
</> </>
); );
......
...@@ -8,11 +8,12 @@ type Props = { ...@@ -8,11 +8,12 @@ type Props = {
maxPage?: number; maxPage?: number;
onNextPageClick: () => void; onNextPageClick: () => void;
onPrevPageClick: () => void; onPrevPageClick: () => void;
hasNextPage: boolean;
} }
const MAX_PAGE_DEFAULT = 50; const MAX_PAGE_DEFAULT = 50;
const Pagination = ({ currentPage, maxPage, onNextPageClick, onPrevPageClick }: Props) => { const Pagination = ({ currentPage, maxPage, onNextPageClick, onPrevPageClick, hasNextPage }: Props) => {
const pageNumber = ( const pageNumber = (
<Flex alignItems="center"> <Flex alignItems="center">
<Button <Button
...@@ -27,6 +28,7 @@ const Pagination = ({ currentPage, maxPage, onNextPageClick, onPrevPageClick }: ...@@ -27,6 +28,7 @@ const Pagination = ({ currentPage, maxPage, onNextPageClick, onPrevPageClick }:
> >
{ currentPage } { currentPage }
</Button> </Button>
{ /* max page will be removed */ }
of of
<Button <Button
variant="outline" variant="outline"
...@@ -58,6 +60,7 @@ const Pagination = ({ currentPage, maxPage, onNextPageClick, onPrevPageClick }: ...@@ -58,6 +60,7 @@ const Pagination = ({ currentPage, maxPage, onNextPageClick, onPrevPageClick }:
w="36px" w="36px"
icon={ <Icon as={ arrowIcon } w={ 5 } h={ 5 }/> } icon={ <Icon as={ arrowIcon } w={ 5 } h={ 5 }/> }
mr={ 8 } mr={ 8 }
disabled={ currentPage === 1 }
/> />
{ pageNumber } { pageNumber }
<IconButton <IconButton
...@@ -68,6 +71,7 @@ const Pagination = ({ currentPage, maxPage, onNextPageClick, onPrevPageClick }: ...@@ -68,6 +71,7 @@ const Pagination = ({ currentPage, maxPage, onNextPageClick, onPrevPageClick }:
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={ 8 } ml={ 8 }
disabled={ !hasNextPage }
/> />
</Flex> </Flex>
{ /* not implemented yet */ } { /* not implemented yet */ }
......
...@@ -116,8 +116,15 @@ const TxsContent = ({ ...@@ -116,8 +116,15 @@ const TxsContent = ({
</HStack> </HStack>
{ content } { content }
<Box mx={{ base: 0, lg: 6 }} my={{ base: 6, lg: 3 }}> <Box mx={{ base: 0, lg: 6 }} my={{ base: 6, lg: 3 }}>
{ hasPagination ? { hasPagination ? (
<Pagination currentPage={ page } onNextPageClick={ onNextPageClick } onPrevPageClick={ onPrevPageClick }/> : <Pagination
currentPage={ page }
hasNextPage={ data?.next_page_params !== undefined && Object.keys(data?.next_page_params).length > 0 }
onNextPageClick={ onNextPageClick }
onPrevPageClick={ onPrevPageClick }
/>
) :
// temporary button, waiting for new pagination mockups
<Button onClick={ resetPage }>Reset</Button> <Button onClick={ resetPage }>Reset</Button>
} }
</Box> </Box>
......
import { useQuery } from '@tanstack/react-query'; import { useQuery, useQueryClient } from '@tanstack/react-query';
import { pick, omit } from 'lodash'; import { pick, omit } from 'lodash';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import React, { useCallback } from 'react'; import React, { useCallback } from 'react';
...@@ -11,8 +11,10 @@ import useFetch from 'lib/hooks/useFetch'; ...@@ -11,8 +11,10 @@ import useFetch from 'lib/hooks/useFetch';
const PAGINATION_FIELDS = [ 'block_number', 'index', 'items_count' ]; const PAGINATION_FIELDS = [ 'block_number', 'index', 'items_count' ];
export default function useQueryWithPages(queryName: string, filter: string) { export default function useQueryWithPages(queryName: string, filter: string) {
const queryClient = useQueryClient();
const router = useRouter(); const router = useRouter();
const [ page, setPage ] = React.useState(1); const [ page, setPage ] = React.useState(1);
const currPageParams = pick(router.query, PAGINATION_FIELDS);
const [ pageParams, setPageParams ] = React.useState<Array<Partial<TransactionsResponse['next_page_params']>>>([ {} ]); const [ pageParams, setPageParams ] = React.useState<Array<Partial<TransactionsResponse['next_page_params']>>>([ {} ]);
const fetch = useFetch(); const fetch = useFetch();
...@@ -21,7 +23,7 @@ export default function useQueryWithPages(queryName: string, filter: string) { ...@@ -21,7 +23,7 @@ export default function useQueryWithPages(queryName: string, filter: string) {
async() => { async() => {
const params: Array<string> = []; const params: Array<string> = [];
Object.entries(pageParams[page - 1]).forEach(([ key, val ]) => params.push(`${ key }=${ val }`)); Object.entries(currPageParams).forEach(([ key, val ]) => params.push(`${ key }=${ val }`));
return fetch(`/api/transactions?filter=${ filter }${ params.length ? '&' + params.join('&') : '' }`); return fetch(`/api/transactions?filter=${ filter }${ params.length ? '&' + params.join('&') : '' }`);
}, },
...@@ -29,16 +31,19 @@ export default function useQueryWithPages(queryName: string, filter: string) { ...@@ -29,16 +31,19 @@ export default function useQueryWithPages(queryName: string, filter: string) {
); );
const onNextPageClick = useCallback(() => { const onNextPageClick = useCallback(() => {
if (page >= pageParams.length && data?.next_page_params) { if (!data?.next_page_params) {
// we hide next page button if no next_page_params
return;
}
// api adds filters into next-page-params now // api adds filters into next-page-params now
// later filters will be removed from response // later filters will be removed from response
const nextPageParams = pick(data.next_page_params, PAGINATION_FIELDS); const nextPageParams = pick(data.next_page_params, PAGINATION_FIELDS);
if (page >= pageParams.length && data?.next_page_params) {
setPageParams(prev => [ ...prev, nextPageParams ]); setPageParams(prev => [ ...prev, nextPageParams ]);
}
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());
router.query = nextPageQuery; router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true });
router.push(router);
}
animateScroll.scrollToTop({ duration: 0 }); animateScroll.scrollToTop({ duration: 0 });
setPage(prev => prev + 1); setPage(prev => prev + 1);
}, [ data, page, pageParams, router ]); }, [ data, page, pageParams, router ]);
...@@ -46,29 +51,28 @@ export default function useQueryWithPages(queryName: string, filter: string) { ...@@ -46,29 +51,28 @@ export default function useQueryWithPages(queryName: string, filter: string) {
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;
if (page === 2) { if (page === 2) {
router.query = omit(router.query, PAGINATION_FIELDS); nextPageQuery = omit(router.query, PAGINATION_FIELDS);
} else { } else {
const nextPageParams = { ...pageParams[page - 1] }; const nextPageParams = { ...pageParams[page - 2] };
const nextPageQuery = { ...router.query }; nextPageQuery = { ...router.query };
Object.entries(nextPageParams).forEach(([ key, val ]) => nextPageQuery[key] = val.toString()); Object.entries(nextPageParams).forEach(([ key, val ]) => nextPageQuery[key] = val.toString());
router.query = nextPageQuery;
} }
router.push(router); router.query = nextPageQuery;
router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true });
animateScroll.scrollToTop({ duration: 0 }); animateScroll.scrollToTop({ duration: 0 });
setPage(prev => prev - 1); setPage(prev => prev - 1);
}, [ router, page, pageParams ]); }, [ router, page, pageParams ]);
const resetPage = useCallback(() => { const resetPage = useCallback(() => {
router.query = omit(router.query, PAGINATION_FIELDS); queryClient.clear();
router.push(router);
animateScroll.scrollToTop({ duration: 0 }); animateScroll.scrollToTop({ duration: 0 });
setPageParams([ {} ]); router.push({ pathname: router.pathname, query: omit(router.query, PAGINATION_FIELDS) }, undefined, { shallow: true });
setPage(1); }, [ router, queryClient ]);
}, [ router ]);
// if there are pagination params on the initial page, we shouldn't show pagination // if there are pagination params on the initial page, we shouldn't show pagination
const hasPagination = !(page === 1 && Object.keys(pick(router.query, PAGINATION_FIELDS)).length > 0); const hasPagination = !(page === 1 && Object.keys(currPageParams).length > 0);
return { data, isError, isLoading, page, onNextPageClick, onPrevPageClick, hasPagination, resetPage }; return { data, isError, isLoading, page, onNextPageClick, onPrevPageClick, hasPagination, resetPage };
} }
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