1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/**
@license
Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
export const GET_PRODUCTS = 'GET_PRODUCTS';
export const ADD_TO_CART = 'ADD_TO_CART';
export const REMOVE_FROM_CART = 'REMOVE_FROM_CART';
export const CHECKOUT_SUCCESS = 'CHECKOUT_SUCCESS';
export const CHECKOUT_FAILURE = 'CHECKOUT_FAILURE';
const PRODUCT_LIST = [
{"id": 1, "title": "Cabot Creamery Extra Sharp Cheddar Cheese", "price": 10.99, "inventory": 2},
{"id": 2, "title": "Cowgirl Creamery Mt. Tam Cheese", "price": 29.99, "inventory": 10},
{"id": 3, "title": "Tillamook Medium Cheddar Cheese", "price": 8.99, "inventory": 5},
{"id": 4, "title": "Point Reyes Bay Blue Cheese", "price": 24.99, "inventory": 7},
{"id": 5, "title": "Shepherd's Halloumi Cheese", "price": 11.99, "inventory": 3}
];
export const getAllProducts = () => (dispatch, getState) => {
// Here you would normally get the data from the server. We're simulating
// that by dispatching an async action (that you would dispatch when you
// succesfully got the data back)
// You could reformat the data in the right format as well:
const products = PRODUCT_LIST.reduce((obj, product) => {
obj[product.id] = product
return obj
}, {});
dispatch({
type: GET_PRODUCTS,
products: products
});
};
export const checkout = (productId) => (dispatch) => {
// Here you could do things like credit card validation, etc.
// If that fails, dispatch CHECKOUT_FAILURE. We're simulating that
// by flipping a coin :)
const flip = Math.floor(Math.random() * 2);
if (flip === 0) {
dispatch({
type: CHECKOUT_FAILURE
});
} else {
dispatch({
type: CHECKOUT_SUCCESS
});
}
};
export const addToCart = (productId) => (dispatch, getState) =>{
const state = getState();
// Just because the UI thinks you can add this to the cart
// doesn't mean it's in the inventory (user could've fixed it);
if (state.shop.products[productId].inventory > 0) {
dispatch(addToCartUnsafe(productId));
}
};
export const removeFromCart = (productId) => {
return {
type: REMOVE_FROM_CART,
productId
};
};
export const addToCartUnsafe = (productId) => {
return {
type: ADD_TO_CART,
productId
};
};
|