Commit 95600ec5 authored by zarina's avatar zarina 🌊

#4, реализована возможность добавления элемента в корзину

parent 4178ca4a
...@@ -3,11 +3,28 @@ import MenuItem from "./MenuItem/MenuItem"; ...@@ -3,11 +3,28 @@ import MenuItem from "./MenuItem/MenuItem";
import './Menu.css' import './Menu.css'
const Menu = props => { const Menu = props => {
return (
<div className='Menu'> let createMenu = () => {
<MenuItem/> let menuKeys = Object.keys(props.menuList);
</div> return menuKeys.map(key => {
); let className = ['MenuItem'];
if (props.menuList[key].type === 'drink') {
className.push('MenuItem_drink')
} else {
className.push('MenuItem_eat')
}
return <MenuItem addElem={() => (props.addElement(key))} name={key} price={props.menuList[key].price}
key={'MenuItem-' + key} class={className.join(' ')}/>
});
};
return (
<div className='Menu'>
{createMenu()}
</div>
);
}; };
export default Menu; export default Menu;
\ No newline at end of file
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
height: auto; height: auto;
width: 35%; width: 35%;
border: 3px solid #eee; border: 3px solid #eee;
padding: 20px 0 ; padding: 20px 0 20px 70px;
padding-left: 70px; display: inline-block;
cursor: pointer;
} }
\ No newline at end of file
...@@ -2,9 +2,9 @@ import React from "react"; ...@@ -2,9 +2,9 @@ import React from "react";
import './MenuItem.css' import './MenuItem.css'
const MenuItem = props => { const MenuItem = props => {
return ( return (
<div className='MenuItem'> <div onClick={props.addElem} className={props.class}>
<h3 className='MenuItem_name'>testik</h3> <h3 className='MenuItem_name'> {props.name}</h3>
<span className='MenuItem_price'>Price: 1321312</span> <span className='MenuItem_price'>Price: {props.price}</span>
</div> </div>
); );
}; };
......
...@@ -35,13 +35,24 @@ class App extends Component { ...@@ -35,13 +35,24 @@ class App extends Component {
basket: {} basket: {}
}; };
addElement = (name) => {
let basket = {...this.state.basket};
if (!basket[name]){
basket[name] = 1
} else {
basket[name]++
}
let totalPrice = this.state.totalPrice + this.state.menu[name].price;
this.setState({totalPrice, basket})
};
render() { render() {
return ( return (
<div className='App'> <div className='App'>
<Basket/> <Basket/>
<Menu/> <Menu menuList={this.state.menu} addElement={this.addElement}/>
</div> </div>
) )
} }
......
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