Advertisement
Guest User

Untitled

a guest
Apr 7th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import Auxilialry from "../../hoc/Auxiliary";
  3. import Bruger from "../../components/Burger/Bruger";
  4. import BuildControls from "../Burger/BuildControls/BuildControls";
  5.  
  6. const INGRIDIENT_PRICES = {
  7.   salad: 0.5,
  8.   cheese: 0.4,
  9.   meat: 1.3,
  10.   bacon: 0.7
  11. };
  12.  
  13. class BurgerBuilder extends Component {
  14.   state = {
  15.     ingredients: {
  16.       salad: 0,
  17.       bacon: 0,
  18.       cheese: 0,
  19.       meat: 0
  20.     },
  21.     totalPrice: 4,
  22.     purchasable: false
  23.   };
  24.  
  25.   updatePurchaseState() {
  26.     const ingredients = {
  27.       ...this.state.ingredients
  28.     };
  29.     const sum = Object.keys(ingredients)
  30.       .map(igKey => {
  31.         return ingredients[igKey];
  32.       })
  33.       .reduce((sum, el) => {
  34.         return sum + el;
  35.       }, 0);
  36.     this.setState({ purchasable: sum > 0 });
  37.   }
  38.  
  39.   addIngridientHandler = type => {
  40.     const oldCount = this.state.ingredients[type];
  41.     if (oldCount >= 5) {
  42.       return;
  43.     }
  44.     const updatedCount = oldCount + 1;
  45.     const updatedIngridients = {
  46.       ...this.state.ingredients
  47.     };
  48.     let additionPrice = 0;
  49.     updatedIngridients[type] = updatedCount;
  50.     additionPrice += INGRIDIENT_PRICES[type];
  51.  
  52.     const newPrice = this.state.totalPrice + additionPrice;
  53.     this.setState({
  54.       totalPrice: newPrice,
  55.       ingredients: updatedIngridients
  56.     },()=>this.updatePurchaseState());
  57. };
  58.  
  59.   removeIngridientHandler = type => {
  60.     const oldCount = this.state.ingredients[type];
  61.     if (oldCount <= 0) {
  62.       return;
  63.     }
  64.     const updatedCount = oldCount - 1;
  65.     const updatedIngridients = {
  66.       ...this.state.ingredients
  67.     };
  68.     let fewerPrice = 0;
  69.     updatedIngridients[type] = updatedCount;
  70.     fewerPrice += INGRIDIENT_PRICES[type];
  71.  
  72.     const newPrice = this.state.totalPrice - fewerPrice;
  73.     this.setState({
  74.       totalPrice: newPrice,
  75.       ingredients: updatedIngridients
  76.     },()=>this.updatePurchaseState());
  77.   };
  78.   render() {
  79.     let disableInfo = {
  80.       ...this.state.ingredients
  81.     };
  82.  
  83.     for (let key in disableInfo) disableInfo[key] = disableInfo[key] <= 0;
  84.  
  85.     return (
  86.       <Auxilialry>
  87.         <Bruger ingredients={this.state.ingredients} />
  88.         <BuildControls
  89.           ingredientAdded={this.addIngridientHandler}
  90.           ingredientSubtracted={this.removeIngridientHandler}
  91.           purchasable={this.state.purchasable}
  92.           disabled={disableInfo}
  93.           price={this.state.totalPrice}
  94.         />
  95.       </Auxilialry>
  96.     );
  97.   }
  98. }
  99.  
  100. export default BurgerBuilder;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement