Add additional info about SWR infinite load to response (#321)
The SWR response only has "isLoading" and "isValidating". "isLoading" only indicates the initial load. For every subsequent load, only "isValidating" gets set to true.
This commit is contained in:
@@ -52,10 +52,16 @@ type CustomSWROptions<Data> = {
|
|||||||
type SWROptions<Data = any, Error = any> = SWRConfiguration<Data, Error> & CustomSWROptions<Data>;
|
type SWROptions<Data = any, Error = any> = SWRConfiguration<Data, Error> & CustomSWROptions<Data>;
|
||||||
type SWRInfiniteOptions<Data = any, Error = any> = SWRInfiniteConfiguration<Data, Error> & CustomSWROptions<Data>;
|
type SWRInfiniteOptions<Data = any, Error = any> = SWRInfiniteConfiguration<Data, Error> & CustomSWROptions<Data>;
|
||||||
|
|
||||||
|
type SWRInfiniteResponseLoadInfo = {
|
||||||
|
isInitialLoad: boolean;
|
||||||
|
isLoadMore: boolean;
|
||||||
|
};
|
||||||
type AbortableRequest = { abortRequest: AbortController['abort'] };
|
type AbortableRequest = { abortRequest: AbortController['abort'] };
|
||||||
type AbortableAxiosResponse<Data = any> = { response: Promise<AxiosResponse<Data>> } & AbortableRequest;
|
type AbortableAxiosResponse<Data = any> = { response: Promise<AxiosResponse<Data>> } & AbortableRequest;
|
||||||
type AbortableSWRResponse<Data = any, Error = any> = SWRResponse<Data, Error> & AbortableRequest;
|
type AbortableSWRResponse<Data = any, Error = any> = SWRResponse<Data, Error> & AbortableRequest;
|
||||||
type AbortableSWRInfiniteResponse<Data = any, Error = any> = SWRInfiniteResponse<Data, Error> & AbortableRequest;
|
type AbortableSWRInfiniteResponse<Data = any, Error = any> = SWRInfiniteResponse<Data, Error> &
|
||||||
|
AbortableRequest &
|
||||||
|
SWRInfiniteResponseLoadInfo;
|
||||||
|
|
||||||
// the following endpoints have not been implemented:
|
// the following endpoints have not been implemented:
|
||||||
// - PUT /api/v1/manga/{mangaId}/chapter/{chapterIndex} - modify chapter # PATCH endpoint used instead
|
// - PUT /api/v1/manga/{mangaId}/chapter/{chapterIndex} - modify chapter # PATCH endpoint used instead
|
||||||
@@ -144,11 +150,11 @@ export class RequestManager {
|
|||||||
axiosOptions,
|
axiosOptions,
|
||||||
swrOptions,
|
swrOptions,
|
||||||
}: { data?: any; axiosOptions?: AxiosRequestConfig; swrOptions?: OptionsSWR } = {},
|
}: { data?: any; axiosOptions?: AxiosRequestConfig; swrOptions?: OptionsSWR } = {},
|
||||||
): SWRInfiniteResponse<Data, ErrorResponse> {
|
): SWRInfiniteResponse<Data, ErrorResponse> & SWRInfiniteResponseLoadInfo {
|
||||||
const { skipRequest, ...swrConfig } = swrOptions ?? {};
|
const { skipRequest, ...swrConfig } = swrOptions ?? {};
|
||||||
|
|
||||||
// useSWRInfinite will (by default) revalidate the first page, to check if the other pages have to be revalidated as well
|
// useSWRInfinite will (by default) revalidate the first page, to check if the other pages have to be revalidated as well
|
||||||
const result = useSWRInfinite<Data, ErrorResponse>(
|
const swrResult = useSWRInfinite<Data, ErrorResponse>(
|
||||||
(index, previousData) => {
|
(index, previousData) => {
|
||||||
const pageEndpoint = getEndpoint(index, previousData);
|
const pageEndpoint = getEndpoint(index, previousData);
|
||||||
return pageEndpoint !== null && !skipRequest ? this.getValidUrlFor(pageEndpoint) : null;
|
return pageEndpoint !== null && !skipRequest ? this.getValidUrlFor(pageEndpoint) : null;
|
||||||
@@ -159,14 +165,17 @@ export class RequestManager {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
const isNextPageMissing = !!swrResult.data && typeof swrResult.data[swrResult.size - 1] === 'undefined';
|
||||||
...result,
|
const isLoadingMore = swrResult.size > 0 && isNextPageMissing;
|
||||||
|
const customSwrResult = {
|
||||||
|
...swrResult,
|
||||||
|
isInitialLoad: swrResult.isLoading,
|
||||||
// SWR "isLoading" state is only updated for the first load, for every subsequent load it's "false"
|
// SWR "isLoading" state is only updated for the first load, for every subsequent load it's "false"
|
||||||
isLoading:
|
isLoadMore: !swrResult.isLoading && isLoadingMore,
|
||||||
result.isLoading ||
|
|
||||||
// check if more data is being loaded
|
|
||||||
(result.size > 0 && !!result.data && typeof result.data[result.size - 1] === 'undefined'),
|
|
||||||
};
|
};
|
||||||
|
customSwrResult.isLoading = customSwrResult.isInitialLoad || customSwrResult.isLoadMore;
|
||||||
|
|
||||||
|
return customSwrResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user