Commit 3c713bec authored by zarina's avatar zarina 🌊

#6, реализована возможность удаления элемента из корзины + выведены иконки (#2)

parent e2db9d87
.Basket { .Basket {
width: 30%; width: 30%;
border: 3px solid silver; border: 3px solid darkslategrey;;
min-height: 400px; min-height: 400px;
border-radius: 10px 0 0 10px; border-radius: 10px 0 0 10px;
padding-top: 20px; padding-top: 20px;
......
...@@ -10,10 +10,11 @@ const Basket = props => { ...@@ -10,10 +10,11 @@ const Basket = props => {
let createBasket = () => { let createBasket = () => {
return basketElements.map((elem, i) => { return basketElements.map((elem, i) => {
return <BasketItem return <BasketItem
key={'BasketItem-' + {elem} + i} key={'BasketItem-' + elem + i}
qty={props.basket[elem]} qty={props.basket[elem]}
price={props.menuList[elem].price} price={props.menuList[elem].price}
name={elem}/>; name={elem}
removeElement={()=> props.removeElement(elem)}/>;
}); });
} }
return ( return (
......
...@@ -8,7 +8,9 @@ const BasketItem = props => { ...@@ -8,7 +8,9 @@ const BasketItem = props => {
<div> <div>
<span> {props.price} KZT </span> <span> {props.price} KZT </span>
<span> <b> x{props.qty} </b></span> <span> <b> x{props.qty} </b></span>
<button onClick={props.removeElement}> x </button>
</div> </div>
</div> </div>
); );
}; };
......
...@@ -3,5 +3,19 @@ ...@@ -3,5 +3,19 @@
justify-content: space-between; justify-content: space-between;
padding: 20px; padding: 20px;
align-items: center; align-items: center;
}
.BasketItem button {
padding: 6px;
line-height: 10px;
font-size: 20px;
color: red;
background: #eee;
border: 1px solid darkslategrey;;
border-radius: 5px;
margin-left: 10px;
}
.BasketItem button:focus {
outline: none;
} }
\ No newline at end of file
.TotalPrice{ .TotalPrice{
border-top: 1px solid black; border-top: 1px solid black;
padding-top: 10px; padding-top: 10px;
margin-top: 10px; margin: 10px 0;
font-weight: bold;
text-transform: uppercase;
} }
\ No newline at end of file
.Menu { .Menu {
width: 70%; width: 70%;
border: 3px solid silver; border: 3px solid darkslategrey;
border-radius: 0 10px 10px 0; border-radius: 0 10px 10px 0;
padding: 10px; padding: 10px;}
}
\ No newline at end of file
...@@ -7,16 +7,14 @@ const Menu = props => { ...@@ -7,16 +7,14 @@ const Menu = props => {
let createMenu = () => { let createMenu = () => {
let menuKeys = Object.keys(props.menuList); let menuKeys = Object.keys(props.menuList);
return menuKeys.map(key => { return menuKeys.map(key => {
let className = ['MenuItem']; let imgUrl = './img/drink_icon.jpg';
if (props.menuList[key].type === 'drink') { if (props.menuList[key].type === 'food') {
className.push('MenuItem_drink') imgUrl = './img/food_icon.jpg';
} else {
className.push('MenuItem_eat')
} }
return <MenuItem addElem={() => (props.addElement(key))} name={key} price={props.menuList[key].price} return <MenuItem addElem={() => (props.addElement(key))} name={key} price={props.menuList[key].price}
key={'MenuItem-' + key} class={className.join(' ')}/> key={'MenuItem-' + key} img={imgUrl}/>
}); });
}; };
......
.MenuItem { .MenuItem {
margin: 10px; margin: 10px;
background: #ccc;
height: auto; height: auto;
width: 35%; width: 320px;
border: 3px solid #eee; border: 5px solid darkslategrey;
border-radius: 10px;
padding: 20px 0 20px 70px; padding: 20px 0 20px 70px;
display: inline-block; display: inline-block;
cursor: pointer; cursor: pointer;
position: relative;
}
.MenuItem img {
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 50px;
} }
\ No newline at end of file
...@@ -2,9 +2,10 @@ import React from "react"; ...@@ -2,9 +2,10 @@ import React from "react";
import './MenuItem.css' import './MenuItem.css'
const MenuItem = props => { const MenuItem = props => {
return ( return (
<div onClick={props.addElem} className={props.class}> <div className='MenuItem' onClick={props.addElem} >
<h3 className='MenuItem_name'> {props.name}</h3> <img src={props.img} width='50' height='50' alt='element_icon'/>
<span className='MenuItem_price'>Price: {props.price}</span> <h3> {props.name}</h3>
<span>Price: {props.price}</span>
</div> </div>
); );
}; };
......
...@@ -8,15 +8,15 @@ class App extends Component { ...@@ -8,15 +8,15 @@ class App extends Component {
menu: { menu: {
Hamburger: { Hamburger: {
price: 200, price: 200,
type: 'eat' type: 'food'
}, },
Cheeseburger: { Cheeseburger: {
price: 300, price: 300,
type: 'eat' type: 'food'
}, },
Fries: { Fries: {
price: 150, price: 150,
type: 'eat' type: 'food'
}, },
Coffee: { Coffee: {
price: 70, price: 70,
...@@ -38,7 +38,7 @@ class App extends Component { ...@@ -38,7 +38,7 @@ class App extends Component {
addElement = (name) => { addElement = (name) => {
let basket = {...this.state.basket}; let basket = {...this.state.basket};
if (!basket[name]){ if (!basket[name]) {
basket[name] = 1 basket[name] = 1
} else { } else {
basket[name]++ basket[name]++
...@@ -49,10 +49,27 @@ class App extends Component { ...@@ -49,10 +49,27 @@ class App extends Component {
}; };
removeElement = (name) => {
let basket = {...this.state.basket};
if (basket[name] === 1) {
delete basket[name]
} 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 menuList={this.state.menu} totalPrice={this.state.totalPrice} basket={this.state.basket}/> <Basket menuList={this.state.menu}
totalPrice={this.state.totalPrice}
basket={this.state.basket}
removeElement={this.removeElement}/>
<Menu menuList={this.state.menu} addElement={this.addElement}/> <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