-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
86 lines (75 loc) · 2.61 KB
/
App.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
import React from 'react'
import * as BooksAPI from './utils/BooksAPI'
import './App.css'
import {Route, Link} from 'react-router-dom'
import SearchBooks from './SearchBooks'
import Header from './Header'
import BookShelf from './BookShelf'
class App extends React.Component {
state = {
books: [],
currentlyReading: [],
wantToRead: [],
read: []
}
componentDidMount() {
BooksAPI.getAll().then((books) => {
this.setState({books})
this.updateShelves();
})
}
updateShelves() {
const {books} = this.state
let currentlyReading = books.filter(book => book.shelf === "currentlyReading")
let wantToRead = books.filter(book => book.shelf === "wantToRead")
let read = books.filter(book => book.shelf === "read")
this.setState({currentlyReading, wantToRead, read})
}
changeShelf = (event, book) => {
let books = this.state.books
let newBook = true
books.map(b => {
if (b.id === book.id) {
newBook = false
}
})
if (newBook) {
books.push(book)
}
books.forEach((b) => {
if (b.id === book.id) {
b.shelf = event.target.value
}
})
this.setState({books})
this.updateShelves()
BooksAPI.update(book, event.target.value)
}
render() {
const {currentlyReading, wantToRead, read, books} = this.state
return (
<div className="app">
<Route exact path="/" render={() => (
<div className="list-books">
<Header/>
<div className="list-books-content">
<div>
<BookShelf name="Currently Reading" books={currentlyReading}
handleUpdate={this.changeShelf}/>
<BookShelf name="Want To Read" books={wantToRead} handleUpdate={this.changeShelf}/>
<BookShelf name="Read" books={read} handleUpdate={this.changeShelf}/>
</div>
</div>
<div className="open-search">
<Link to="/search">Add a book</Link>
</div>
</div>
)}/>
<Route path="/search" render={() => (
<SearchBooks handleUpdate={this.changeShelf} books={books}/>
)}/>
</div>
)
}
}
export default App