Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basket, remove item, price, total price #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/components/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ import { PureComponent } from 'react';
import PropTypes from 'prop-types';
import Restaurants from '../restaurants';
import Header from '../header';
import Basket from '../basket';

export default class App extends PureComponent {
constructor(props) {
super(props);
this.state = {showBasket: false};
}
handleBasket = () => {
this.setState({
showBasket: !this.state.showBasket
});
}

render() {
return (
<div>
<Header />
{this.state.showBasket && <Basket restaurants={this.props.restaurants} handleBasket={this.handleBasket}/>}
<Header setBasket={this.handleBasket}/>
<Restaurants restaurants={this.props.restaurants} />
</div>
);
Expand Down
63 changes: 63 additions & 0 deletions src/components/basket/basket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { connect } from 'react-redux';
import { decrement, increment, removeItem } from '../../redux/actions';

import { ReactComponent as DeleteIcon } from '../../icons/delete-icon.svg';
import Button from '../button';
import styles from './basket.module.css';

const Basket = ({ order, decrement, increment, restaurants, removeItem, handleBasket}) => {
const dishes = [...restaurants].map(item => item.menu.map(item2 => item2)).flat(2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это вычисление лучше мемоизировать

let selectedItems = [];

for (let key in order) {
dishes.forEach(item => {
if (item.id === key){
selectedItems = [...selectedItems, item];
item.qty = order[key]
}
})
}
const totalPrice = selectedItems.reduce(( prev, cur ) => prev + cur.price*cur.qty, 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и в целом все эти вычисления можно вынести в mapStateToProps


return(
<div className={styles.basket}>
<DeleteIcon className={styles.closeIcon} onClick={handleBasket} />
<p>Selected Items:</p>
{selectedItems.map(item => (
<div key={item.id} className={styles.basketItem}>
<div>
<p>{item.name}</p>
<p>{item.qty*item.price}$</p>
<p className={styles.qty}>qty: {item.qty}</p>
</div>
<div className={styles.wrapperButtons}>
<DeleteIcon className={styles.deleteIcon} onClick={() => removeItem(item.id)} />
<div className={styles.buttons}>
<Button
onClick={() => decrement(item.id)}
icon="minus"
/>
<Button
onClick={() => increment(item.id)}
icon="plus"
/>
</div>
</div>
</div>
)
)}
<p>Total price: {totalPrice}$</p>
</div>
)
};

const mapStateToProps = (state, props) => ({
order: state.order
});

const mapDispatchToProps = {
decrement,
increment,
removeItem,
};
export default connect(mapStateToProps, mapDispatchToProps)(Basket);
51 changes: 51 additions & 0 deletions src/components/basket/basket.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.basket {
position: fixed;
width: 500px;
top: 170px;
border: 1px solid var(--grey);
z-index: 3;
left: calc(50% - 250px);
padding: 15px;
height: 300px;
background: var(--grey);
overflow-y: auto;
}

.basketItem{
padding-bottom: 7px;
border-bottom: 1px solid darkgray;
display: flex;
justify-content: space-between;
}

.buttons {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
width: auto;
}

.deleteIcon{
width: 20px;
height: 20px;
fill: var(--yellow);
margin: 12px 5px 0 auto;
cursor: pointer;
}

.wrapperButtons{
display: flex;
flex-direction: column;
justify-content: space-between;
}

.closeIcon{
position: absolute;
width: 20px;
height: 20px;
right: 10px;
top: 10px;
fill: darkgrey;
cursor: pointer;
}
1 change: 1 addition & 0 deletions src/components/basket/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './basket';
6 changes: 5 additions & 1 deletion src/components/header/header.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { ReactComponent as Logo } from '../../icons/logo.svg';
import styles from './header.module.css';

const Header = () => (
import { ReactComponent as BasketIcon } from '../../icons/basket.svg';


const Header = ({setBasket}) => (
<header className={styles.header}>
<Logo />
<BasketIcon className={styles.basket} onClick={setBasket}/>
</header>
);

Expand Down
9 changes: 9 additions & 0 deletions src/components/header/header.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@
right: 20px;
top: 0;
}

.basket{
position: absolute;
width: 29px;
fill: #fff;
top: 16px;
right: 55px;
cursor: pointer;
}
1 change: 1 addition & 0 deletions src/icons/basket.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/delete-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/redux/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DECREMENT, INCREMENT } from './constants';
import { DECREMENT, INCREMENT, REMOVE_ITEM } from './constants';

export const increment = (id) => ({ type: INCREMENT, id });
export const decrement = (id) => ({ type: DECREMENT, id });
export const removeItem = (id) => ({ type: REMOVE_ITEM, id });
1 change: 1 addition & 0 deletions src/redux/constants.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const REMOVE_ITEM = 'REMOVE_ITEM';
5 changes: 4 additions & 1 deletion src/redux/reducer/order.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DECREMENT, INCREMENT } from '../constants';
import { DECREMENT, INCREMENT, REMOVE_ITEM } from '../constants';

// { [productId]: amount }
export default function (state = {}, action) {
Expand All @@ -8,6 +8,9 @@ export default function (state = {}, action) {
return { ...state, [id]: (state[id] || 0) + 1 };
case DECREMENT:
return { ...state, [id]: (state[id] || 0) - 1 };
case REMOVE_ITEM:
delete state[id];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это мутация данных предыдущего стейта, этого делать нельзя

return { ...state };
default:
return state;
}
Expand Down