Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react): add custom props for data loader components #8

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/react/components/DataInfiniteLoader/DataInfiniteLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const DataInfiniteLoader = <TError,>({
LoadingView,
ErrorView,
MoreView,
loadingViewProps,
errorViewProps,
moreViewProps,
children,
}: DataInfiniteLoaderProps<TError>): React.ReactNode => {
const errorAction = React.useMemo<ErrorViewProps<TError>['action']>(
Expand All @@ -22,36 +25,32 @@ export const DataInfiniteLoader = <TError,>({
[errorActionProp],
);

const content = React.useMemo(() => {
const renderContent = () => {
if (status === 'loading') {
return <LoadingView />;
return <LoadingView {...loadingViewProps} />;
}

if (status === 'error') {
return <ErrorView error={error} action={errorAction} />;
return <ErrorView error={error} action={errorAction} {...errorViewProps} />;
}

if (status === 'success' && hasNextPage) {
return <MoreView isLoading={isFetchingNextPage} onClick={fetchNextPage} />;
return (
<MoreView
isLoading={isFetchingNextPage}
onClick={fetchNextPage}
{...moreViewProps}
/>
);
}

return null;
}, [
status,
hasNextPage,
LoadingView,
ErrorView,
error,
errorAction,
MoreView,
isFetchingNextPage,
fetchNextPage,
]);
};

return (
<>
{status === 'success' ? children : null}
{content}
{renderContent()}
</>
);
};
16 changes: 12 additions & 4 deletions src/react/components/DataInfiniteLoader/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ export interface MoreViewProps {
onClick: () => void;
}

export interface DataInfiniteLoaderProps<TError> {
export interface DataInfiniteLoaderProps<
TError,
TLoadingViewProps extends {} = {},
TErrorViewProps extends ErrorViewProps<TError> = ErrorViewProps<TError>,
TMoreViewProps extends MoreViewProps = MoreViewProps,
> {
status: DataLoaderStatus;
error: TError | null;
errorAction?: ErrorAction | ErrorAction['handler'];
hasNextPage: boolean;
fetchNextPage: () => unknown;
isFetchingNextPage: boolean;
LoadingView: ComponentType;
ErrorView: ComponentType<ErrorViewProps<TError>>;
MoreView: ComponentType<MoreViewProps>;
LoadingView: ComponentType<TLoadingViewProps>;
ErrorView: ComponentType<TErrorViewProps>;
MoreView: ComponentType<TMoreViewProps>;
loadingViewProps?: TLoadingViewProps;
errorViewProps?: Omit<TErrorViewProps, keyof ErrorViewProps<TError>>;
moreViewProps?: Omit<TMoreViewProps, keyof MoreViewProps>;
children: ReactNode;
}
6 changes: 4 additions & 2 deletions src/react/components/DataLoader/DataLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const DataLoader = <TError,>({
errorAction: errorActionProp,
LoadingView,
ErrorView,
loadingViewProps,
errorViewProps,
children,
}: DataLoaderProps<TError>): React.ReactNode => {
const errorAction = React.useMemo<ErrorViewProps<TError>['action']>(
Expand All @@ -19,11 +21,11 @@ export const DataLoader = <TError,>({
);

if (status === 'loading') {
return <LoadingView />;
return <LoadingView {...loadingViewProps} />;
}

if (status === 'error') {
return <ErrorView error={error} action={errorAction} />;
return <ErrorView error={error} action={errorAction} {...errorViewProps} />;
}

return <>{children}</>;
Expand Down
12 changes: 9 additions & 3 deletions src/react/components/DataLoader/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import type {ComponentType, ReactNode} from 'react';
import type {DataLoaderStatus} from '../../../core';
import type {ErrorAction, ErrorViewProps} from '../types';

export interface DataLoaderProps<TError> {
export interface DataLoaderProps<
TError,
TLoadingViewProps extends {} = {},
TErrorViewProps extends ErrorViewProps<TError> = ErrorViewProps<TError>,
> {
status: DataLoaderStatus;
error: TError | null;
errorAction?: ErrorAction | ErrorAction['handler'];
LoadingView: ComponentType;
ErrorView: ComponentType<ErrorViewProps<TError>>;
LoadingView: ComponentType<TLoadingViewProps>;
ErrorView: ComponentType<TErrorViewProps>;
loadingViewProps?: TLoadingViewProps;
errorViewProps?: Omit<TErrorViewProps, keyof ErrorViewProps<TError>>;
children: ReactNode;
}