Skip to content

Commit

Permalink
Add isbn as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuk committed Nov 13, 2023
1 parent 3df0f4e commit 4d5420d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
12 changes: 9 additions & 3 deletions src/library/application/LibraryRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { RouteObject } from 'react-router-dom';
import { RouteObject, useParams } from 'react-router-dom';
import { BookComponent } from '@/library/infrastructure/primary/BookComponent.tsx';
import { LibraryApp } from '@/library/application/LibraryApp.tsx';
import { ISBN } from '@/library/domain/ISBN.ts';

const BooksPage = () => {
const { isbn } = useParams<string>();
return <BookComponent isbn={ISBN.of(isbn!)} />;
};

export const libraryRoutes: RouteObject = {
path: '/',
element: <LibraryApp />,
children: [
{
path: '',
element: <BookComponent />,
path: 'book/:isbn',
element: <BooksPage />,
},
],
};
3 changes: 2 additions & 1 deletion src/library/domain/Books.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Book } from '@/library/domain/Book';
import { Either } from '@/functional/Either';
import { ISBN } from '@/library/domain/ISBN.ts';

export interface Books {
get(): Promise<Either<Error, Book>>;
get(isbn: ISBN): Promise<Either<Error, Book>>;
}
12 changes: 8 additions & 4 deletions src/library/infrastructure/primary/BookComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import { useTranslation } from 'react-i18next';
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';

export const BookComponent = () => {
export const BookComponent = (props: { isbn: ISBN }) => {
const books = inject(BOOKS);

const { t } = useTranslation();
const [book, setBook] = useState<Book>();

const { isInProgress, isFailing, isSuccessful, errorMessage } = useLoadEither(books.get(), book => {
setBook(book);
});
const { isInProgress, isFailing, isSuccessful, errorMessage } = useLoadEither(
() => books.get(props.isbn),
book => {
setBook(book);
}
);

return (
<>
Expand Down
6 changes: 3 additions & 3 deletions src/library/infrastructure/primary/UseLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface Loaded {
isFailing: boolean;
}

export const useLoadEither = <T>(promise: Promise<Either<Error, T>>, then: (value: T) => void): Loaded => {
export const useLoadEither = <T>(promise: () => Promise<Either<Error, T>>, then: (value: T) => void): Loaded => {
const [errorMessage, setErrorMessage] = useState('');
const [status, setStatus] = useState<LoadingStatus>(IN_PROGRESS);

Expand All @@ -30,8 +30,8 @@ export const useLoadEither = <T>(promise: Promise<Either<Error, T>>, then: (valu
setStatus(SUCCESS);
});

promise.then(successLoad).catch(errorLoad);
}, [promise, then]);
promise().then(successLoad).catch(errorLoad);
}, []);

const is = (to: LoadingStatus): boolean => status === to;

Expand Down
5 changes: 3 additions & 2 deletions src/library/infrastructure/secondary/RestBooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { Either } from '@/functional/Either';
import { Book } from '@/library/domain/Book';
import { AxiosInstance } from 'axios';
import { RestBook, toBook } from '@/library/infrastructure/secondary/RestBook';
import { ISBN } from '@/library/domain/ISBN.ts';

export class RestBooks implements Books {
constructor(private readonly axiosInstance: AxiosInstance) {}

get(): Promise<Either<Error, Book>> {
get(isbn: ISBN): Promise<Either<Error, Book>> {
return this.axiosInstance
.get<RestBook>('/isbn/9780321125217.json')
.get<RestBook>(`/isbn/${isbn.get()}.json`)
.then(response => response.data)
.then(toBook)
.catch(error => Promise.resolve(Either.err(error)));
Expand Down
18 changes: 9 additions & 9 deletions test/component/spec/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const BOOK = {
number_of_pages: 42,
};

const stubOpenLibraryIsbn = () => cy.intercept('https://openlibrary.org/isbn/9780321125217.json', BOOK);
const stubOpenLibraryIsbn = () => cy.intercept('https://openlibrary.org/isbn/9780321125217.json', BOOK).as('BOOK');

const stubOpenLibraryIsbnInvalid = () =>
cy.intercept('https://openlibrary.org/isbn/9780321125217.json', {
Expand Down Expand Up @@ -34,13 +34,13 @@ describe('Library', () => {
});

it('Should be loading before result', () => {
cy.visit('/', loadLanguage('en'));
cy.visit('/book/9780321125217', loadLanguage('en'));

cy.contains(dataSelector('book.loading'), 'In progress');
});

it('Should be loading before result', () => {
cy.visit('/', loadLanguage('fr'));
cy.visit('/book/9780321125217', loadLanguage('fr'));

cy.contains(dataSelector('book.loading'), 'En cours');
});
Expand All @@ -49,23 +49,23 @@ describe('Library', () => {
it('Should not show book with network error', () => {
stubOpenLibraryIsbnNetworkError();

cy.visit('/');
cy.visit('/book/9780321125217');

cy.contains(dataSelector('book.error'), 'Request failed');
});

it('Should not show book with invalid ISBN', () => {
stubOpenLibraryIsbnInvalid();

cy.visit('/');
cy.visit('/book/9780321125217');

cy.contains(dataSelector('book.error'), 'Non digits are not allowed for ISBN');
});

it('Should get book', () => {
stubOpenLibraryIsbn();

cy.visit('/');
cy.visit('/book/9780321125217');

cy.contains(dataSelector('book.title'), 'Domain-driven design');
cy.contains(dataSelector('book.isbn'), '9780321125217');
Expand All @@ -75,15 +75,15 @@ describe('Library', () => {
it('Should have english labels', () => {
stubOpenLibraryIsbn();

cy.visit('/', loadLanguage('en'));
cy.visit('/book/9780321125217', loadLanguage('en'));

cy.contains(dataSelector('book.label.title'), 'Title: ');
});

it('Should have french labels', () => {
it.only('Should have french labels', () => {
stubOpenLibraryIsbn();

cy.visit('/', loadLanguage('fr'));
cy.visit('/book/9780321125217', loadLanguage('fr'));

cy.contains(dataSelector('book.label.title'), 'Titre : ');
});
Expand Down

0 comments on commit 4d5420d

Please sign in to comment.