-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.js
40 lines (32 loc) · 1.05 KB
/
index.js
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
import { combineReducers } from 'redux'
import { ADD_TO_CART } from '../actions'
import { default as cart, getQuantity, getAddedIds } from './cart'
import { default as products, getProduct } from './products'
export function getCart(state) {
return state.cart
}
export function getCheckoutError(state) {
return state.cart.checkoutStatus.error
}
export function isCheckoutPending(state) {
return state.cart.checkoutStatus.checkoutPending
}
export function getTotal(state) {
return getAddedIds(state.cart)
.reduce((total, id) => total + getProduct(state.products, id).price * getQuantity(state.cart, id), 0)
.toFixed(2)
}
export function getCartProducts(state) {
return getAddedIds(state.cart).map((id) => ({
...getProduct(state.products, id),
quantity: getQuantity(state.cart, id),
}))
}
const shoppingCart = combineReducers({
cart,
products,
})
export default function root(state, action) {
if (action.type === ADD_TO_CART && state.products.byId[action.productId].inventory <= 0) return state
return shoppingCart(state, action)
}