Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-dev committed Apr 24, 2020
1 parent aa15170 commit f96daab
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 51 deletions.
38 changes: 8 additions & 30 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Route, Link } from 'react-router-dom';
import { Route } from 'react-router-dom';
import * as BooksAPI from './BooksAPI';
import Shelf from './Shelf';
import Shelves from './Shelves';
import Search from './Search';
import './App.css';

Expand All @@ -22,7 +22,7 @@ const shelves = [

class BooksApp extends React.Component {
state = {
booksInShelves: [], // Contains books that are assigned to any shelf
booksInShelves: [], // Contains books that are assigned to a shelf
};

componentDidMount() {
Expand Down Expand Up @@ -63,10 +63,6 @@ class BooksApp extends React.Component {
});
};

getBooksByShelf = (shelfId) => {
return this.state.booksInShelves.filter((b) => b.shelf === shelfId);
};

isShelf = (shelfId) => {
return shelves.map((shelf) => shelf.id).includes(shelfId);
};
Expand All @@ -80,29 +76,11 @@ class BooksApp extends React.Component {
exact
path="/"
render={() => (
<div className="list-books">
<div className="list-books-title">
<h1>MyReads</h1>
</div>
<div className="list-books-content">
<div>
{shelves.map((shelf) => (
<Shelf
key={shelf.id}
shelf={shelf}
shelves={shelves}
books={this.getBooksByShelf(shelf.id)}
onBookMove={this.moveBook}
/>
))}
</div>
</div>
<div className="open-search">
<Link to="/search">
<button>Add a book</button>
</Link>
</div>
</div>
<Shelves
shelves={shelves}
books={booksInShelves}
onBookMove={this.moveBook}
/>
)}
/>
<Route
Expand Down
53 changes: 32 additions & 21 deletions src/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,43 @@ class Search extends Component {
query,
}),
() => {
let newSearchState = {
searchedBooks: [],
hasSearchError: false,
};
query.length > 0 &&
BooksAPI.search(query).then((books) => {
const isValidSearch =
typeof books !== 'undefined' && !books.hasOwnProperty('error');
if (isValidSearch) {
this.mapBooksToShelves(books); // Searched books don't contain shelf attributes, so map them
newSearchState.searchedBooks = books || [];
} else {
newSearchState.hasSearchError = true;
}
});
this.setState(() => newSearchState);
query.length > 0
? this.performSearch(query)
: this.setState(() => ({
searchedBooks: [],
hasSearchError: false,
}));
}
);
};

mapBooksToShelves = (books) => {
performSearch = (query) => {
BooksAPI.search(query).then((searchedBooks) => {
const isValidSearch =
typeof searchedBooks !== 'undefined' &&
!searchedBooks.hasOwnProperty('error');
if (isValidSearch) {
this.mapBooksToShelves(searchedBooks);
this.setState(() => ({
searchedBooks: searchedBooks || [],
hasSearchError: false,
}));
} else {
this.setState(() => ({
searchedBooks: [],
hasSearchError: true,
}));
}
});
};

mapBooksToShelves = (searchedBooks) => {
const { booksInShelves } = this.props;
// Searched books don't contain shelf attributes, so map them
booksInShelves.map((bookInShelf) =>
books
.filter((book) => book.id === bookInShelf.id)
.map((book) => (book.shelf = bookInShelf.shelf))
searchedBooks
.filter((searchedBook) => searchedBook.id === bookInShelf.id)
.map((searchedBook) => (searchedBook.shelf = bookInShelf.shelf))
);
};

Expand Down Expand Up @@ -85,7 +96,7 @@ class Search extends Component {

Search.propTypes = {
shelves: PropTypes.array.isRequired,
booksInShelves: PropTypes.object.isRequired,
booksInShelves: PropTypes.array.isRequired,
onBookMove: PropTypes.func.isRequired,
};

Expand Down
46 changes: 46 additions & 0 deletions src/Shelves.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { Link } from 'react-router-dom';
import Shelf from './Shelf';
import PropTypes from 'prop-types';

const Shelves = (props) => {
const { books, shelves, onBookMove } = props;

const getBooksByShelf = (shelfId) => {
return books.filter((b) => b.shelf === shelfId);
};

return (
<div className="list-books">
<div className="list-books-title">
<h1>MyReads</h1>
</div>
<div className="list-books-content">
<div>
{shelves.map((shelf) => (
<Shelf
key={shelf.id}
shelf={shelf}
shelves={shelves}
books={getBooksByShelf(shelf.id)}
onBookMove={onBookMove}
/>
))}
</div>
</div>
<div className="open-search">
<Link to="/search">
<button>Add a book</button>
</Link>
</div>
</div>
);
};

Shelves.propTypes = {
books: PropTypes.array.isRequired,
shelves: PropTypes.array.isRequired,
onBookMove: PropTypes.func.isRequired,
};

export default Shelves;

0 comments on commit f96daab

Please sign in to comment.