Skip to content

Commit 8744280

Browse files
refactor: update shoppingCartReducer with switch statement
1 parent cd8ee42 commit 8744280

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/store/shopping-cart-context.jsx

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,66 @@ import { DUMMY_PRODUCTS } from '../dummy-products.js';
55
export const ShoppingCartContext = createContext();
66

77
function shoppingCartReducer(state, action) {
8-
if (action.type === 'ADD_ITEM') {
9-
const updatedItems = [...state.items];
8+
switch (action.type) {
9+
case 'ADD_ITEM': {
10+
const updatedItems = [...state.items];
1011

11-
const existingCartItemIndex = updatedItems.findIndex(
12-
(cartItem) => cartItem.id === action.payload
13-
);
14-
const existingCartItem = updatedItems[existingCartItemIndex];
12+
const existingCartItemIndex = updatedItems.findIndex(
13+
(cartItem) => cartItem.id === action.payload
14+
);
15+
const existingCartItem = updatedItems[existingCartItemIndex];
1516

16-
if (existingCartItem) {
17-
const updatedItem = {
18-
...existingCartItem,
19-
quantity: existingCartItem.quantity + 1,
17+
if (existingCartItem) {
18+
const updatedItem = {
19+
...existingCartItem,
20+
quantity: existingCartItem.quantity + 1,
21+
};
22+
updatedItems[existingCartItemIndex] = updatedItem;
23+
} else {
24+
const product = DUMMY_PRODUCTS.find(
25+
(product) => product.id === action.payload
26+
);
27+
updatedItems.push({
28+
id: action.payload,
29+
name: product.title,
30+
price: product.price,
31+
quantity: 1,
32+
});
33+
}
34+
35+
return {
36+
...state, // not needed here because we have only one value
37+
items: updatedItems,
2038
};
21-
updatedItems[existingCartItemIndex] = updatedItem;
22-
} else {
23-
const product = DUMMY_PRODUCTS.find(
24-
(product) => product.id === action.payload
25-
);
26-
updatedItems.push({
27-
id: action.payload,
28-
name: product.title,
29-
price: product.price,
30-
quantity: 1,
31-
});
3239
}
3340

34-
return {
35-
...state, // not needed here because we have only one value
36-
items: updatedItems,
37-
};
38-
}
41+
case 'UPDATE_ITEM': {
42+
const updatedItems = [...state.items];
43+
const updatedItemIndex = updatedItems.findIndex(
44+
(item) => item.id === action.payload.productId
45+
);
3946

40-
if (action.type === 'UPDATE_ITEM') {
41-
const updatedItems = [...state.items];
42-
const updatedItemIndex = updatedItems.findIndex(
43-
(item) => item.id === action.payload.productId
44-
);
47+
const updatedItem = {
48+
...updatedItems[updatedItemIndex],
49+
};
4550

46-
const updatedItem = {
47-
...updatedItems[updatedItemIndex],
48-
};
51+
updatedItem.quantity += action.payload.amount;
4952

50-
updatedItem.quantity += action.payload.amount;
53+
if (updatedItem.quantity <= 0) {
54+
updatedItems.splice(updatedItemIndex, 1);
55+
} else {
56+
updatedItems[updatedItemIndex] = updatedItem;
57+
}
5158

52-
if (updatedItem.quantity <= 0) {
53-
updatedItems.splice(updatedItemIndex, 1);
54-
} else {
55-
updatedItems[updatedItemIndex] = updatedItem;
59+
return {
60+
...state,
61+
items: updatedItems,
62+
};
63+
}
64+
default: {
65+
throw new Error(`Unhandled action type: ${action.type}`);
5666
}
57-
58-
return {
59-
...state,
60-
items: updatedItems,
61-
};
6267
}
63-
64-
return state;
6568
}
6669

6770
export default function ShoppingCartProvider({ children }) {

0 commit comments

Comments
 (0)