-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Use more ES6 syntaxes in the shopping cart example #1947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d41070
Merge remote-tracking branch 'reactjs/master'
ChrisJamesC edc18fe
Merge remote-tracking branch 'reactjs/master'
ChrisJamesC a70be32
Merge remote-tracking branch 'reactjs/master' into cc-shopping-cart
ChrisJamesC c4e6c32
Use more ES6 syntaxes in the shopping cart example
ChrisJamesC 35f68af
Use object assignation for getState()
ChrisJamesC c813282
:green_heart: fix tests failing because of missing spaces
ChrisJamesC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I strongly dislike this style. Can we please do a pass in a separate PR that ensures returned multilne JSX is always wrapped in parens? As in:
and