-
Notifications
You must be signed in to change notification settings - Fork 0
/
BooksList.js
72 lines (62 loc) · 1.71 KB
/
BooksList.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
import React, { useEffect, useState } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Book from '../components/book';
import { removeBook, changeFilter } from '../actions/index';
import CategoryFilter from '../components/CategoryFilter';
import { getBooks, deleteBook } from '../services/store';
const BooksList = props => {
const {
removeBook, changeFilter, filter,
} = props;
const [books, setBooks] = useState([]);
const handleRemoveBook = book => {
deleteBook(book).then(() => {
removeBook(book);
});
};
const handleFilterChange = e => {
changeFilter(e.target.value);
};
useEffect(() => {
getBooks(setBooks);
}, []);
return (
<>
<div className="category-container">
<div>CATEGORY FILTER:</div>
<CategoryFilter changeFilter={handleFilterChange} />
</div>
<div className="books-table">
<div>
{
books
.filter(book => book.category === filter || filter === 'All')
.map(book => (
<Book key={book.id} book={book} removeBook={handleRemoveBook} />
))
}
</div>
</div>
</>
);
};
BooksList.propTypes = {
// books: PropTypes.instanceOf(Array).isRequired,
removeBook: PropTypes.func.isRequired,
changeFilter: PropTypes.func.isRequired,
filter: PropTypes.string.isRequired,
};
const mapStateToProps = state => ({
books: state.books,
filter: state.filter,
});
const mapDispatchToProps = dispatch => ({
removeBook: id => {
dispatch(removeBook(id));
},
changeFilter: filter => {
dispatch(changeFilter(filter));
},
});
export default connect(mapStateToProps, mapDispatchToProps)(BooksList);