Skip to main content

17.20 Refactoring Reducers Continued

burgerBuilders.js

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

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

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

const addIngredient = (state, action) => {
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);
};

const removeIngredient = (state, action) => {
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);
};

const setIngredients = (state, action) => {
return updateObject(state, {
ingredients: {
salad: action.ingredients.salad,
bacon: action.ingredients.bacon,
cheese: action.ingredients.cheese,
meat: action.ingredients.meat,
},
totalPrice: 4,
error: false,
});
};

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

const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.ADD_INGREDIENT:
return addIngredient(state, action);

case actionTypes.REMOVE_INGREDIENT:
return removeIngredient(state, action);

case actionTypes.SET_INGREDIENTS:
return setIngredients(state, action);

case actionTypes.FETCH_INGREDIENTS_FAILED:
return fetchIngredientsFailed(state, action);

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 purchaseInit = (state, action) => {
return updateObject(state, { purchased: false });
};

const purchaseBurgerStart = (state, action) => {
return 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) => {
return updateObject(state, { loading: false });
};

const fetchOrderStart = (state, action) => {
return updateObject(state, { loading: true });
};

const fetchOrderSuccess = (state, action) => {
return updateObject(state, { orders: action.orders, loading: false });
};

const fetchOrdersFail = (state, action) => {
return updateObject(state, { loading: false });
};

const reducer = (state = initialState, action) => {
switch (action.type) {
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 fetchOrderStart(state, action);
case actionTypes.FETCH_ORDERS_SUCCESS:
return fetchOrderSuccess(state, action);
case actionTypes.FETCH_ORDERS_FAIL:
return fetchOrdersFail(state, action);
default:
return state;
}
};

export default reducer;