Skip to main content

17.19 Refactoring Reducers

reducers/burgerBuilder.js

src\store\reducers\burgerBuilder.js
import * as actionTypes from "../actions/actionTypes";
import { updateObject } from "../utility";

const initialState = {
ingredients: null,
totalPrice: 4,
error: false,
};

const INGREDIENT_PRICES = {
salad: 0.5,
cheese: 0.4,
meat: 1.3,
bacon: 0.7,
};

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 updatedSt = {
ingredients: updatedIngs,
totalPrice: state.totalPrice + INGREDIENT_PRICES[action.ingredientName],
};
return updateObject(state, updatedSt);
case actionTypes.SET_INGREDIENTS:
return updateObject(state, {
ingredients: {
salad: action.ingredients.salad,
bacon: action.ingredients.bacon,
cheese: action.ingredients.cheese,
meat: action.ingredients.meat,
},
totalPrice: 4,
error: false,
});
case actionTypes.FETCH_INGREDIENTS_FAILED:
return updateObject(state, { error: true });
default:
return state;
}
};

export default reducer;

reducers/order.js

src\store\reducers\order.js
import * as actionTypes from "../actions/actionTypes";
import { updateObject } from "../utility";

const initialState = {
orders: [],
loading: false,
purchased: 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 });
default:
return state;
}
};

export default reducer;

New utility.js

src\store\utility.js
export const updateObject = (oldObject, updatedProperties) => {
return {
...oldObject,
...updatedProperties,
};
};