Skip to content

Commit 938fecf

Browse files
committed
refactor: Enhanced code readability slightly
1 parent ab46cdb commit 938fecf

File tree

4 files changed

+75
-53
lines changed

4 files changed

+75
-53
lines changed

src/RTKLoader.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,44 @@ type Props<T> = {
2020
export function RTKLoader<T>(
2121
props: Props<T>
2222
): React.ReactElement {
23-
if (props.query.isLoading || props.query.isUninitialized) {
23+
const shouldLoad =
24+
props.query.isLoading || props.query.isUninitialized;
25+
const hasError = props.query.isError && props.query.error;
26+
const isFetching = props.query.isFetching;
27+
28+
if (shouldLoad) {
2429
return props.loader ?? <React.Fragment />;
2530
}
26-
if (props.query.isError && props.query.error) {
31+
32+
if (hasError) {
2733
return (
2834
props.onError?.(props.query.error as SerializedError) ?? (
2935
<React.Fragment />
3036
)
3137
);
3238
}
33-
if (props.query.isFetching && props.onFetching) {
39+
40+
if (isFetching && props.onFetching) {
3441
return props.onFetching;
3542
}
43+
3644
if (props.query.data !== undefined) {
45+
const prepend = isFetching
46+
? props.whileFetching?.prepend ?? null
47+
: null;
48+
const append = isFetching
49+
? props.whileFetching?.append ?? null
50+
: null;
51+
const componentWithData = props.onSuccess(props.query.data);
52+
3753
return (
3854
<>
39-
{props.query.isFetching
40-
? props.whileFetching?.prepend ?? null
41-
: null}
42-
{props.onSuccess(props.query.data)}
43-
{props.query.isFetching
44-
? props.whileFetching?.append ?? null
45-
: null}
55+
{prepend}
56+
{componentWithData}
57+
{append}
4658
</>
4759
);
4860
}
61+
4962
return <React.Fragment />;
5063
}

src/createLoader.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export const createUseLoader = <
88
>(
99
createUseLoaderArgs: Types.CreateUseLoaderArgs<QRU, R, A>
1010
): Types.UseLoader<A, R> => {
11-
// useLoader
12-
return (...args) => {
11+
const useLoader = (...args: Types.OptionalGenericArg<A>) => {
1312
const createdQueries = createUseLoaderArgs.queries(...args);
1413
const aggregatedQuery = aggregateToQuery(createdQueries);
1514

@@ -30,7 +29,7 @@ export const createUseLoader = <
3029

3130
return aggregatedQuery as Types.UseQueryResult<R>;
3231
};
33-
//
32+
return useLoader;
3433
};
3534

3635
export const createLoader = <

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ export type WithLoaderArgs<
8585
A = never
8686
> = Loader<P, R, A>;
8787

88-
type WhileFetchingArgs<P extends unknown, R extends unknown> = {
88+
export type WhileFetchingArgs<
89+
P extends unknown,
90+
R extends unknown
91+
> = {
8992
prepend?: (props: P, data?: R) => ReactElement;
9093
append?: (props: P, data?: R) => ReactElement;
9194
};
@@ -103,6 +106,7 @@ export type CreateLoaderArgs<
103106
error: FetchBaseQueryError | SerializedError,
104107
joinedQuery: UseQueryResult<undefined>
105108
) => ReactElement;
109+
/** @deprecated Using onFetching might result in loss of internal state. Use `whileFetching` instead, or pass the query to the component */
106110
onFetching?: (
107111
props: P,
108112
renderBody: () => ReactElement
@@ -123,6 +127,7 @@ export type Loader<
123127
error: SerializedError | FetchBaseQueryError,
124128
joinedQuery: UseQueryResult<undefined>
125129
) => ReactElement;
130+
/** @deprecated Using onFetching might result in loss of internal state. Use `whileFetching` instead, or pass the query to the component */
126131
onFetching?: (
127132
props: P,
128133
renderBody: () => ReactElement

src/withLoader.tsx

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { SerializedError } from "@reduxjs/toolkit";
2+
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query";
13
import React from "react";
24
import { RTKLoader } from "./RTKLoader";
35
import * as Types from "./types";
@@ -25,48 +27,51 @@ export const withLoader = <
2527
) as unknown as Types.ComponentWithLoaderData<P, R>;
2628
}
2729

30+
const onLoading = args.onLoading?.(props);
31+
32+
const onError = args.onError
33+
? (error: SerializedError | FetchBaseQueryError) => {
34+
if (!args.onError) return <React.Fragment />;
35+
return args.onError(
36+
props,
37+
error,
38+
query as Types.UseQueryResult<undefined>
39+
);
40+
}
41+
: undefined;
42+
43+
const onSuccess = (data: R) => (
44+
<CachedComponent {...props} ref={data} />
45+
);
46+
47+
const whileFetching = args.whileFetching
48+
? {
49+
prepend: args.whileFetching.prepend?.(
50+
props,
51+
query?.data
52+
),
53+
append: args.whileFetching.append?.(
54+
props,
55+
query?.data
56+
),
57+
}
58+
: undefined;
59+
60+
const onFetching = args?.onFetching?.(
61+
props,
62+
query.data
63+
? () => onSuccess(query.data as R)
64+
: () => <React.Fragment />
65+
);
66+
2867
return (
2968
<RTKLoader
3069
query={query}
31-
loader={args.onLoading?.(props)}
32-
onError={
33-
args.onError
34-
? (error) =>
35-
args.onError?.(
36-
props,
37-
error,
38-
query as Types.UseQueryResult<undefined>
39-
) ?? <React.Fragment />
40-
: undefined
41-
}
42-
onSuccess={(data) => (
43-
<CachedComponent {...props} ref={data} />
44-
)}
45-
whileFetching={
46-
args.whileFetching
47-
? {
48-
prepend: args.whileFetching?.prepend?.(
49-
props,
50-
query?.data
51-
),
52-
append: args.whileFetching?.append?.(
53-
props,
54-
query?.data
55-
),
56-
}
57-
: undefined
58-
}
59-
onFetching={args?.onFetching?.(
60-
props,
61-
query.data
62-
? () => (
63-
<CachedComponent
64-
{...props}
65-
ref={query.data as R}
66-
/>
67-
)
68-
: () => <React.Fragment />
69-
)}
70+
onSuccess={onSuccess}
71+
onError={onError}
72+
loader={onLoading}
73+
onFetching={onFetching}
74+
whileFetching={whileFetching}
7075
/>
7176
);
7277
};

0 commit comments

Comments
 (0)