From 73008aecbecf2f7a2592a3145f8994438539e787 Mon Sep 17 00:00:00 2001 From: Tien Nam Dao <tien.dao2@tiki.vn> Date: Thu, 2 Mar 2023 09:32:23 +0700 Subject: [PATCH] fix: pagination push router --- hooks/usePagination.ts | 5 +++-- pages/block/index.tsx | 2 +- pages/tx/index.tsx | 2 +- views/block/hook/useBlock.ts | 4 ++-- views/transactions/hook/useTransaction.ts | 4 ++-- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/hooks/usePagination.ts b/hooks/usePagination.ts index e6d1e0ba..5f5f3e97 100644 --- a/hooks/usePagination.ts +++ b/hooks/usePagination.ts @@ -22,6 +22,7 @@ export default function usePagination(rootPath: string) { pagination: 'offset', order: 'height.desc' }) + const setPagination = ({ page, total }: CustomRouter) => { let data: CustomRouter = { page, total } data = pickBy<CustomRouter>(data, item => item !== undefined) @@ -33,7 +34,7 @@ export default function usePagination(rootPath: string) { // page >= 1 will make window page re-render a time to apply query params if (page !== undefined && page > 1 && router.asPath !== '/') { - router.push( + router.replace( { pathname: rootPath, query: { ..._pagination, page } @@ -42,7 +43,7 @@ export default function usePagination(rootPath: string) { { shallow: true } ) } else if (page == 1) { - if (router.asPath !== rootPath) router.push({ pathname: rootPath }, undefined, { shallow: true }) + if (router.asPath !== rootPath) router.replace({ pathname: rootPath }, undefined, { shallow: true }) } } diff --git a/pages/block/index.tsx b/pages/block/index.tsx index 4d77e67d..e85c0992 100644 --- a/pages/block/index.tsx +++ b/pages/block/index.tsx @@ -15,7 +15,7 @@ import Layout from '../../components/Layout' const BlockDetailPage: React.FC<NextPage> = _ => { const [loaderTime, setLoaderTime] = useState(false) - const { fullPageData, getPropserAddress, pagination, changePage } = useBlock() + const { fullPageData, getPropserAddress, pagination, changePage } = useBlock('/block') const { isMobile } = useMobileLayout() //loader display at least 1 second useEffect(() => { diff --git a/pages/tx/index.tsx b/pages/tx/index.tsx index 469294e0..34cab4d9 100644 --- a/pages/tx/index.tsx +++ b/pages/tx/index.tsx @@ -14,7 +14,7 @@ import Layout from '../../components/Layout' const BlockDetailPage: React.FC<NextPage> = _ => { const [loaderTime, setLoaderTime] = useState(false) - const { fullPageData, pagination, changePage } = useTransaction() + const { fullPageData, pagination, changePage } = useTransaction('/tx') const { isMobile } = useMobileLayout(1220) //loader display at least 1 second diff --git a/views/block/hook/useBlock.ts b/views/block/hook/useBlock.ts index c6867acb..7de96f05 100644 --- a/views/block/hook/useBlock.ts +++ b/views/block/hook/useBlock.ts @@ -6,11 +6,11 @@ import { setLatestBlock } from 'slices/commonSlice' import { useAppDispatch } from 'store/hooks' import useSWR from 'swr' -export default function useBlock() { +export default function useBlock(path: string = '/') { const dispatch = useAppDispatch() const [_items, _setBlockItem] = useState<BlockItem[]>() - const { pagination, setPagination } = usePagination('/block') + const { pagination, setPagination } = usePagination(path) const _fetchCondition = () => { return [ API_LIST.ALL_BLOCKS, diff --git a/views/transactions/hook/useTransaction.ts b/views/transactions/hook/useTransaction.ts index 3b5e0d9a..ec428033 100644 --- a/views/transactions/hook/useTransaction.ts +++ b/views/transactions/hook/useTransaction.ts @@ -13,9 +13,9 @@ import { getFromToTxFromCosmosEntry } from '../utils' -export default function useTransaction() { +export default function useTransaction(path: string = '/') { const [_items, _setTransactionItem] = useState<TransactionItemModified[]>() - const { pagination, setPagination } = usePagination('/tx') + const { pagination, setPagination } = usePagination(path) const _fetchCondition = () => { return [