Skip to content

Commit

Permalink
Making a Leaner Switch Case by Refactoring Code in REDUCERS
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch-sriram committed Sep 1, 2020
1 parent 1a6d06b commit e85c53e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 59 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,4 @@ Dependency Installation: **`npm i --save redux-thunk`**
14. Handling Purchases & Updating the UI: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/c42f5f9d6b1cc7c7d948028e34bd21f64eecd8c6)
15. Resetting the `totalPrice` after Purchases: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/ddc8161cff3e022fa088e0f5cc42b2b06ea6caa4)
16. Fetching Orders via Redux & Refactoring the **`REDUCERS`** using Custom Utility Function(s): [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/e39ea9c1d7c92ca92cc3639ac6be175c904e87c8)
17. Making a Leaner Switch Case by Refactoring Code in **`REDUCERS`**: [Commit Details]()
80 changes: 43 additions & 37 deletions src/store/reducers/burgerBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,51 @@ const INGREDIENT_PRICES = {
bacon: 0.7,
};

const ADD = true, SUBTRACT = false;

const modifyIngredient = (state, action, modifier) => {
const updatedIngredient = {
[action.ingredientName]:
modifier === ADD
? state.ingredients[action.ingredientName] + 1
: state.ingredients[action.ingredientName] - 1,
};
const updatedIngredients = updateObject(state.ingredients, updatedIngredient);
const updatedState = {
ingredients: updatedIngredients,
totalPrice:
modifier === ADD
? state.totalPrice + INGREDIENT_PRICES[action.ingredientName]
: state.totalPrice - INGREDIENT_PRICES[action.ingredientName],
};
return updateObject(state, updatedState);
};

const setIngredients = (state, action) => {
let price = initialState.totalPrice;
for (let ingredient of Object.entries(action.ingredients))
price += INGREDIENT_PRICES[ingredient[0]] * ingredient[1];

return updateObject(state, {
ingredients: {
salad: action.ingredients.salad,
bacon: action.ingredients.bacon,
cheese: action.ingredients.cheese,
meat: action.ingredients.meat,
},
totalPrice: price,
error: false,
});
}

const fetchIngredientsFailed = (state, action) => updateObject(state, { error: true, });

const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.ADD_INGREDIENT:
const updatedIngredient = { [action.ingredientName]: state.ingredients[action.ingredientName] + 1 };
const updatedIngredients = updateObject(state.ingredients, updatedIngredient);
const updatedState = {
ingredients: updatedIngredients,
totalPrice: state.totalPrice + INGREDIENT_PRICES[action.ingredientName],
};
return updateObject(state, updatedState);

case actionTypes.REMOVE_INGREDIENT:
const updatedIng = { [action.ingredientName]: state.ingredients[action.ingredientName] - 1 };
const updatedIngs = updateObject(state.ingredients, updatedIng);
const updated_State = {
ingredients: updatedIngs,
totalPrice: state.totalPrice - INGREDIENT_PRICES[action.ingredientName],
};
return updateObject(state, updated_State);

case actionTypes.SET_INGREDIENTS:
let price = initialState.totalPrice;
for (let ingredient of Object.entries(action.ingredients)) {
price += INGREDIENT_PRICES[ingredient[0]] * ingredient[1];
}
return updateObject(state, {
ingredients: {
salad: action.ingredients.salad,
bacon: action.ingredients.bacon,
cheese: action.ingredients.cheese,
meat: action.ingredients.meat,
},
totalPrice: price,
error: false,
});

case actionTypes.FETCH_INGREDIENTS_FAILED:
return updatedState(state, { error: true, });

case actionTypes.ADD_INGREDIENT: return modifyIngredient(state, action, ADD);
case actionTypes.REMOVE_INGREDIENT: return modifyIngredient(state, action, SUBTRACT);
case actionTypes.SET_INGREDIENTS: return setIngredients(state, action);
case actionTypes.FETCH_INGREDIENTS_FAILED: return fetchIngredientsFailed(state, action);
default: return state;
}
};
Expand Down
46 changes: 24 additions & 22 deletions src/store/reducers/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@ const initialState = {
purchased: false,
};

const purchaseInit = (state, action) => updateObject(state, { purchased: false, });
const purchaseBurgerStart = (state, action) => updateObject(state, { loading: true, });

const purchaseBurgerSuccess = (state, action) => {
const newOrder = updateObject(action.orderData, { id: action.orderID, });
return updateObject(state, {
loading: false,
purchased: true,
orders: state.orders.concat(newOrder),
});
};

const purchaseBurgerFail = (state, action) => updateObject(state, { loading: false, });
const fetchOrdersStart = (state, action) => updateObject(state, { loading: true, });
const fetchOrdersSuccess = (state, action) => updateObject(state, { orders: action.orders, loading: false, });
const fetchOrdersFail = (state, action) => updateObject(state, { loading: false, });

const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.PURCHASE_INIT:
return updateObject(state, { purchased: false, });
case actionTypes.PURCHASE_BURGER_START:
return updateObject(state, { loading: true, });
case actionTypes.PURCHASE_BURGER_SUCCESS:
const newOrder = updateObject(action.orderData, { id: action.orderID, });
return updateObject(state, {
loading: false,
purchased: true,
orders: state.orders.concat(newOrder),
});
case actionTypes.PURCHASE_BURGER_FAIL:
return updateObject(state, { loading: false, });
case actionTypes.FETCH_ORDERS_START:
return updateObject(state, { loading: true, });
case actionTypes.FETCH_ORDERS_SUCCESS:
return updateObject(state, {
orders: action.orders,
loading: false,
});
case actionTypes.FETCH_ORDERS_FAIL:
return updateObject(state, { loading: false, });
case actionTypes.PURCHASE_INIT: return purchaseInit(state, action);
case actionTypes.PURCHASE_BURGER_START: return purchaseBurgerStart(state, action);
case actionTypes.PURCHASE_BURGER_SUCCESS: return purchaseBurgerSuccess(state, action);
case actionTypes.PURCHASE_BURGER_FAIL: return purchaseBurgerFail(state, action);
case actionTypes.FETCH_ORDERS_START: return fetchOrdersStart(state, action);
case actionTypes.FETCH_ORDERS_SUCCESS: return fetchOrdersSuccess(state, action);
case actionTypes.FETCH_ORDERS_FAIL: return fetchOrdersFail(state, action);
default: return state;
}
};
Expand Down

0 comments on commit e85c53e

Please sign in to comment.