-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchBooks.js
73 lines (64 loc) · 2.22 KB
/
SearchBooks.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
import React, {Component} from 'react'
import {Link} from 'react-router-dom'
import PropTypes from 'prop-types'
import * as BooksAPI from './utils/BooksAPI'
import BookShelf from "./BookShelf";
class SearchBooks extends Component {
state = {
query: '',
books: []
}
static propTypes = {
handleUpdate: PropTypes.func.isRequired,
books: PropTypes.oneOfType([PropTypes.array, PropTypes.object]).isRequired
}
updateQuery(query) {
this.setState({query})
}
searchBooks(query) {
if(query) {
BooksAPI.search(query, 20).then((res) => {
let receivedBooks = res
if(receivedBooks.length > 1) {
receivedBooks.map((book) => {
this.props.books.map((oldBook) => {
if(book.id === oldBook.id) {
book.shelf = oldBook.shelf
}
})
})
}
if(this.refs.myRef) {
this.setState({ books: receivedBooks })
}
})
}
}
render() {
const {query, books} = this.state
const {handleUpdate} = this.props
this.searchBooks(query)
let queryString = "Showing results for \""+query+"\"";
return (
<div className="search-books" ref="myRef">
<div className="search-books-bar">
<Link to="/" className="close-search">Close</Link>
<div className="search-books-input-wrapper">
<input
type="text"
placeholder="Search by title or author"
value={query}
onChange={(event) => this.updateQuery(event.target.value)}
/>
</div>
</div>
<div className="search-books-results">
{query && (
<BookShelf name={queryString} books={books} handleUpdate={handleUpdate}/>
)}
</div>
</div>
)
}
}
export default SearchBooks