import React, { Component } from 'react';
import './SearchBooks.css';
import axios from 'axios';
export default class SearchBooks extends Component {
constructor(props) {
super(props)
this.state = {
nameBooks: '',
allBooks: null,
allBook: {}
};
this.timerId = null;
}
searchBooks = (e) => {
this.setState({
nameBooks: e.target.value
});
if (this.timerId) {
clearTimeout(this.timerId);
this.timerId = null;
};
this.timerId = setTimeout(() => {
const books = [];
axios.get(`http://openlibrary.org/search.json?title=${this.state.nameBooks}`)
.then(res => {
const titleBooks = res.data.docs;
titleBooks.map(book => {
books.push(book.title)
book.key = {}
this.setState({
allBooks: books
});
console.log(this.state, 'allbook')
});
});
}, 1000);
};
async getBooks() {
}
render() {
return (
<div className='SearchBooks'>
<div className='title'>
<h1>Look for your favorite book </h1>
</div>
<div className='search'>
<input onChange={this.searchBooks} className='inp' type='text' />
</div>
<i className="fa fa-search " onClick={() => this.getBooks()} />
<div className='bookBox'>
{this.state.allBooks ?
this.state.allBooks?.map(book => {
return (
<span className='book'>{book}</span>
)
})
:
null
}
</div>
</div>
);
};
}