Skip to main content

15.04 Finishing Reducer For Ingredients

BurgerBuilder.js

BurgerBuilder.js
  ...
state = {
ingredients: null,
totalPrice: 4,
purchasable: false,
purchasing: false,
loading: false,
error: false,
};

componentDidMount() {
console.log(this.props);
// axios
// .get(
// "https://react-burger-bf7e8-default-rtdb.europe-west1.firebasedatabase.app/ingregients.json"
// )
// .then((response) => {
// this.setState({ ingredients: response.data });
// })
// .catch((error) => {
// this.setState({ error: true });
// });
}

updatePurchaseState

...

reducer.js

reducer.js
import * as actionTypes from "./actions";

const initialState = {
ingredients: {
salad: 0,
bacon: 0,
cheese: 0,
meat: 0,
},
totalPrice: 4,
};

const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.ADD_INGREDIENT:
return {
...state,
ingredients: {
...state.ingredients,
[action.ingredientName]: state.ingrediens[action.ingredientName] + 1,
},
};
case actionTypes.REMOVE_INGREDIENT:
return {
...state,
ingredients: {
...state.ingredients,
[action.ingredientName]: state.ingrediens[action.ingredientName] - 1,
},
};
default:
return state;
}
};

export default reducer;