Add server side pagination support

This commit is contained in:
Willian Mitsuda 2021-08-03 01:34:14 -03:00
parent a6b1551756
commit d7621eec59
2 changed files with 11 additions and 14 deletions

View File

@ -36,26 +36,19 @@ const BlockTransactions: React.FC = () => {
const [totalTxs, txs] = useBlockTransactions( const [totalTxs, txs] = useBlockTransactions(
provider, provider,
blockNumber.toNumber() blockNumber.toNumber(),
pageNumber - 1,
PAGE_SIZE
); );
const page = useMemo(() => {
if (!txs) {
return undefined;
}
const pageStart = (pageNumber - 1) * PAGE_SIZE;
return txs.slice(pageStart, pageStart + PAGE_SIZE);
}, [txs, pageNumber]);
const total = totalTxs;
document.title = `Block #${blockNumber} Txns | Otterscan`; document.title = `Block #${blockNumber} Txns | Otterscan`;
return ( return (
<StandardFrame> <StandardFrame>
<BlockTransactionHeader blockTag={blockNumber.toNumber()} /> <BlockTransactionHeader blockTag={blockNumber.toNumber()} />
<BlockTransactionResults <BlockTransactionResults
page={page} page={txs}
total={total ?? 0} total={totalTxs ?? 0}
pageNumber={pageNumber} pageNumber={pageNumber}
/> />
</StandardFrame> </StandardFrame>

View File

@ -66,7 +66,9 @@ export const readBlock = async (
export const useBlockTransactions = ( export const useBlockTransactions = (
provider: ethers.providers.JsonRpcProvider | undefined, provider: ethers.providers.JsonRpcProvider | undefined,
blockNumber: number blockNumber: number,
pageNumber: number,
pageSize: number
): [number | undefined, ProcessedTransaction[] | undefined] => { ): [number | undefined, ProcessedTransaction[] | undefined] => {
const [totalTxs, setTotalTxs] = useState<number>(); const [totalTxs, setTotalTxs] = useState<number>();
const [txs, setTxs] = useState<ProcessedTransaction[]>(); const [txs, setTxs] = useState<ProcessedTransaction[]>();
@ -79,6 +81,8 @@ export const useBlockTransactions = (
const readBlock = async () => { const readBlock = async () => {
const result = await provider.send("ots_getBlockTransactions", [ const result = await provider.send("ots_getBlockTransactions", [
blockNumber, blockNumber,
pageNumber,
pageSize,
]); ]);
const _block = provider.formatter.blockWithTransactions( const _block = provider.formatter.blockWithTransactions(
result.fullblock result.fullblock
@ -139,7 +143,7 @@ export const useBlockTransactions = (
setTxs(processedTxs); setTxs(processedTxs);
}; };
readBlock(); readBlock();
}, [provider, blockNumber]); }, [provider, blockNumber, pageNumber, pageSize]);
return [totalTxs, txs]; return [totalTxs, txs];
}; };