Skip to content

Commit

Permalink
Working on the totalPrice's state in Redux STORE
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch-sriram committed Aug 25, 2020
1 parent 5154a16 commit 0ef7cae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 78 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,4 @@ Firebase Common API Endpoint: <https://burger-builder-ram.firebaseio.com/>
2. Connecting the Redux **`STORE`** to the `<App />` Component: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/257c8bc90ad26b59efa9fa0f0020b293544825b3)
3. Finishing the **`REDUCER`** for Ingredients: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/45a0123a382061ed1c7807bb39dc54c3f514b515)
4. Connecting the `<BurgerBuilder />` Container to Redux `**STORE**`: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/3425425c5be98d658f59f98d81296bb3724de573)
5. Working on the `totalPrice`'s state in Redux STORE: [Commit Details]()
102 changes: 26 additions & 76 deletions src/containers/BurgerBuilder/BurgerBuilder.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@ import withErrorHandler from '../../hoc/withErrorHandler/withErrorHandler.closur
import axios from "../../axios-orders";
import * as actionTypes from "../../store/actions";

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

class BurgerBuilder extends Component {
state = {
totalPrice: 4,
purchasable: false,
orderNow: false,
loading: false,
Expand All @@ -37,27 +29,27 @@ class BurgerBuilder extends Component {
* NOTE: we have to append `.json` at the end of the API
* endpoint in case of firebase db.
*/
componentDidMount() {
axios.get("https://burger-builder-ram.firebaseio.com/ingredients.json")
.then(response => {
this.setState({ ingredients: response.data },
() => {
const purchasableInfo = { ...this.state.ingredients };
for (let key in purchasableInfo) {
if (this.state.purchasable) {
break;
}
if (purchasableInfo[key] > 0) {
this.setState({ purchasable: true });
}
}
});
})
.catch(error => {
this.setState({ error: true }, () => console.log(error));
return error;
});
}
// componentDidMount() {
// axios.get("https://burger-builder-ram.firebaseio.com/ingredients.json")
// .then(response => {
// this.setState({ ingredients: response.data },
// () => {
// const purchasableInfo = { ...this.state.ingredients };
// for (let key in purchasableInfo) {
// if (this.state.purchasable) {
// break;
// }
// if (purchasableInfo[key] > 0) {
// this.setState({ purchasable: true });
// }
// }
// });
// })
// .catch(error => {
// this.setState({ error: true }, () => console.log(error));
// return error;
// });
// }

updatePurchasableState() {
const ingredients = { ...this.state.ingredients };
Expand All @@ -67,49 +59,6 @@ class BurgerBuilder extends Component {
this.setState({ purchasable: sum > 0 });
}

addIngredientHandler = (type) => {
const oldCount = this.state.ingredients[type];
const updatedCount = oldCount + 1;
const updatedIngredients = { ...this.state.ingredients };
updatedIngredients[type] = updatedCount;

const priceAddition = INGREDIENT_PRICES[type];
const oldPrice = this.state.totalPrice;
const newPrice = oldPrice + priceAddition;

this.setState(
{
totalPrice: newPrice,
ingredients: updatedIngredients,
},
() => {
this.updatePurchasableState();
}
);
};

removeIngredientHandler = (type) => {
const oldCount = this.state.ingredients[type];
if (oldCount <= 0) return;
const updatedCount = oldCount - 1;
const updatedIngredients = { ...this.state.ingredients };
updatedIngredients[type] = updatedCount;

const priceDeduction = INGREDIENT_PRICES[type];
const oldPrice = this.state.totalPrice;
const newPrice = oldPrice - priceDeduction;

this.setState(
{
totalPrice: newPrice,
ingredients: updatedIngredients,
},
() => {
this.updatePurchasableState();
}
);
};

orderNowHandler = () => {
this.setState({ orderNow: true });
};
Expand Down Expand Up @@ -142,7 +91,7 @@ class BurgerBuilder extends Component {
let orderSummary = null,
burger = this.state.error ? <p style={{textAlign: 'center'}}>Ingredients can't be loaded!</p> : <Spinner />;

if (this.state.ingredients) {
if (this.props.ings) {
burger = (
<Wrapper>
<Burger ingredients={this.props.ings} />
Expand All @@ -151,7 +100,7 @@ class BurgerBuilder extends Component {
ingredientRemoved={this.props.onIngredientRemoved}
disabled={disabledInfo}
purchasable={this.state.purchasable}
price={this.state.totalPrice}
price={this.props.price}
ordered={this.orderNowHandler}
/>
</Wrapper>
Expand All @@ -160,7 +109,7 @@ class BurgerBuilder extends Component {
orderSummary = (
<OrderSummary
ingredients={this.props.ings}
price={this.state.totalPrice}
price={this.props.price}
orderCancelled={this.orderCancelHandler}
orderContinued={this.orderContinueHandler}
/>
Expand All @@ -183,7 +132,8 @@ class BurgerBuilder extends Component {
// REDUX STORE SETUP
const mapStateToProps = state => {
return {
ings: state.ingredients
ings: state.ingredients,
price: state.totalPrice,
}
};

Expand Down
18 changes: 16 additions & 2 deletions src/store/reducer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import * as actionTypes from "./actions";

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

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:
Expand All @@ -13,7 +25,8 @@ const reducer = (state = initialState, action) => {
ingredients: {
...state.ingredients,
[action.ingredientName]: state.ingredients[action.ingredientName] + 1,
}
},
totalPrice: state.totalPrice + INGREDIENT_PRICES[action.ingredientName],
};

case actionTypes.REMOVE_INGREDIENT:
Expand All @@ -23,6 +36,7 @@ const reducer = (state = initialState, action) => {
...state.ingredients,
[action.ingredientName]: state.ingredients[action.ingredientName] - 1,
},
totalPrice: state.totalPrice - INGREDIENT_PRICES[action.ingredientName],
};

default: return state;
Expand Down

0 comments on commit 0ef7cae

Please sign in to comment.