Commit 58ec543a authored by tom's avatar tom

fix pagination

parent 729dacb3
...@@ -36,6 +36,7 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -36,6 +36,7 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
const fetch = useFetch(); const fetch = useFetch();
const isMounted = React.useRef(false); const isMounted = React.useRef(false);
const canGoBackwards = React.useRef(!router.query.page); const canGoBackwards = React.useRef(!router.query.page);
const [ hasPagination, setHasPagination ] = React.useState(page > 1);
const queryKey = [ queryName, ...(queryIds || []), { page, filters } ]; const queryKey = [ queryName, ...(queryIds || []), { page, filters } ];
...@@ -74,6 +75,7 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -74,6 +75,7 @@ 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); nextPageQuery.page = String(page + 1);
setHasPagination(true);
router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true }) router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true })
.then(() => { .then(() => {
...@@ -101,6 +103,7 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -101,6 +103,7 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
setPage(prev => prev - 1); setPage(prev => prev - 1);
page === 2 && queryClient.removeQueries({ queryKey: [ queryName ] }); page === 2 && queryClient.removeQueries({ queryKey: [ queryName ] });
}); });
setHasPagination(true);
}, [ router, page, paginationFields, pageParams, queryClient, scrollToTop, queryName ]); }, [ router, page, paginationFields, pageParams, queryClient, scrollToTop, queryName ]);
const resetPage = useCallback(() => { const resetPage = useCallback(() => {
...@@ -118,6 +121,8 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -118,6 +121,8 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
queryClient.removeQueries({ queryKey: [ queryName ], type: 'inactive' }); queryClient.removeQueries({ queryKey: [ queryName ], type: 'inactive' });
}, 100); }, 100);
}); });
setHasPagination(true);
}, [ queryClient, queryName, router, paginationFields, scrollToTop ]); }, [ queryClient, queryName, router, paginationFields, scrollToTop ]);
const onFilterChange = useCallback((newFilters: PaginationFilters<QueryName> | undefined) => { const onFilterChange = useCallback((newFilters: PaginationFilters<QueryName> | undefined) => {
...@@ -156,6 +161,8 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -156,6 +161,8 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
canGoBackwards: canGoBackwards.current, canGoBackwards: canGoBackwards.current,
}; };
const isPaginationVisible = hasPagination || (!queryResult.isLoading && !queryResult.isError && pagination.hasNextPage);
React.useEffect(() => { React.useEffect(() => {
if (page !== 1 && isMounted.current) { if (page !== 1 && isMounted.current) {
queryClient.cancelQueries({ queryKey }); queryClient.cancelQueries({ queryKey });
...@@ -171,5 +178,5 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>( ...@@ -171,5 +178,5 @@ export default function useQueryWithPages<QueryName extends PaginatedQueryKeys>(
}, 0); }, 0);
}, []); }, []);
return { ...queryResult, pagination, onFilterChange }; return { ...queryResult, pagination, onFilterChange, isPaginationVisible };
} }
...@@ -122,11 +122,9 @@ const AddressBlocksValidated = ({ addressQuery }: Props) => { ...@@ -122,11 +122,9 @@ const AddressBlocksValidated = ({ addressQuery }: Props) => {
); );
})(); })();
const isPaginatorHidden = !query.isLoading && !query.isError && query.pagination.page === 1 && !query.pagination.hasNextPage;
return ( return (
<Box> <Box>
{ !isPaginatorHidden && ( { query.isPaginationVisible && (
<ActionBar mt={ -6 }> <ActionBar mt={ -6 }>
<Pagination ml="auto" { ...query.pagination }/> <Pagination ml="auto" { ...query.pagination }/>
</ActionBar> </ActionBar>
......
...@@ -35,7 +35,7 @@ const AddressInternalTxs = () => { ...@@ -35,7 +35,7 @@ const AddressInternalTxs = () => {
const queryIdArray = castArray(queryId); const queryIdArray = castArray(queryId);
const queryIdStr = queryIdArray[0]; const queryIdStr = queryIdArray[0];
const { data, isLoading, isError, pagination, onFilterChange } = useQueryWithPages({ const { data, isLoading, isError, pagination, onFilterChange, isPaginationVisible } = useQueryWithPages({
apiPath: `/node-api/addresses/${ queryId }/internal-transactions`, apiPath: `/node-api/addresses/${ queryId }/internal-transactions`,
queryName: QueryKeys.addressInternalTxs, queryName: QueryKeys.addressInternalTxs,
queryIds: queryIdArray, queryIds: queryIdArray,
...@@ -43,8 +43,6 @@ const AddressInternalTxs = () => { ...@@ -43,8 +43,6 @@ const AddressInternalTxs = () => {
scroll: { elem: SCROLL_ELEM, offset: SCROLL_OFFSET }, scroll: { elem: SCROLL_ELEM, offset: SCROLL_OFFSET },
}); });
const isPaginatorHidden = !isLoading && !isError && pagination.page === 1 && !pagination.hasNextPage;
const handleFilterChange = React.useCallback((val: string | Array<string>) => { const handleFilterChange = React.useCallback((val: string | Array<string>) => {
const newVal = getFilterValue(val); const newVal = getFilterValue(val);
...@@ -94,7 +92,7 @@ const AddressInternalTxs = () => { ...@@ -94,7 +92,7 @@ const AddressInternalTxs = () => {
onFilterChange={ handleFilterChange } onFilterChange={ handleFilterChange }
isActive={ Boolean(filterValue) } isActive={ Boolean(filterValue) }
/> />
{ !isPaginatorHidden && <Pagination ml="auto" { ...pagination }/> } { isPaginationVisible && <Pagination ml="auto" { ...pagination }/> }
</ActionBar> </ActionBar>
{ content } { content }
</Element> </Element>
......
...@@ -43,12 +43,6 @@ const AddressTxs = () => { ...@@ -43,12 +43,6 @@ const AddressTxs = () => {
addressTxsQuery.onFilterChange({ filter: newVal }); addressTxsQuery.onFilterChange({ filter: newVal });
}, [ addressTxsQuery ]); }, [ addressTxsQuery ]);
const isPaginatorHidden =
!addressTxsQuery.isLoading &&
!addressTxsQuery.isError &&
addressTxsQuery.pagination.page === 1 &&
!addressTxsQuery.pagination.hasNextPage;
const filter = ( const filter = (
<AddressTxsFilter <AddressTxsFilter
defaultFilter={ filterValue } defaultFilter={ filterValue }
...@@ -62,7 +56,7 @@ const AddressTxs = () => { ...@@ -62,7 +56,7 @@ const AddressTxs = () => {
{ !isMobile && ( { !isMobile && (
<ActionBar mt={ -6 }> <ActionBar mt={ -6 }>
{ filter } { filter }
{ !isPaginatorHidden && <Pagination { ...addressTxsQuery.pagination }/> } { addressTxsQuery.isPaginationVisible && <Pagination { ...addressTxsQuery.pagination }/> }
</ActionBar> </ActionBar>
) } ) }
<TxsContent <TxsContent
......
...@@ -19,13 +19,12 @@ import AddressCoinBalanceTableItem from './AddressCoinBalanceTableItem'; ...@@ -19,13 +19,12 @@ import AddressCoinBalanceTableItem from './AddressCoinBalanceTableItem';
interface Props { interface Props {
query: UseQueryResult<AddressCoinBalanceHistoryResponse> & { query: UseQueryResult<AddressCoinBalanceHistoryResponse> & {
pagination: PaginationProps; pagination: PaginationProps;
isPaginationVisible: boolean;
}; };
} }
const AddressCoinBalanceHistory = ({ query }: Props) => { const AddressCoinBalanceHistory = ({ query }: Props) => {
const isPaginatorHidden = !query.isLoading && !query.isError && query.pagination.page === 1 && !query.pagination.hasNextPage;
const content = (() => { const content = (() => {
if (query.isLoading) { if (query.isLoading) {
return ( return (
...@@ -75,7 +74,7 @@ const AddressCoinBalanceHistory = ({ query }: Props) => { ...@@ -75,7 +74,7 @@ const AddressCoinBalanceHistory = ({ query }: Props) => {
return ( return (
<Box mt={ 8 }> <Box mt={ 8 }>
{ !isPaginatorHidden && ( { query.isPaginationVisible && (
<ActionBar mt={ -6 }> <ActionBar mt={ -6 }>
<Pagination ml="auto" { ...query.pagination }/> <Pagination ml="auto" { ...query.pagination }/>
</ActionBar> </ActionBar>
......
...@@ -13,9 +13,10 @@ import Pagination from 'ui/shared/Pagination'; ...@@ -13,9 +13,10 @@ import Pagination from 'ui/shared/Pagination';
interface Props { interface Props {
pagination: PaginationProps; pagination: PaginationProps;
isPaginationVisible: boolean;
} }
const BlocksTabSlot = ({ pagination }: Props) => { const BlocksTabSlot = ({ pagination, isPaginationVisible }: Props) => {
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const fetch = useFetch(); const fetch = useFetch();
...@@ -41,7 +42,7 @@ const BlocksTabSlot = ({ pagination }: Props) => { ...@@ -41,7 +42,7 @@ const BlocksTabSlot = ({ pagination }: Props) => {
</Text> </Text>
</Box> </Box>
) } ) }
<Pagination my={ 1 } { ...pagination }/> { isPaginationVisible && <Pagination my={ 1 } { ...pagination }/> }
</Flex> </Flex>
); );
}; };
......
...@@ -46,8 +46,7 @@ const BlockPageContent = () => { ...@@ -46,8 +46,7 @@ const BlockPageContent = () => {
{ id: 'txs', title: 'Transactions', component: <TxsContent query={ blockTxsQuery } showBlockInfo={ false } showSocketInfo={ false }/> }, { id: 'txs', title: 'Transactions', component: <TxsContent query={ blockTxsQuery } showBlockInfo={ false } showSocketInfo={ false }/> },
]; ];
const isPaginatorHidden = !blockTxsQuery.isLoading && !blockTxsQuery.isError && blockTxsQuery.pagination.page === 1 && !blockTxsQuery.pagination.hasNextPage; const hasPagination = !isMobile && router.query.tab === 'txs' && blockTxsQuery.isPaginationVisible;
const hasPagination = !isMobile && router.query.tab === 'txs' && !isPaginatorHidden;
const referrer = isInBrowser ? window.document.referrer : appProps.referrer; const referrer = isInBrowser ? window.document.referrer : appProps.referrer;
......
...@@ -55,7 +55,7 @@ const BlocksPageContent = () => { ...@@ -55,7 +55,7 @@ const BlocksPageContent = () => {
<RoutedTabs <RoutedTabs
tabs={ tabs } tabs={ tabs }
tabListProps={ isMobile ? undefined : TAB_LIST_PROPS } tabListProps={ isMobile ? undefined : TAB_LIST_PROPS }
rightSlot={ <BlocksTabSlot pagination={ blocksQuery.pagination }/> } rightSlot={ <BlocksTabSlot pagination={ blocksQuery.pagination } isPaginationVisible={ blocksQuery.isPaginationVisible }/> }
stickyEnabled={ !isMobile } stickyEnabled={ !isMobile }
/> />
</Page> </Page>
......
...@@ -43,7 +43,7 @@ const Transactions = () => { ...@@ -43,7 +43,7 @@ const Transactions = () => {
<RoutedTabs <RoutedTabs
tabs={ tabs } tabs={ tabs }
tabListProps={ isMobile ? undefined : TAB_LIST_PROPS } tabListProps={ isMobile ? undefined : TAB_LIST_PROPS }
rightSlot={ <TxsTabSlot pagination={ txsQuery.pagination }/> } rightSlot={ <TxsTabSlot pagination={ txsQuery.pagination } isPaginationVisible={ txsQuery.isPaginationVisible && !isMobile }/> }
stickyEnabled={ !isMobile } stickyEnabled={ !isMobile }
/> />
</Box> </Box>
......
...@@ -51,7 +51,7 @@ const TokenTransfer = ({ isLoading: isLoadingProp, isDisabled, queryName, queryI ...@@ -51,7 +51,7 @@ const TokenTransfer = ({ isLoading: isLoadingProp, isDisabled, queryName, queryI
{ type: getTokenFilterValue(router.query.type), filter: getAddressFilterValue(router.query.filter) }, { type: getTokenFilterValue(router.query.type), filter: getAddressFilterValue(router.query.filter) },
); );
const { isError, isLoading, data, pagination, onFilterChange } = useQueryWithPages({ const { isError, isLoading, data, pagination, onFilterChange, isPaginationVisible } = useQueryWithPages({
apiPath: path, apiPath: path,
queryName, queryName,
queryIds, queryIds,
...@@ -128,7 +128,7 @@ const TokenTransfer = ({ isLoading: isLoadingProp, isDisabled, queryName, queryI ...@@ -128,7 +128,7 @@ const TokenTransfer = ({ isLoading: isLoadingProp, isDisabled, queryName, queryI
onAddressFilterChange={ handleAddressFilterChange } onAddressFilterChange={ handleAddressFilterChange }
defaultAddressFilter={ filters.filter } defaultAddressFilter={ filters.filter }
/> />
<Pagination ml="auto" { ...pagination }/> { isPaginationVisible && <Pagination ml="auto" { ...pagination }/> }
</ActionBar> </ActionBar>
) } ) }
{ content } { content }
......
...@@ -75,7 +75,7 @@ const TxInternals = () => { ...@@ -75,7 +75,7 @@ const TxInternals = () => {
// const [ searchTerm, setSearchTerm ] = React.useState<string>(''); // const [ searchTerm, setSearchTerm ] = React.useState<string>('');
const [ sort, setSort ] = React.useState<Sort>(); const [ sort, setSort ] = React.useState<Sort>();
const txInfo = useFetchTxInfo({ updateDelay: 5 * SECOND }); const txInfo = useFetchTxInfo({ updateDelay: 5 * SECOND });
const { data, isLoading, isError, pagination } = useQueryWithPages({ const { data, isLoading, isError, pagination, isPaginationVisible } = useQueryWithPages({
apiPath: `/node-api/transactions/${ txInfo.data?.hash }/internal-transactions`, apiPath: `/node-api/transactions/${ txInfo.data?.hash }/internal-transactions`,
queryName: QueryKeys.txInternals, queryName: QueryKeys.txInternals,
queryIds: txInfo.data?.hash ? [ txInfo.data.hash ] : undefined, queryIds: txInfo.data?.hash ? [ txInfo.data.hash ] : undefined,
...@@ -83,7 +83,6 @@ const TxInternals = () => { ...@@ -83,7 +83,6 @@ const TxInternals = () => {
enabled: Boolean(txInfo.data?.hash) && Boolean(txInfo.data?.status), enabled: Boolean(txInfo.data?.hash) && Boolean(txInfo.data?.status),
}, },
}); });
const isPaginatorHidden = !isLoading && !isError && pagination.page === 1 && !pagination.hasNextPage;
const isMobile = useIsMobile(); const isMobile = useIsMobile();
...@@ -131,12 +130,12 @@ const TxInternals = () => { ...@@ -131,12 +130,12 @@ const TxInternals = () => {
return isMobile ? return isMobile ?
<TxInternalsList data={ filteredData }/> : <TxInternalsList data={ filteredData }/> :
<TxInternalsTable data={ filteredData } sort={ sort } onSortToggle={ handleSortToggle } top={ isPaginatorHidden ? 0 : 80 }/>; <TxInternalsTable data={ filteredData } sort={ sort } onSortToggle={ handleSortToggle } top={ isPaginationVisible ? 80 : 0 }/>;
})(); })();
return ( return (
<Box> <Box>
{ !isPaginatorHidden && ( { isPaginationVisible && (
<ActionBar mt={ -6 }> <ActionBar mt={ -6 }>
<Pagination ml="auto" { ...pagination }/> <Pagination ml="auto" { ...pagination }/>
</ActionBar> </ActionBar>
......
...@@ -16,7 +16,7 @@ import useFetchTxInfo from 'ui/tx/useFetchTxInfo'; ...@@ -16,7 +16,7 @@ import useFetchTxInfo from 'ui/tx/useFetchTxInfo';
const TxLogs = () => { const TxLogs = () => {
const txInfo = useFetchTxInfo({ updateDelay: 5 * SECOND }); const txInfo = useFetchTxInfo({ updateDelay: 5 * SECOND });
const { data, isLoading, isError, pagination } = useQueryWithPages({ const { data, isLoading, isError, pagination, isPaginationVisible } = useQueryWithPages({
apiPath: `/node-api/transactions/${ txInfo.data?.hash }/logs`, apiPath: `/node-api/transactions/${ txInfo.data?.hash }/logs`,
queryName: QueryKeys.txLogs, queryName: QueryKeys.txLogs,
queryIds: txInfo.data?.hash ? [ txInfo.data.hash ] : undefined, queryIds: txInfo.data?.hash ? [ txInfo.data.hash ] : undefined,
...@@ -24,7 +24,6 @@ const TxLogs = () => { ...@@ -24,7 +24,6 @@ const TxLogs = () => {
enabled: Boolean(txInfo.data?.hash) && Boolean(txInfo.data?.status), enabled: Boolean(txInfo.data?.hash) && Boolean(txInfo.data?.status),
}, },
}); });
const isPaginatorHidden = !isLoading && !isError && pagination.page === 1 && !pagination.hasNextPage;
if (!txInfo.isLoading && !txInfo.isError && !txInfo.data.status) { if (!txInfo.isLoading && !txInfo.isError && !txInfo.data.status) {
return txInfo.socketStatus ? <TxSocketAlert status={ txInfo.socketStatus }/> : <TxPendingAlert/>; return txInfo.socketStatus ? <TxSocketAlert status={ txInfo.socketStatus }/> : <TxPendingAlert/>;
...@@ -49,7 +48,7 @@ const TxLogs = () => { ...@@ -49,7 +48,7 @@ const TxLogs = () => {
return ( return (
<Box> <Box>
{ !isPaginatorHidden && ( { isPaginationVisible && (
<ActionBar mt={ -6 }> <ActionBar mt={ -6 }>
<Pagination ml="auto" { ...pagination }/> <Pagination ml="auto" { ...pagination }/>
</ActionBar> </ActionBar>
......
import React from 'react'; import React from 'react';
import useIsMobile from 'lib/hooks/useIsMobile';
import type { Props as PaginationProps } from 'ui/shared/Pagination'; import type { Props as PaginationProps } from 'ui/shared/Pagination';
import Pagination from 'ui/shared/Pagination'; import Pagination from 'ui/shared/Pagination';
interface Props { interface Props {
pagination: PaginationProps; pagination: PaginationProps;
isPaginationVisible: boolean;
} }
const TxsTabSlot = ({ pagination }: Props) => { const TxsTabSlot = ({ pagination, isPaginationVisible }: Props) => {
const isMobile = useIsMobile(); if (!isPaginationVisible) {
if (isMobile) {
return null; return null;
} }
......
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