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

HW-3 #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

HW-3 #44

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
17 changes: 17 additions & 0 deletions src/components/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@ import { PureComponent } from 'react';
import PropTypes from 'prop-types';
import Restaurants from '../restaurants';
import Header from '../header';
import Basket from '../basket';

const getProductsMap = (restaurants) => {
const productsMap = new Map();
restaurants
.flatMap(item => item.menu)
.forEach(product => {
productsMap.set(product.id, {
name: product.name,
price: product.price,
})
});
return productsMap;
};

export default class App extends PureComponent {
render() {
const productsMap = getProductsMap(this.props.restaurants);
return (
<div>
<Basket productsMap={productsMap} />
<Header />
<Restaurants restaurants={this.props.restaurants} />
</div>
Expand All @@ -16,4 +32,5 @@ export default class App extends PureComponent {

App.propTypes = {
restaurants: PropTypes.array,
order: PropTypes.object,
};
41 changes: 41 additions & 0 deletions src/components/basket/basket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { connect } from 'react-redux';
import PropTypes from 'prop-types';

import Item from './item';

import styles from './basket.module.css';

const getTotal = (productsMap, orderItems) => {
return orderItems.reduce(
(previous, current) => previous + productsMap.get(current.id).price * current.amount,
0
);
};

const Basket = ({productsMap, order}) => {
const orderItems = Object.entries(order).map(item => ({
Copy link
Owner

Choose a reason for hiding this comment

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

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

Copy link
Owner

Choose a reason for hiding this comment

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

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

id: item[0],
amount: item[1],
}));

return <div className={styles.basket}>
<strong>Your order</strong>
<ul>
{orderItems.map(
(item) => <Item {...item} {...productsMap.get(item.id)} key={item.id} />
)}
</ul>
<div>Total: {getTotal(productsMap, orderItems)}$</div>
</div>
};

Basket.propTypes = {
order: PropTypes.object.isRequired,
productsMap: PropTypes.object.isRequired,
};

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

export default connect(mapStateToProps)(Basket);
11 changes: 11 additions & 0 deletions src/components/basket/basket.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.basket {
background-color: #ccc;
padding: 20px 10px;
}
.basket-item {
margin-bottom: 5px;
}
.basket-buttons {
display: inline-flex;
margin-left: 15px;
}
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';
46 changes: 46 additions & 0 deletions src/components/basket/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { connect } from 'react-redux';
import PropTypes from 'prop-types';

import Button from '../button';
import { decrement, increment, remove } from '../../redux/actions';

import styles from './basket.module.css';

const Item = ({id, amount, name, price, increment, decrement, remove}) => (
<li className={styles['basket-item']}>
{name} {price}$ x {amount} = {price * amount}$
<span className={styles['basket-buttons']}>
<Button
onClick={increment}
icon="plus"
/>
<Button
onClick={decrement}
icon="minus"
/>
<Button
onClick={remove}
icon="delete"
/>
</span>
</li>
);

Item.propTypes = {
id: PropTypes.string.isRequired,
amount: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
price: PropTypes.number.isRequired,
increment: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
remove: PropTypes.func.isRequired,
};


const mapDispatchToProps = (dispatch, props) => ({
decrement: () => dispatch(decrement(props.id)),
increment: () => dispatch(increment(props.id)),
remove: () => dispatch(remove(props.id)),
});

export default connect(null, mapDispatchToProps)(Item);
3 changes: 3 additions & 0 deletions src/components/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import styles from './button.module.css';

import { ReactComponent as PlusIcon } from '../../icons/plus.svg';
import { ReactComponent as MinusIcon } from '../../icons/minus.svg';
import { ReactComponent as DeleteIcon } from '../../icons/delete.svg';


const icons = {
plus: PlusIcon,
minus: MinusIcon,
delete: DeleteIcon,
};

const Button = ({ icon, ...props }) => {
Expand Down
1 change: 1 addition & 0 deletions src/components/header/header.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ReactComponent as Logo } from '../../icons/logo.svg';

import styles from './header.module.css';

const Header = () => (
Expand Down
11 changes: 11 additions & 0 deletions src/icons/delete.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 } from './constants';

export const increment = (id) => ({ type: INCREMENT, id });
export const decrement = (id) => ({ type: DECREMENT, id });
export const remove = (id) => ({ type: REMOVE, 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 = 'REMOVE';
19 changes: 17 additions & 2 deletions src/redux/reducer/order.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { DECREMENT, INCREMENT } from '../constants';
import { DECREMENT, INCREMENT, REMOVE } from '../constants';

const removeItem = (state, id) => {
let newState = { ...state };
delete newState[id];
return newState;
};

// { [productId]: amount }
export default function (state = {}, action) {
const { type, id } = action;
switch (type) {
case INCREMENT:
return { ...state, [id]: (state[id] || 0) + 1 };

case DECREMENT:
return { ...state, [id]: (state[id] || 0) - 1 };
let newAmount = (state[id] || 0) - 1;
if (newAmount > 0) {
return { ...state, [id]: newAmount };
}
return removeItem(state, id);

case REMOVE:
return removeItem(state, id);

default:
return state;
}
Expand Down
Loading