Skip to content

Commit

Permalink
fix: pagination push router
Browse files Browse the repository at this point in the history
  • Loading branch information
Tien Nam Dao committed Mar 2, 2023
1 parent bb446d3 commit 73008ae
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions hooks/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 }
Expand All @@ -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 })
}
}

Expand Down
2 changes: 1 addition & 1 deletion pages/block/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion pages/tx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions views/block/hook/useBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions views/transactions/hook/useTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down

0 comments on commit 73008ae

Please sign in to comment.