fix(explorer): update tx navigation for 0.74.0 (#5662)

This commit is contained in:
Edd 2024-01-24 16:31:56 +00:00 committed by GitHub
parent 261f32aa5b
commit 27a9d5f247
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 25 additions and 65 deletions

View File

@ -61,53 +61,4 @@ describe('TxsListNavigation', () => {
expect(nextPageMock).toHaveBeenCalledTimes(1); expect(nextPageMock).toHaveBeenCalledTimes(1);
}); });
it('disables "Older" button if hasMoreTxs is false', () => {
render(
<TxsListNavigation
refreshTxs={NOOP}
nextPage={NOOP}
previousPage={NOOP}
hasMoreTxs={false}
hasPreviousPage={false}
>
<span></span>
</TxsListNavigation>
);
expect(screen.getByText('Older')).toBeDisabled();
});
it('disables "Newer" button if hasPreviousPage is false', () => {
render(
<TxsListNavigation
refreshTxs={NOOP}
nextPage={NOOP}
previousPage={NOOP}
hasMoreTxs={true}
hasPreviousPage={false}
>
<span></span>
</TxsListNavigation>
);
expect(screen.getByText('Newer')).toBeDisabled();
});
it('disables both buttons when more and previous are false', () => {
render(
<TxsListNavigation
refreshTxs={NOOP}
nextPage={NOOP}
previousPage={NOOP}
hasMoreTxs={false}
hasPreviousPage={false}
>
<span></span>
</TxsListNavigation>
);
expect(screen.getByText('Newer')).toBeDisabled();
expect(screen.getByText('Older')).toBeDisabled();
});
}); });

View File

@ -10,7 +10,8 @@ export interface TxListNavigationProps {
loading?: boolean; loading?: boolean;
hasPreviousPage: boolean; hasPreviousPage: boolean;
hasMoreTxs: boolean; hasMoreTxs: boolean;
children: React.ReactNode; children?: React.ReactNode;
isEmpty?: boolean;
} }
/** /**
* Displays a list of transactions with filters and controls to navigate through the list. * Displays a list of transactions with filters and controls to navigate through the list.
@ -21,9 +22,8 @@ export const TxsListNavigation = ({
refreshTxs, refreshTxs,
nextPage, nextPage,
previousPage, previousPage,
hasMoreTxs,
hasPreviousPage,
children, children,
isEmpty,
loading = false, loading = false,
}: TxListNavigationProps) => { }: TxListNavigationProps) => {
return ( return (
@ -35,7 +35,6 @@ export const TxsListNavigation = ({
<Button <Button
className="mr-2" className="mr-2"
size="xs" size="xs"
disabled={!hasPreviousPage || loading}
onClick={() => { onClick={() => {
previousPage(); previousPage();
}} }}
@ -44,7 +43,7 @@ export const TxsListNavigation = ({
</Button> </Button>
<Button <Button
size="xs" size="xs"
disabled={!hasMoreTxs} disabled={isEmpty}
onClick={() => { onClick={() => {
nextPage(); nextPage();
}} }}

View File

@ -43,7 +43,7 @@ export const getTxsDataUrl = (params: IGetTxsDataUrl) => {
url.searchParams.append('first', count); url.searchParams.append('first', count);
url.searchParams.append('after', params.after); url.searchParams.append('after', params.after);
} else { } else {
url.searchParams.append('last', count); url.searchParams.append('first', count);
} }
// Hacky fix for param as array // Hacky fix for param as array

View File

@ -6,7 +6,7 @@ describe('getTxsDataUrl', () => {
count: 10, count: 10,
baseUrl: 'https://example.com/transactions', baseUrl: 'https://example.com/transactions',
}; };
const expectedUrl = 'https://example.com/transactions?last=10'; const expectedUrl = 'https://example.com/transactions?first=10';
expect(getTxsDataUrl(params)).toEqual(expectedUrl); expect(getTxsDataUrl(params)).toEqual(expectedUrl);
}); });
@ -41,7 +41,7 @@ describe('getTxsDataUrl', () => {
baseUrl: 'https://example.com/transactions', baseUrl: 'https://example.com/transactions',
}; };
const expectedUrl = const expectedUrl =
'https://example.com/transactions?last=10&filters[cmd.type]=Made%20Up%20Transaction&filters[tx.submitter]=1234'; 'https://example.com/transactions?first=10&filters[cmd.type]=Made%20Up%20Transaction&filters[tx.submitter]=1234';
expect(getTxsDataUrl(params)).toEqual(expectedUrl); expect(getTxsDataUrl(params)).toEqual(expectedUrl);
}); });

View File

@ -31,14 +31,14 @@ export interface IUseTxsData {
} }
export const useTxsData = ({ export const useTxsData = ({
count = 25, count = 50,
before, before,
after, after,
filters, filters,
party, party,
}: IUseTxsData) => { }: IUseTxsData) => {
const [, setSearchParams] = useSearchParams(); const [, setSearchParams] = useSearchParams();
let hasMoreTxs = true; let hasMoreTxs = false;
let txsData: BlockExplorerTransactionResult[] = []; let txsData: BlockExplorerTransactionResult[] = [];
const url = getTxsDataUrl({ const url = getTxsDataUrl({
@ -60,8 +60,8 @@ export const useTxsData = ({
} }
const nextPage = useCallback(() => { const nextPage = useCallback(() => {
const after = data?.transactions.at(-1)?.cursor || ''; const before = data?.transactions.at(-1)?.cursor || '';
const params: URLSearchParamsInit = { after }; const params: URLSearchParamsInit = { before };
if (filters) { if (filters) {
params.filters = Array.from(filters).join(','); params.filters = Array.from(filters).join(',');
} }
@ -69,8 +69,8 @@ export const useTxsData = ({
}, [filters, data, setSearchParams]); }, [filters, data, setSearchParams]);
const previousPage = useCallback(() => { const previousPage = useCallback(() => {
const before = data?.transactions[0]?.cursor || ''; const after = data?.transactions[0]?.cursor || '';
const params: URLSearchParamsInit = { before }; const params: URLSearchParamsInit = { after };
if (filters && filters.size > 0 && filters.size === 1) { if (filters && filters.size > 0 && filters.size === 1) {
params.filters = Array.from(filters)[0]; params.filters = Array.from(filters)[0];
} }

View File

@ -51,9 +51,10 @@ export const TxsListFiltered = () => {
refreshTxs={refreshTxs} refreshTxs={refreshTxs}
nextPage={nextPage} nextPage={nextPage}
previousPage={previousPage} previousPage={previousPage}
hasPreviousPage={true} hasPreviousPage={hasMoreTxs}
loading={loading} loading={loading}
hasMoreTxs={hasMoreTxs} hasMoreTxs={hasMoreTxs}
isEmpty={txsData.length === 0}
> >
<TxsFilter <TxsFilter
filters={filters} filters={filters}
@ -70,7 +71,16 @@ export const TxsListFiltered = () => {
txs={txsData} txs={txsData}
loadMoreTxs={nextPage} loadMoreTxs={nextPage}
error={error} error={error}
className="mb-28 w-full min-w-[400px]" className="mb-4 w-full min-w-[400px]"
/>
<TxsListNavigation
refreshTxs={refreshTxs}
nextPage={nextPage}
previousPage={previousPage}
hasPreviousPage={hasMoreTxs}
loading={loading}
hasMoreTxs={hasMoreTxs}
isEmpty={txsData.length === 0}
/> />
</> </>
); );