Skip to content

Commit

Permalink
fix(page): pagination of data
Browse files Browse the repository at this point in the history
  • Loading branch information
Daryl-L committed Oct 7, 2023
1 parent becc58f commit 29ab363
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/pages/Script/ScriptsComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import TransactionCellScript from '../Transaction/TransactionCellScript'
import { shannonToCkb, toCamelcase } from '../../utils/util'
import { localeNumberString } from '../../utils/number'
import DecimalCapacity from '../../components/DecimalCapacity'
import { CellInScript, CkbTransactionInScript } from './types'
import { CellInScript, CkbTransactionInScript, PageInfo } from './types'
import styles from './styles.module.scss'
import { QueryResult } from '../../components/QueryResult'
import AddressText from '../../components/AddressText'
Expand All @@ -29,7 +29,7 @@ export const ScriptTransactions = ({ page, size }: { page: number; size: number
const { codeHash, hashType } = useParams<{ codeHash: string; hashType: string }>()

const transactionsQuery = useQuery(['scripts_ckb_transactions', codeHash, hashType, page, size], async () => {
const { data, meta } = await v2AxiosIns
const { data } = await v2AxiosIns
.get(`scripts/ckb_transactions`, {
params: {
code_hash: codeHash,
Expand All @@ -39,14 +39,14 @@ export const ScriptTransactions = ({ page, size }: { page: number; size: number
},
})
.then((res: AxiosResponse) =>
toCamelcase<Response.Response<{ ckbTransactions: CkbTransactionInScript[] }>>(res.data),
toCamelcase<Response.Response<{ ckbTransactions: CkbTransactionInScript[]; meta: PageInfo }>>(res.data),
)

if (data == null || data.ckbTransactions == null || data.ckbTransactions.length === 0) {
throw new Error('Transactions empty')
}
return {
total: meta?.total ?? 0,
total: data.meta.total,
ckbTransactions: data.ckbTransactions,
}
})
Expand Down Expand Up @@ -125,7 +125,7 @@ export const ScriptCells = ({
const { codeHash, hashType } = useParams<{ codeHash: string; hashType: string }>()

const cellsQuery = useQuery([`scripts_${cellType}`, codeHash, hashType, page, size], async () => {
const { data, meta } = await v2AxiosIns
const { data } = await v2AxiosIns
.get(`scripts/${cellType}`, {
params: {
code_hash: codeHash,
Expand All @@ -135,7 +135,9 @@ export const ScriptCells = ({
},
})
.then((res: AxiosResponse) =>
toCamelcase<Response.Response<{ deployedCells?: CellInScript[]; referringCells?: CellInScript[] }>>(res.data),
toCamelcase<
Response.Response<{ deployedCells?: CellInScript[]; referringCells?: CellInScript[]; meta: PageInfo }>
>(res.data),
)
const camelCellType = camelcase(cellType) as 'deployedCells' | 'referringCells'
if (data == null) {
Expand All @@ -146,7 +148,7 @@ export const ScriptCells = ({
throw new Error('Cells empty')
}
return {
total: meta?.total ?? 0,
total: data.meta.total ?? 0,
cells,
}
})
Expand Down
5 changes: 5 additions & 0 deletions src/pages/Script/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export interface ScriptInfo {

export type ScriptTabType = 'transactions' | 'deployed_cells' | 'referring_cells' | undefined

export type PageInfo = {
total: number
pageSize: number
}

export interface CkbTransactionInScript {
id: number
txHash: string
Expand Down

0 comments on commit 29ab363

Please sign in to comment.