-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookSearch.js
100 lines (91 loc) · 2.21 KB
/
BookSearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React, { Component } from "react";
import BookDetail from "./BookDetail";
import * as BooksAPI from "./BooksAPI";
import { Link } from "react-router-dom";
import { Debounce } from 'react-throttle';
import { If, Then, Else } from 'react-if';
class BookSearch extends Component {
state = {
query: "",
loading: false,
books: []
};
handleInput = query => {
this.setState({ books: [], loading: true, query: query }, () => {
if (this.state.query) {
BooksAPI.search(this.state.query).then(books =>
this.setState({ books: books, loading: false })
);
} else {
this.setState({ books: [], loading: false });
}
});
};
updateShelf(book, shelf) {
BooksAPI.update(book, shelf).then(() => {
console.log("shelf updated");
});
}
displayBooks() {
if (!this.state.query) {
return <div className="books-grid">No books to show!!</div>;
}
if (!this.state.books.error) {
return (
<div>
<div className="books-grid">
Showing {this.state.books.length} books for '{this.state.query}'
</div>
<ol className="books-grid">
{this.state.books.map(book => {
return (
<li key={book.id}>
<BookDetail
book={book}
updateShelf={(book, shelf) => {
this.updateShelf(book, shelf);
}}
/>
</li>
);
})}
</ol>
</div>
);
}
return <div className="books-grid">No books to show!!</div>;
}
loader() {
return <div className="books-grid">Loading...</div>;
}
render() {
return (
<div className="search-books">
<div className="search-books-bar">
<Link className="close-search" to="/">
Close
</Link>
<div className="search-books-input-wrapper">
<Debounce time="300" handler="onChange" >
<input
type="text"
onChange={(event) =>
this.handleInput(event.target.value)}
placeholder="Search by title or author"
/>
</Debounce>
</div>
</div>
<div className="search-books-results">
<div>
<If condition={this.state.loading===true}>
<Then>{this.loader()}</Then>
<Else>{this.displayBooks()}</Else>
</If>
</div>
</div>
</div>
);
}
}
export default BookSearch;