|
1 | 1 | import shop from '../api/shop' |
2 | 2 | import * as types from '../constants/ActionTypes' |
3 | 3 |
|
4 | | -function receiveProducts(products) { |
5 | | - return { |
6 | | - type: types.RECEIVE_PRODUCTS, |
7 | | - products: products |
8 | | - } |
9 | | -} |
| 4 | +const receiveProducts = products => ({ |
| 5 | + type: types.RECEIVE_PRODUCTS, |
| 6 | + products: products |
| 7 | +}) |
10 | 8 |
|
11 | | -export function getAllProducts() { |
12 | | - return dispatch => { |
13 | | - shop.getProducts(products => { |
14 | | - dispatch(receiveProducts(products)) |
15 | | - }) |
16 | | - } |
17 | | -} |
| 9 | +export const getAllProducts = () => dispatch => shop.getProducts(products => { |
| 10 | + dispatch(receiveProducts(products)) |
| 11 | +}) |
18 | 12 |
|
19 | | -function addToCartUnsafe(productId) { |
20 | | - return { |
21 | | - type: types.ADD_TO_CART, |
22 | | - productId |
23 | | - } |
24 | | -} |
| 13 | +const addToCartUnsafe = productId => ({ |
| 14 | + type: types.ADD_TO_CART, |
| 15 | + productId |
| 16 | +}) |
25 | 17 |
|
26 | | -export function addToCart(productId) { |
27 | | - return (dispatch, getState) => { |
28 | | - if (getState().products.byId[productId].inventory > 0) { |
29 | | - dispatch(addToCartUnsafe(productId)) |
30 | | - } |
| 18 | +export const addToCart = productId => (dispatch, getState) => { |
| 19 | + if (getState().products.byId[productId].inventory > 0) { |
| 20 | + dispatch(addToCartUnsafe(productId)) |
31 | 21 | } |
32 | 22 | } |
33 | 23 |
|
34 | | -export function checkout(products) { |
35 | | - return (dispatch, getState) => { |
36 | | - const cart = getState().cart |
| 24 | +export const checkout = products => (dispatch, getState) => { |
| 25 | + const { cart } = getState() |
37 | 26 |
|
| 27 | + dispatch({ |
| 28 | + type: types.CHECKOUT_REQUEST |
| 29 | + }) |
| 30 | + shop.buyProducts(products, () => { |
38 | 31 | dispatch({ |
39 | | - type: types.CHECKOUT_REQUEST |
| 32 | + type: types.CHECKOUT_SUCCESS, |
| 33 | + cart |
40 | 34 | }) |
41 | | - shop.buyProducts(products, () => { |
42 | | - dispatch({ |
43 | | - type: types.CHECKOUT_SUCCESS, |
44 | | - cart |
45 | | - }) |
46 | | - // Replace the line above with line below to rollback on failure: |
47 | | - // dispatch({ type: types.CHECKOUT_FAILURE, cart }) |
48 | | - }) |
49 | | - } |
| 35 | + // Replace the line above with line below to rollback on failure: |
| 36 | + // dispatch({ type: types.CHECKOUT_FAILURE, cart }) |
| 37 | + }) |
50 | 38 | } |
0 commit comments