Skip to content

Commit

Permalink
Move React loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuk committed Aug 9, 2024
1 parent f53807f commit 1d83e63
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/library/infrastructure/primary/BookComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { inject } from '@/injections.ts';
import { BOOKS } from '@/library/application/LibraryKeys.ts';
import { Book } from '@/library/domain/Book.ts';
import { ISBN } from '@/library/domain/ISBN.ts';
import { Loader, loadError, loadFor, loadInProgress, loadSuccess } from '@/library/infrastructure/primary/Loader.ts';
import { Loader, loadError, loadInProgress, loadSuccess } from '@/library/infrastructure/primary/Loader.ts';
import {reactLoadFor} from "@/library/infrastructure/primary/reactLoadFor.ts";

const BookInfoComponent = ({ book }: { book: Book }) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -36,7 +37,7 @@ export const BookComponent = (props: { isbn: ISBN }) => {
.catch((error: Error) => setBookLoader(loadError(error.message)));
}, [props.isbn]);

return loadFor(bookLoader)({
return reactLoadFor(bookLoader)({
progress: () => <p data-selector="book.loading">{t('book.inProgress')}</p>,
error: message => <p data-selector="book.error">{message}</p>,
success: book => <BookInfoComponent book={book} />,
Expand Down
21 changes: 0 additions & 21 deletions src/library/infrastructure/primary/Loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ReactElement } from 'react';

export const LoadingInProgress = Symbol();
export const LoadingError = Symbol();
export const LoadingSuccess = Symbol();
Expand Down Expand Up @@ -37,22 +35,3 @@ export const loadError = (errorMessage: string): LoadError => ({
errorMessage,
status: LoadingError,
});

type LoaderCallback<T> = {
success: (content: T) => ReactElement;
error: (message: string) => ReactElement;
progress: () => ReactElement;
};

export const loadFor =
<T>(loader: Loader<T>) =>
({ success, error, progress }: LoaderCallback<T>): ReactElement => {
switch (loader.status) {
case LoadingInProgress:
return progress();
case LoadingError:
return error(loader.errorMessage);
case LoadingSuccess:
return success(loader.content);
}
};
21 changes: 21 additions & 0 deletions src/library/infrastructure/primary/reactLoadFor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {ReactElement} from "react";
import {Loader, LoadingError, LoadingInProgress, LoadingSuccess} from "@/library/infrastructure/primary/Loader.ts";

type LoaderCallback<T> = {
success: (content: T) => ReactElement;
error: (message: string) => ReactElement;
progress: () => ReactElement;
};

export const reactLoadFor =
<T>(loader: Loader<T>) =>
({success, error, progress}: LoaderCallback<T>): ReactElement => {
switch (loader.status) {
case LoadingInProgress:
return progress();
case LoadingError:
return error(loader.errorMessage);
case LoadingSuccess:
return success(loader.content);
}
};

0 comments on commit 1d83e63

Please sign in to comment.