Skip to content

Commit

Permalink
Merge pull request #8 from iks3000/async-data
Browse files Browse the repository at this point in the history
async data
  • Loading branch information
iks3000 authored May 23, 2020
2 parents 2f6257d + f055a4c commit 65676b0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/components/book-list-item/book-list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import './book-list-item.css';
const BookListItem = ({ book }) => {
const { title, author, price, coverImage } = book;
return (
<div className="book-list-item bg-light rounded rounded shadow p-3">
<div className="book-list-item bg-light rounded shadow p-3">
<div className="book-cover">
<img src={coverImage} alt="cover"/>
</div>
<div className="book-details">
<a href="#" className="book-title">{title}</a>
<span className="book-title">{title}</span>
<div className="book-author">{author}</div>
<div className="book-price">${price}</div>
<button className="btn btn-info add-to-cart shadow-none">Add to cart</button>
Expand Down
22 changes: 10 additions & 12 deletions src/components/book-list/book-list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import BookListItem from '../book-list-item';
import Spinner from '../spinner';
import { connect } from 'react-redux';

import { withBookstoreService } from '../hoc';
Expand All @@ -10,17 +11,16 @@ import './book-list.css';
class BookList extends Component {

componentDidMount() {
// 1. receive data
const { bookstoreService } = this.props;
const data = bookstoreService.getBooks();
console.log(data);

// 2. dispatch action to store
this.props.booksLoaded(data);
const { bookstoreService, booksLoaded } = this.props;
bookstoreService.getBooks()
.then((data) => booksLoaded(data));
}

render() {
const { books } = this.props;
const { books, loading } = this.props;
if (loading) {
return <Spinner />
}
return (
<ul className="book-list d-flex flex-wrap justify-content-start p-0">
{
Expand All @@ -37,10 +37,8 @@ class BookList extends Component {
}
};

const mapStateToProps = (state) => {
return {
books: state.books
}
const mapStateToProps = ({ books, loading }) => {
return { books, loading }
}

const mapDispatchToProps = {
Expand Down
8 changes: 8 additions & 0 deletions src/components/shop-header/shop-header.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
padding-right: 1rem;
}

.shop-header a {
transition: all 0.3s;
}

.shop-header a:hover {
text-decoration: none;
}

.shop-header .shopping-cart .cart-icon {
font-size: 2.2rem;
color: cadetblue;
Expand Down
16 changes: 10 additions & 6 deletions src/components/shop-header/shop-header.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from 'react';
import { Link } from 'react-router-dom';
import './shop-header.css';

const ShopHeader = ({ numItem, total }) => {
return (
<header className="shop-header row">
<a href="#" className="logo text-dark">BookStore</a>
<a href="#" className="shopping-cart">
<i className="cart-icon fa fa-shopping-cart" />
{numItem} item (${total})
</a>
<Link to="/">
<div className="logo text-dark">BookStore</div>
</Link>
<Link to="/cart">
<div className="shopping-cart d-flex align-items-center h-100">
<i className="cart-icon fa fa-shopping-cart" />
{numItem} item (${total})
</div>
</Link>
</header>
)
};
Expand Down
8 changes: 4 additions & 4 deletions src/components/shopping-cart-table/shopping-cart-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ const ShoppingCartTable = () => {
<td>2</td>
<td>$40</td>
<td>
<button className="btn btn-outline-danger btn-sm shadow-none">
<i className="fa fa-trash-o" />
<button className="btn btn-outline-warning btn-sm shadow-none">
<i className="fa fa-minus-circle" />
</button>
<button className="btn btn-outline-success btn-sm shadow-none">
<i className="fa fa-plus-circle" />
</button>
<button className="btn btn-outline-warning btn-sm shadow-none">
<i className="fa fa-minus-circle" />
<button className="btn btn-outline-danger btn-sm shadow-none">
<i className="fa fa-trash-o" />
</button>
</td>
</tr>
Expand Down
6 changes: 4 additions & 2 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@

const initialState = {
books: []
books: [],
loading: true
};

const reducer = (state = initialState, action) => {

switch (action.type) {
case 'BOOKS_LOADED':
return {
books: action.payload
books: action.payload,
loading: false
};
default:
return state;
Expand Down
67 changes: 37 additions & 30 deletions src/services/bookstore-service.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
export default class BookStoreService {

data = [
{
id: 1,
title: 'Production-Ready Microservices',
author: 'Susan J. Fowler',
price: 31.49,
coverImage: 'https://images-na.ssl-images-amazon.com/images/I/51oxXEG6TRL._SX379_BO1,204,203,200_.jpg'
},
{
id: 2,
title: 'Release It!',
author: 'Michael T. Nygard',
price: 29.40,
coverImage: 'https://images-na.ssl-images-amazon.com/images/I/81c+o9-DetL.jpg'
},
{
id: 3,
title: 'The Ultimate Book of Vehicles',
author: 'Anne-Sophie Baumann, Didier Balicevic',
price: 15.69,
coverImage: 'https://images-na.ssl-images-amazon.com/images/I/51JXljxwF5L._SX379_BO1,204,203,200_.jpg'
},
{
id: 4,
title: 'The Ultimate Book of Planet Earth',
author: 'Anne-Sophie Baumann, Didier Balicevic',
price: 18.69,
coverImage: 'https://images-na.ssl-images-amazon.com/images/I/51yOh1rE3bL._SX380_BO1,204,203,200_.jpg'
}
];

getBooks() {
return [
{
id: 1,
title: 'Production-Ready Microservices',
author: 'Susan J. Fowler',
price: 31.49,
coverImage: 'https://images-na.ssl-images-amazon.com/images/I/51oxXEG6TRL._SX379_BO1,204,203,200_.jpg'
},
{
id: 2,
title: 'Release It!',
author: 'Michael T. Nygard',
price: 29.40,
coverImage: 'https://images-na.ssl-images-amazon.com/images/I/81c+o9-DetL.jpg'
},
{
id: 3,
title: 'The Ultimate Book of Vehicles',
author: 'Anne-Sophie Baumann, Didier Balicevic',
price: 15.69,
coverImage: 'https://images-na.ssl-images-amazon.com/images/I/51JXljxwF5L._SX379_BO1,204,203,200_.jpg'
},
{
id: 4,
title: 'The Ultimate Book of Planet Earth',
author: 'Anne-Sophie Baumann, Didier Balicevic',
price: 18.69,
coverImage: 'https://images-na.ssl-images-amazon.com/images/I/51yOh1rE3bL._SX380_BO1,204,203,200_.jpg'
}
];
return new Promise((resolve) => {
setTimeout(() => {
resolve(this.data)
}, 700);
})
}
};

0 comments on commit 65676b0

Please sign in to comment.