-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1947 from ChrisJamesC/cc-shopping-cart
Use more ES6 syntaxes in the shopping cart example
- Loading branch information
Showing
16 changed files
with
136 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,38 @@ | ||
import shop from '../api/shop' | ||
import * as types from '../constants/ActionTypes' | ||
|
||
function receiveProducts(products) { | ||
return { | ||
type: types.RECEIVE_PRODUCTS, | ||
products: products | ||
} | ||
} | ||
const receiveProducts = products => ({ | ||
type: types.RECEIVE_PRODUCTS, | ||
products: products | ||
}) | ||
|
||
export function getAllProducts() { | ||
return dispatch => { | ||
shop.getProducts(products => { | ||
dispatch(receiveProducts(products)) | ||
}) | ||
} | ||
} | ||
export const getAllProducts = () => dispatch => shop.getProducts(products => { | ||
dispatch(receiveProducts(products)) | ||
}) | ||
|
||
function addToCartUnsafe(productId) { | ||
return { | ||
type: types.ADD_TO_CART, | ||
productId | ||
} | ||
} | ||
const addToCartUnsafe = productId => ({ | ||
type: types.ADD_TO_CART, | ||
productId | ||
}) | ||
|
||
export function addToCart(productId) { | ||
return (dispatch, getState) => { | ||
if (getState().products.byId[productId].inventory > 0) { | ||
dispatch(addToCartUnsafe(productId)) | ||
} | ||
export const addToCart = productId => (dispatch, getState) => { | ||
if (getState().products.byId[productId].inventory > 0) { | ||
dispatch(addToCartUnsafe(productId)) | ||
} | ||
} | ||
|
||
export function checkout(products) { | ||
return (dispatch, getState) => { | ||
const cart = getState().cart | ||
export const checkout = products => (dispatch, getState) => { | ||
const { cart } = getState() | ||
|
||
dispatch({ | ||
type: types.CHECKOUT_REQUEST | ||
}) | ||
shop.buyProducts(products, () => { | ||
dispatch({ | ||
type: types.CHECKOUT_REQUEST | ||
type: types.CHECKOUT_SUCCESS, | ||
cart | ||
}) | ||
shop.buyProducts(products, () => { | ||
dispatch({ | ||
type: types.CHECKOUT_SUCCESS, | ||
cart | ||
}) | ||
// Replace the line above with line below to rollback on failure: | ||
// dispatch({ type: types.CHECKOUT_FAILURE, cart }) | ||
}) | ||
} | ||
// Replace the line above with line below to rollback on failure: | ||
// dispatch({ type: types.CHECKOUT_FAILURE, cart }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,33 @@ | ||
import React, { Component, PropTypes } from 'react' | ||
import React, { PropTypes } from 'react' | ||
import Product from './Product' | ||
|
||
export default class Cart extends Component { | ||
render() { | ||
const { products, total, onCheckoutClicked } = this.props | ||
const Cart = ({ products, total, onCheckoutClicked }) => { | ||
const hasProducts = products.length > 0 | ||
const nodes = !hasProducts ? | ||
<em>Please add some products to cart.</em> : | ||
products.map(product => | ||
<Product | ||
title={product.title} | ||
price={product.price} | ||
quantity={product.quantity} | ||
key={product.id}/> | ||
) | ||
|
||
const hasProducts = products.length > 0 | ||
const nodes = !hasProducts ? | ||
<em>Please add some products to cart.</em> : | ||
products.map(product => | ||
<Product | ||
title={product.title} | ||
price={product.price} | ||
quantity={product.quantity} | ||
key={product.id}/> | ||
) | ||
|
||
return ( | ||
<div> | ||
<h3>Your Cart</h3> | ||
<div>{nodes}</div> | ||
<p>Total: ${total}</p> | ||
<button onClick={onCheckoutClicked} | ||
disabled={hasProducts ? '' : 'disabled'}> | ||
Checkout | ||
</button> | ||
</div> | ||
) | ||
} | ||
return <div> | ||
<h3>Your Cart</h3> | ||
<div>{nodes}</div> | ||
<p>Total: ${total}</p> | ||
<button onClick={onCheckoutClicked} | ||
disabled={hasProducts ? '' : 'disabled'}> | ||
Checkout | ||
</button> | ||
</div> | ||
} | ||
|
||
Cart.propTypes = { | ||
products: PropTypes.array, | ||
total: PropTypes.string, | ||
onCheckoutClicked: PropTypes.func | ||
} | ||
|
||
export default Cart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import React, { Component, PropTypes } from 'react' | ||
import React, { PropTypes } from 'react' | ||
|
||
export default class Product extends Component { | ||
render() { | ||
const { price, quantity, title } = this.props | ||
return <div> {title} - ${price} {quantity ? `x ${quantity}` : null} </div> | ||
} | ||
} | ||
const Product = ({ price, quantity, title }) => | ||
<div> {title} - ${price} {quantity ? `x ${quantity}` : null} </div> | ||
|
||
Product.propTypes = { | ||
price: PropTypes.number, | ||
quantity: PropTypes.number, | ||
title: PropTypes.string | ||
} | ||
|
||
export default Product |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
import React, { Component, PropTypes } from 'react' | ||
import React, { PropTypes } from 'react' | ||
|
||
export default class ProductsList extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<h3>{this.props.title}</h3> | ||
<div>{this.props.children}</div> | ||
</div> | ||
) | ||
} | ||
} | ||
const ProductsList = ({title, children}) => <div> | ||
<h3>{title}</h3> | ||
<div>{children}</div> | ||
</div> | ||
|
||
ProductsList.propTypes = { | ||
children: PropTypes.node, | ||
title: PropTypes.string.isRequired | ||
} | ||
|
||
export default ProductsList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
import React, { Component } from 'react' | ||
import React from 'react' | ||
import ProductsContainer from './ProductsContainer' | ||
import CartContainer from './CartContainer' | ||
|
||
export default class App extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<h2>Shopping Cart Example</h2> | ||
<hr/> | ||
<ProductsContainer /> | ||
<hr/> | ||
<CartContainer /> | ||
</div> | ||
) | ||
} | ||
} | ||
const App = () => <div> | ||
<h2>Shopping Cart Example</h2> | ||
<hr/> | ||
<ProductsContainer /> | ||
<hr/> | ||
<CartContainer /> | ||
</div> | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.