Commit f2d6d338 authored by tom's avatar tom

api for block txs

parent f1122f41
import type { NextApiRequest } from 'next';
import handler from 'lib/api/handler';
const getUrl = (req: NextApiRequest) => {
return `/v2/blocks/${ req.query.id }/transactions`;
};
const requestHandler = handler(getUrl, [ 'GET' ]);
export default requestHandler;
import type { AddressParam } from 'types/api/addressParams'; import type { AddressParam } from 'types/api/addressParams';
import type { Reward } from 'types/api/reward'; import type { Reward } from 'types/api/reward';
import type { Transaction } from 'types/api/transaction';
export type BlockType = 'block' | 'reorg' | 'uncle'; export type BlockType = 'block' | 'reorg' | 'uncle';
...@@ -37,3 +38,12 @@ export interface BlocksResponse { ...@@ -37,3 +38,12 @@ export interface BlocksResponse {
items_count: number; items_count: number;
}; };
} }
export interface BlockTransactionsResponse {
items: Array<Transaction>;
next_page_params: {
block_number: number;
index: number;
items_count: number;
} | null;
}
...@@ -7,4 +7,5 @@ export enum QueryKeys { ...@@ -7,4 +7,5 @@ export enum QueryKeys {
txInternals = 'tx-internals', txInternals = 'tx-internals',
txLog = 'tx-log', txLog = 'tx-log',
txRawTrace = 'tx-raw-trace', txRawTrace = 'tx-raw-trace',
blockTxs = 'block-transactions',
} }
...@@ -109,7 +109,7 @@ const BlockDetails = () => { ...@@ -109,7 +109,7 @@ const BlockDetails = () => {
title="Transactions" title="Transactions"
hint="The number of transactions in the block." hint="The number of transactions in the block."
> >
<Link href={ link('block', { id: router.query.id }, { tab: 'transactions' }) }> <Link href={ link('block', { id: router.query.id }, { tab: 'txs' }) }>
{ data.tx_count } transactions { data.tx_count } transactions
</Link> </Link>
</DetailsInfoItem> </DetailsInfoItem>
......
import { useRouter } from 'next/router';
import React from 'react'; import React from 'react';
import TxsWithSort from 'ui/txs/TxsWithSort'; import { QueryKeys } from 'types/client/queries';
import TxsContent from 'ui/txs/TxsContent';
const BlockTxs = () => { const BlockTxs = () => {
const router = useRouter();
return ( return (
// <TxsContent <TxsContent
// showDescription={ false } queryName={ QueryKeys.blockTxs }
// showSortButton={ false } apiPath={ `/api/blocks/${ router.query.id }/transactions` }
// txs={ [] } />
// page={ 1 }
// // eslint-disable-next-line react/jsx-no-bind
// onNextPageClick={ () => {} }
// // eslint-disable-next-line react/jsx-no-bind
// onPrevPageClick={ () => {} }
// />
// eslint-disable-next-line react/jsx-no-bind
<TxsWithSort txs={ [] } sort={ () => () => {} }/>
); );
}; };
......
import { Alert, Box, HStack, Show, Button } from '@chakra-ui/react'; import { Alert, Box, HStack, Show, Button } from '@chakra-ui/react';
import React, { useState, useCallback } from 'react'; import React, { useState, useCallback } from 'react';
import type { QueryKeys } from 'types/client/queries';
import type { Sort } from 'types/client/txs-sort'; import type { Sort } from 'types/client/txs-sort';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
...@@ -16,15 +17,17 @@ import TxsWithSort from './TxsWithSort'; ...@@ -16,15 +17,17 @@ import TxsWithSort from './TxsWithSort';
import useQueryWithPages from './useQueryWithPages'; import useQueryWithPages from './useQueryWithPages';
type Props = { type Props = {
queryName: string; queryName: QueryKeys;
showDescription?: boolean; showDescription?: boolean;
stateFilter: 'validated' | 'pending'; stateFilter?: 'validated' | 'pending';
apiPath: string;
} }
const TxsContent = ({ const TxsContent = ({
showDescription, showDescription,
queryName, queryName,
stateFilter, stateFilter,
apiPath,
}: Props) => { }: Props) => {
const [ sorting, setSorting ] = useState<Sort>(); const [ sorting, setSorting ] = useState<Sort>();
...@@ -62,7 +65,7 @@ const TxsContent = ({ ...@@ -62,7 +65,7 @@ const TxsContent = ({
onNextPageClick, onNextPageClick,
hasPagination, hasPagination,
resetPage, resetPage,
} = useQueryWithPages(queryName, stateFilter); } = useQueryWithPages(queryName, stateFilter, apiPath);
const isMobile = useIsMobile(false); const isMobile = useIsMobile(false);
......
...@@ -14,6 +14,7 @@ const TxsTab = ({ tab }: Props) => { ...@@ -14,6 +14,7 @@ const TxsTab = ({ tab }: Props) => {
queryName={ tab === 'validated' ? QueryKeys.transactionsValidated : QueryKeys.transactionsPending } queryName={ tab === 'validated' ? QueryKeys.transactionsValidated : QueryKeys.transactionsPending }
showDescription={ tab === 'validated' } showDescription={ tab === 'validated' }
stateFilter={ tab } stateFilter={ tab }
apiPath="/api/transactions"
/> />
); );
}; };
......
...@@ -10,7 +10,7 @@ import useFetch from 'lib/hooks/useFetch'; ...@@ -10,7 +10,7 @@ import useFetch from 'lib/hooks/useFetch';
const PAGINATION_FIELDS = [ 'block_number', 'index', 'items_count' ]; const PAGINATION_FIELDS = [ 'block_number', 'index', 'items_count' ];
export default function useQueryWithPages(queryName: string, filter: string) { export default function useQueryWithPages(queryName: string, filter: string | undefined, apiPath: string) {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const router = useRouter(); const router = useRouter();
const [ page, setPage ] = React.useState(1); const [ page, setPage ] = React.useState(1);
...@@ -23,9 +23,12 @@ export default function useQueryWithPages(queryName: string, filter: string) { ...@@ -23,9 +23,12 @@ export default function useQueryWithPages(queryName: string, filter: string) {
async() => { async() => {
const params: Array<string> = []; const params: Array<string> = [];
Object.entries(currPageParams).forEach(([ key, val ]) => params.push(`${ key }=${ val }`)); Object.entries({
...currPageParams,
filter,
}).forEach(([ key, val ]) => val && params.push(`${ key }=${ val }`));
return fetch(`/api/transactions?filter=${ filter }${ params.length ? '&' + params.join('&') : '' }`); return fetch(`${ apiPath }${ params.length ? '?' + params.join('&') : '' }`);
}, },
{ staleTime: Infinity }, { staleTime: Infinity },
); );
......
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