Skip to content
This repository was archived by the owner on Apr 5, 2020. It is now read-only.

Commit f7645ad

Browse files
committed
feat: search results sync with shelves
- Checking search results in myBooks and updating .shelf field
1 parent f8e9529 commit f7645ad

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class BooksApp extends React.Component {
4646
<Route
4747
path='/search'
4848
render={({history}) => (
49-
<SearchBooks onHandleChange={this.handleChange}/>
49+
<SearchBooks myBooks={this.state.books} onHandleChange={this.handleChange}/>
5050
)}
5151
/>
5252

src/SearchBooks.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,27 @@ class SearchBooks extends Component {
1010
constructor() {
1111
super();
1212
this.state = {
13-
books: []
13+
searchResults: [],
1414
};
1515

1616
this.searchBooks = this.searchBooks.bind(this);
1717
}
1818

1919
searchBooks(e) {
2020
e.preventDefault();
21+
2122
const values = serializeForm(e.target, {hash: true});
22-
console.log(values.query);
23-
BooksAPI.search(values.query, 20).then((books) => {
24-
console.log(books);
25-
this.setState({books})
23+
const myBooks = this.props.myBooks;
24+
let findBook = (book, myBooks) => {
25+
26+
myBooks.find((bookToFind) => bookToFind.id === book.id)
27+
};
28+
29+
BooksAPI.search(values.query, 20).then((searchResults) => {
30+
31+
searchResults.map(book => findBook(book, myBooks) ? book.shelf = findBook(book, myBooks).shelf : book.shelf = 'none');
32+
this.setState({searchResults})
33+
2634
})
2735
}
2836

@@ -47,7 +55,7 @@ class SearchBooks extends Component {
4755
</form>
4856
</div>
4957
<div className="search-books-results">
50-
<BooksGrid books={this.state.books} onHandleChange={this.props.onHandleChange}/>
58+
<BooksGrid books={this.state.searchResults} onHandleChange={this.props.onHandleChange}/>
5159
</div>
5260
</div>
5361
)

0 commit comments

Comments
 (0)