Advertisement
Guest User

Untitled

a guest
Apr 7th, 2019
178
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.     });
  57.     this.updatePurchaseState();
  58. };
  59.  
  60.   removeIngridientHandler = type => {
  61.     const oldCount = this.state.ingredients[type];
  62.     if (oldCount <= 0) {
  63.       return;
  64.     }
  65.     const updatedCount = oldCount - 1;
  66.     const updatedIngridients = {
  67.       ...this.state.ingredients
  68.     };
  69.     let fewerPrice = 0;
  70.     updatedIngridients[type] = updatedCount;
  71.     fewerPrice += INGRIDIENT_PRICES[type];
  72.  
  73.     const newPrice = this.state.totalPrice - fewerPrice;
  74.     this.setState({
  75.       totalPrice: newPrice,
  76.       ingredients: updatedIngridients
  77.     });
  78.     this.updatePurchaseState();
  79.   };
  80.   render() {
  81.     let disableInfo = {
  82.       ...this.state.ingredients
  83.     };
  84.  
  85.     for (let key in disableInfo) disableInfo[key] = disableInfo[key] <= 0;
  86.  
  87.     return (
  88.       <Auxilialry>
  89.         <Bruger ingredients={this.state.ingredients} />
  90.         <BuildControls
  91.           ingredientAdded={this.addIngridientHandler}
  92.           ingredientSubtracted={this.removeIngridientHandler}
  93.           purchasable={this.state.purchasable}
  94.           disabled={disableInfo}
  95.           price={this.state.totalPrice}
  96.         />
  97.       </Auxilialry>
  98.     );
  99.   }
  100. }
  101.  
  102. export default BurgerBuilder;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement