Commit 9538a8f5 authored by tom's avatar tom

fix ts overload

parent f2a7a152
......@@ -692,7 +692,10 @@ export type PaginatedResources = 'blocks' | 'block_txs' |
export type PaginatedResponse<Q extends PaginatedResources> = ResourcePayload<Q>;
/* eslint-disable @typescript-eslint/indent */
export type ResourcePayload<Q extends ResourceName> =
// !!! IMPORTANT !!!
// Don't add any new types here because TypeScript cannot handle it properly
// use ResourcePayloadB instead
export type ResourcePayloadA<Q extends ResourceName> =
Q extends 'user_info' ? UserInfo :
Q extends 'custom_abi' ? CustomAbis :
Q extends 'public_tags' ? PublicTags :
......@@ -793,11 +796,25 @@ Q extends 'domains_lookup' ? EnsDomainLookupResponse :
Q extends 'user_ops' ? UserOpsResponse :
Q extends 'user_op' ? UserOp :
Q extends 'user_ops_account' ? UserOpsAccount :
never;
// !!! IMPORTANT !!!
// See comment above
/* eslint-enable @typescript-eslint/indent */
/* eslint-disable @typescript-eslint/indent */
export type ResourcePayloadB<Q extends ResourceName> =
Q extends 'marketplace_dapps' ? Array<MarketplaceAppOverview> :
Q extends 'marketplace_dapp' ? MarketplaceAppOverview :
never;
/* eslint-enable @typescript-eslint/indent */
export type ResourcePayload<Q extends ResourceName> = ResourcePayloadA<Q> | ResourcePayloadB<Q>;
// Right now there is no paginated resources in B-part
// Add "| ResourcePayloadB<Q>[...]" if it is not true anymore
export type PaginatedResponseItems<Q extends ResourceName> = Q extends PaginatedResources ? ResourcePayloadA<Q>['items'] : never;
export type PaginatedResponseNextPageParams<Q extends ResourceName> = Q extends PaginatedResources ? ResourcePayloadA<Q>['next_page_params'] : never;
/* eslint-disable @typescript-eslint/indent */
export type PaginationFilters<Q extends PaginatedResources> =
Q extends 'blocks' ? BlockFilters :
......
import type { ArrayElement } from 'types/utils';
import type { PaginatedResources, PaginatedResponse } from 'lib/api/resources';
import type { PaginatedResources, PaginatedResponse, PaginatedResponseItems } from 'lib/api/resources';
export function generateListStub<Resource extends PaginatedResources>(
stub: ArrayElement<PaginatedResponse<Resource>['items']>,
stub: ArrayElement<PaginatedResponseItems<Resource>>,
num = 50,
rest: Omit<PaginatedResponse<Resource>, 'items'>,
) {
......
......@@ -34,6 +34,14 @@ function getPaginationParamsFromQuery(queryString: string | Array<string> | unde
return {};
}
function getNextPageParams<R extends PaginatedResources>(data: ResourcePayload<R> | undefined) {
if (!data || typeof data !== 'object' || !('next_page_params' in data)) {
return;
}
return data.next_page_params;
}
export type QueryWithPagesResult<Resource extends PaginatedResources> =
UseQueryResult<ResourcePayload<Resource>, ResourceError<unknown>> &
{
......@@ -76,29 +84,30 @@ export default function useQueryWithPages<Resource extends PaginatedResources>({
},
});
const { data } = queryResult;
const nextPageParams = getNextPageParams(data);
const onNextPageClick = useCallback(() => {
if (!data?.next_page_params) {
if (!nextPageParams) {
// we hide next page button if no next_page_params
return;
}
setPageParams((prev) => ({
...prev,
[page + 1]: data.next_page_params as NextPageParams,
[page + 1]: nextPageParams as NextPageParams,
}));
setPage(prev => prev + 1);
const nextPageQuery = {
...router.query,
page: String(page + 1),
next_page_params: encodeURIComponent(JSON.stringify(data.next_page_params)),
next_page_params: encodeURIComponent(JSON.stringify(nextPageParams)),
};
setHasPages(true);
scrollToTop();
router.push({ pathname: router.pathname, query: nextPageQuery }, undefined, { shallow: true });
}, [ data?.next_page_params, page, router, scrollToTop ]);
}, [ nextPageParams, page, router, scrollToTop ]);
const onPrevPageClick = useCallback(() => {
// returning to the first page
......@@ -181,7 +190,6 @@ export default function useQueryWithPages<Resource extends PaginatedResources>({
});
}, [ router, scrollToTop ]);
const nextPageParams = data?.next_page_params;
const hasNextPage = nextPageParams ? Object.keys(nextPageParams).length > 0 : false;
const pagination = {
......
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