Skip to content

Commit b3f1c16

Browse files
committed
Refactor selector colocation for better readability
1 parent 63d7e27 commit b3f1c16

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

examples/shopping-cart/reducers/index.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,37 @@ import { combineReducers } from 'redux'
22
import cart, * as fromCart from './cart'
33
import products, * as fromProducts from './products'
44

5+
export default combineReducers({
6+
cart,
7+
products
8+
})
9+
10+
function getAddedIds(state) {
11+
return fromCart.getAddedIds(state.cart)
12+
}
13+
14+
function getQuantity(state, id) {
15+
return fromCart.getQuantity(state.cart, id)
16+
}
17+
18+
function getProduct(state, id) {
19+
return fromProducts.getProduct(state.products, id)
20+
}
21+
522
export function getTotal(state) {
6-
return fromCart.getAddedIds(state.cart).reduce((total, id) =>
7-
total + (
8-
fromProducts.getProduct(state.products, id).price *
9-
fromCart.getQuantity(state.cart, id)
10-
),
23+
return getAddedIds(state).reduce((total, id) =>
24+
total + getProduct(state, id).price * getQuantity(state, id),
1125
0
1226
).toFixed(2)
1327
}
1428

1529
export function getCartProducts(state) {
16-
return fromCart.getAddedIds(state.cart).map(id => Object.assign(
30+
return getAddedIds(state).map(id => Object.assign(
1731
{},
18-
fromProducts.getProduct(state.products, id),
32+
getProduct(state, id),
1933
{
20-
quantity: fromCart.getQuantity(state.cart, id)
34+
quantity: getQuantity(state, id)
2135
}
2236
))
2337
}
2438

25-
export default combineReducers({
26-
cart,
27-
products
28-
})

0 commit comments

Comments
 (0)