Skip to content

Commit

Permalink
Removing Ingredients Safely using the removeIngredientHandler()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch-sriram committed Aug 4, 2020
1 parent 6f630c0 commit cb10e79
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ NOTE: There can be more additions into the design, over iterations of the Applic
9. Adding the `<BuildControl />` & `<BuildControls />` Component: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/2c9be483093bc2f9015799e04844cda739f5d699)
10. Outputting Multiple `<BuildControl />` components using the `<BuildControls />` Component: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/ab893618efaf7013c49acc06cdb1b11d8af72d29)
11. Connecting `state` to each `<BuildControl />` & `<BuildControls />` Component
1. Adding `onClick` handler to the `More` button: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/bd206d7166ba60336acf25803fc2e01047b85485)
1. Adding Ingredients Dynamically &mdash; Connecting `onClick` handler to the `More` button: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/bd206d7166ba60336acf25803fc2e01047b85485)
2. Removing Ingredients Safely &mdash; Connecting `onClick` handler to the `Less` button: [Commit Details]()
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import { BuildControl, Label } from './BuildControl.styled';
const buildControl = props => (
<BuildControl>
<Label>{props.label}</Label>
<button className="Less">Less</button>
<button className="More" onClick={props.added}>More</button>
<button
className="Less"
onClick={props.removed}
disabled={props.disabled}>Less</button>
<button
className="More"
onClick={props.added}>More</button>
</BuildControl>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const buildControls = props => (
key={control.label}
label={control.label}
added={() => props.ingredientAdded(control.type)}
removed={() => props.ingredientRemoved(control.type)}
disabled={props.disabled[control.type]}
/>
))}
</BuildControls>
Expand Down
24 changes: 24 additions & 0 deletions src/containers/BurgerBuilder/BurgerBuilder.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,36 @@ class BurgerBuilder extends Component {
});
};

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
});
};

render() {
const disabledInfo = { ...this.state.ingredients };
for (let key in disabledInfo) {
disabledInfo[key] = disabledInfo[key] <= 0;
}

return (
<Aux>
<Burger ingredients={this.state.ingredients}/>
<BuildControls
ingredientAdded={this.addIngredientHandler}
ingredientRemoved={this.removeIngredientHandler}
disabled={disabledInfo}
/>
</Aux>
);
Expand Down

0 comments on commit cb10e79

Please sign in to comment.