Advertisement
Guest User

App.js

a guest
May 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import * as BooksAPI from './BooksAPI'
  3. import './App.css'
  4. import { Link, Route } from 'react-router-dom'
  5. import SearchBooks from "./SearchBooks";
  6. import BookShelf from "./BookShelf"
  7.  
  8. class BooksApp extends React.Component {
  9.     state = {
  10.         books: []
  11.     };
  12.  
  13.     updateBooks = () => {
  14.         BooksAPI.getAll().then( (books) => {
  15.             books.map((book) => {
  16.                 //prevents rendering errors if books don't have an image link
  17.                 if(!book.imageLinks) {
  18.                     book.imageLinks = {
  19.                         thumbnail: ''
  20.                     };
  21.                 }
  22.  
  23.                 //prevents rendering errors if books don't have an author
  24.                 if(!book.authors) {
  25.                     book.authors = [];
  26.                 }
  27.                 return book;
  28.             });
  29.  
  30.             this.setState({ books })
  31.         });
  32.     };
  33.  
  34.     componentDidMount() {
  35.         this.updateBooks();
  36.     };
  37.  
  38.  
  39.     render() {
  40.         const shelves = {
  41.             currentlyReading: ['Currently Reading', 'currentlyReading'],
  42.             wantToRead: ['Want to Read', 'wantToRead'],
  43.             read: ['Read', 'read']
  44.           }
  45.  
  46.  
  47.         return (
  48.             <div className="app">
  49.                 {/*<Route path="/search" component={SearchBooks}/>*/}
  50.                 <Route path="/search" render={() => (
  51.                     <SearchBooks updateBooks={this.updateBooks} shelvedBooks={this.state.books}/>
  52.                 )}/>
  53.                 <Route exact path="/" render={() => (
  54.                         <div className="list-books">
  55.                             <div className="list-books-title">
  56.                                 <h1>MyReads</h1>
  57.                             </div>
  58.  
  59.                             <div className="list-books-content">
  60.                                 { Object.keys(shelves).map((shelf) =>
  61.                                     <BookShelf key={shelf}
  62.                                         books={this.state.books}
  63.                                         shelf={shelves[shelf][1]}
  64.                                         updateBooks={this.updateBooks}
  65.                                         shelfTitle={shelves[shelf][0]}/>
  66.                                 )}
  67.                             </div>
  68.                             <div className="open-search">
  69.                                 <Link
  70.                                     to={"/search"}
  71.                                 >Add a book</Link>
  72.                             </div>
  73.                         </div>
  74.                         )
  75.                     }
  76.                 />
  77.             </div>
  78.         )
  79.     }
  80. }
  81.  
  82. export default BooksApp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement