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

Misnik ht 3 #46

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
2 changes: 2 additions & 0 deletions src/components/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ 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 {
render() {
return (
<div>
<Header />
<Basket restaurants={this.props.restaurants}/>
<Restaurants restaurants={this.props.restaurants} />
</div>
);
Expand Down
43 changes: 43 additions & 0 deletions src/components/basket/basket-item/basket-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { connect } from 'react-redux';
import { decrement, deleteItem, increment } from '../../../redux/actions';
import styles from '../../product/product.module.css';
import Button from '../../button';

function BasketItem({ id, name, amount, decrement, increment, deleteItem, summ }) {
return (
<div>
<div>
<span>{name}</span>&nbsp;
<span>{amount}</span>&nbsp;
<span>{summ}</span>
</div>
<div className={styles.buttons}>
<Button
onClick={decrement}
data-id='product-decrement'
icon='minus'
/>
<Button
onClick={increment}
data-id='product-increment'
icon='plus'
/>
<Button
onClick={deleteItem}
data-id='product-delete'
icon='delete'
/>
</div>
</div>
)
}

const mapStateToProps = () => ({})

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

export default connect(mapStateToProps, mapDispatchToProps)(BasketItem)
Copy link
Owner

@koretskiyav koretskiyav Dec 10, 2021

Choose a reason for hiding this comment

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

если mapStateToProps не нужен, то можно null передать

1 change: 1 addition & 0 deletions src/components/basket/basket-item/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './basket-item';
37 changes: 37 additions & 0 deletions src/components/basket/basket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import BasketItem from './basket-item';
import { connect } from 'react-redux';

function Basket ({ restaurants, orderMap, amount }) {

const menu = restaurants.map((item) => item.menu).reduce((previousValue, currentValue) => previousValue.concat(currentValue))
Copy link
Owner

Choose a reason for hiding this comment

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

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

const newOrderMap = orderMap;
let allSum = 0;

const products = Object.entries(newOrderMap).filter(item => item[1] > 0).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.

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

const productItems = menu.find(({ id }) => item[0] === id)
const sum = productItems.price * item[1]
allSum = allSum + sum
return {
id: item[0],
name: productItems.name,
amount: item[1],
sum,
allSum
}
})

return (
<div>
{products.map((item) => (
<BasketItem key={item.id} id={item.id} name={item.name} amount={item.amount} summ={item.sum}/>
))}
<p>Общая сумма заказа: {allSum}</p>
</div>
)
}

const mapStateToProps = (state) => ({
orderMap: state.order
})

export default connect(mapStateToProps)(Basket)
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';
2 changes: 2 additions & 0 deletions src/components/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ 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/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, DELETE, INCREMENT } from './constants';

export const increment = (id) => ({ type: INCREMENT, id });
export const decrement = (id) => ({ type: DECREMENT, id });
export const deleteItem = (id) => ({ type: DELETE, 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 DELETE = 'DELETE';
6 changes: 4 additions & 2 deletions 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, DELETE, INCREMENT } from '../constants';

// { [productId]: amount }
export default function (state = {}, action) {
Expand All @@ -7,7 +7,9 @@ export default function (state = {}, action) {
case INCREMENT:
return { ...state, [id]: (state[id] || 0) + 1 };
case DECREMENT:
return { ...state, [id]: (state[id] || 0) - 1 };
return { ...state, [id]: (state[id]) > 0 ? state[id] - 1 : 0 };
case DELETE:
return { ...state, [id]: 0 };
default:
return state;
}
Expand Down
Loading