Commit 8ea89b32 authored by anton's avatar anton

first_lesson-3

parent e8d3255b
import { IngredientNames } from "@/interfaces/burger-interface";
import "./BuildControl.css"; import "./BuildControl.css";
interface Props {} interface Props {
type: IngredientNames;
hadlerLess: () => void;
hadlerMore: () => void;
}
export function BuildControl(props: Props) { export function BuildControl({ type, hadlerLess, hadlerMore }: Props) {
return null; return (
<div className="BuildControl">
<div className="Label">{type}</div>
<button className="Less" onClick={hadlerLess}>
Less
</button>
<button className="More" onClick={hadlerMore}>
More
</button>
</div>
);
} }
import { IngredientNames, Ingredients } from "@/interfaces/burger-interface";
import "./BuildControls.css"; import "./BuildControls.css";
import { BuildControl } from "@/components/BuildControls/BuildControl/BuildControl";
interface Props {} interface Props {
ingredients: Ingredients;
hadlerLess: (key: IngredientNames) => void;
hadlerMore: (key: IngredientNames) => void;
}
export function BuildControls({ ingredients, hadlerLess, hadlerMore }: Props) {
const ingrKeys = Object.keys(ingredients) as (keyof Ingredients)[];
export function BuildControls(props: Props) { return (
return null; <div className="BuildControls">
{ingrKeys.map((key, i) => (
<BuildControl
key={key + i}
type={key}
hadlerLess={() => hadlerLess(key)}
hadlerMore={() => hadlerMore(key)}
/>
))}
</div>
);
} }
...@@ -8,21 +8,22 @@ interface Props { ...@@ -8,21 +8,22 @@ interface Props {
} }
export function Burger({ ingredients }: Props) { export function Burger({ ingredients }: Props) {
const ingKeys = Object.keys(ingredients) as (keyof Ingredients)[]; const ingredientsKeys = Object.keys(ingredients) as (keyof Ingredients)[];
let ingList: ReactNode[] = []; let ingrList: ReactNode[] = [];
ingKeys.forEach((key) => { ingredientsKeys.forEach((ingrKey) => {
const amount = ingredients[key]; let amount = ingredients[ingrKey];
for (let i = 0; i < amount; i++) {
ingrList.push(<Ingredient key={ingrKey + i} type={ingrKey} />);
}
}); });
return ( return (
<div className="Burger"> <div className="Burger">
<Ingredient type={"bread-top"} /> <Ingredient type={"bread-top"} />
<Ingredient type={"cheese"} /> {ingrList}
<Ingredient type={"bacon"} />
<Ingredient type={"meat"} />
<Ingredient type={"salad"} />
<Ingredient type={"bread-bottom"} /> <Ingredient type={"bread-bottom"} />
</div> </div>
); );
......
...@@ -7,6 +7,8 @@ interface Props { ...@@ -7,6 +7,8 @@ interface Props {
export function Ingredient({ type }: Props) { export function Ingredient({ type }: Props) {
switch (type) { switch (type) {
case "bread-bottom":
return <div className="BreadBottom"></div>;
case "bread-top": case "bread-top":
return ( return (
<div className="BreadTop"> <div className="BreadTop">
...@@ -14,20 +16,15 @@ export function Ingredient({ type }: Props) { ...@@ -14,20 +16,15 @@ export function Ingredient({ type }: Props) {
<div className="Seeds2"></div> <div className="Seeds2"></div>
</div> </div>
); );
case "bread-bottom":
return <div className="BreadBottom"></div>;
case "bacon":
return <div className="Bacon"></div>;
case "cheese":
return <div className="Cheese"></div>;
case "meat": case "meat":
return <div className="Meat"></div>; return <div className="Meat"></div>;
case "cheese":
return <div className="Cheese"></div>;
case "salad": case "salad":
return <div className="Salad"></div>; return <div className="Salad"></div>;
case "bacon":
return <div className="Bacon"></div>;
default:
break;
} }
} }
import { BuildControls } from "@/components/BuildControls/BuildControls"; import { BuildControls } from "@/components/BuildControls/BuildControls";
import { Burger } from "@/components/Burger/Burger"; import { Burger } from "@/components/Burger/Burger";
import { Ingredients } from "@/interfaces/burger-interface"; import { IngredientNames, Ingredients } from "@/interfaces/burger-interface";
import { useState } from "react"; import { useState } from "react";
export function BurgerBuilder() { export function BurgerBuilder() {
...@@ -10,10 +10,24 @@ export function BurgerBuilder() { ...@@ -10,10 +10,24 @@ export function BurgerBuilder() {
bacon: 0, bacon: 0,
meat: 0, meat: 0,
}); });
const hadlerLess = (ingKey: IngredientNames) => {
setIngredients((prevState) => ({
...prevState,
[ingKey]: prevState[ingKey] !== 0 ? prevState[ingKey] - 1 : 0,
}));
};
const hadlerMore = (ingKey: IngredientNames) => {
setIngredients((prevState) => ({
...prevState,
[ingKey]: prevState[ingKey] + 1,
}));
};
return ( return (
<> <>
<Burger ingredients={ingredients} /> <Burger ingredients={ingredients} />
<BuildControls /> <BuildControls ingredients={ingredients} hadlerLess={hadlerLess} hadlerMore={hadlerMore} />
</> </>
); );
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment