diff --git a/src/components/app/app.js b/src/components/app/app.js
index 3124600..2d708d2 100644
--- a/src/components/app/app.js
+++ b/src/components/app/app.js
@@ -2,13 +2,16 @@ 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() {
+ const { restaurants } = this.props;
return (
-
+
+
);
}
diff --git a/src/components/basket/basket.js b/src/components/basket/basket.js
new file mode 100644
index 0000000..c5ae62a
--- /dev/null
+++ b/src/components/basket/basket.js
@@ -0,0 +1,63 @@
+import { connect } from 'react-redux';
+import {useMemo} from "react";
+import BasketItem from './basketItem';
+import PropTypes from "prop-types";
+
+function Basket({ restaurants, order }) {
+
+ const menus = useMemo(
+ () => restaurants.map(({ menu }) => ( menu )).reduce((pre, cur) => pre.concat(cur)),
+ [restaurants]
+ );
+
+ const products = useMemo(
+ () => Object.keys(order).map(key => {
+ const product = menus.find(({id}) => (id === key));
+ return {
+ id: key,
+ name: product.name,
+ count: order[key],
+ sum: product.price * order[key],
+ }
+ }), [menus, order]);
+
+ const total = useMemo(
+ () => products.length && products.reduce((pre, cur) => pre + cur.sum, 0)
+ , [products]);
+
+ return (
+ <>
+
+ {products.map(({id, name, count, sum}) =>
+
+
+
+ )}
+
+
+ Сумма: {total}
+
+ >
+ );
+}
+
+Basket.propTypes = {
+ restaurants: PropTypes.arrayOf(
+ PropTypes.shape({
+ menu: PropTypes.arrayOf(
+ PropTypes.shape({
+ id: PropTypes.string,
+ name: PropTypes.string,
+ price: PropTypes.number,
+ }).isRequired
+ ).isRequired
+ }).isRequired
+ ).isRequired,
+ order: PropTypes.objectOf(PropTypes.number),
+};
+
+const mapStateToProps = (state) => ({
+ order: state.order || 0,
+});
+
+export default connect(mapStateToProps)(Basket);
diff --git a/src/components/basket/basketItem/basketItem.js b/src/components/basket/basketItem/basketItem.js
new file mode 100644
index 0000000..eb000d9
--- /dev/null
+++ b/src/components/basket/basketItem/basketItem.js
@@ -0,0 +1,47 @@
+import { memo } from 'react'
+import { connect } from 'react-redux';
+import {decrement, increment, deleteProduct} from "../../../redux/actions";
+import Button from "../../button";
+import PropTypes from "prop-types";
+
+
+function BasketItem({ id, name, count, sum, decrement, increment, deleteProduct }) {
+ return (
+
+ {id}{' '}
+ {name}{' '}
+ {count}{' '}
+ {sum}
+
+
+
+
+ );
+}
+
+BasketItem.propTypes = {
+ id: PropTypes.string,
+ name: PropTypes.string,
+ count: PropTypes.number,
+ sum: PropTypes.number,
+ decrement: PropTypes.func,
+ increment: PropTypes.func,
+ deleteProduct: PropTypes.func,
+}
+
+const mapDispatchToProps = (dispatch, props) => ({
+ decrement: () => dispatch(decrement(props.id)),
+ increment: () => dispatch(increment(props.id)),
+ deleteProduct: () => dispatch(deleteProduct(props.id)),
+});
+
+export default connect(null, mapDispatchToProps)(memo(BasketItem));
diff --git a/src/components/basket/basketItem/index.js b/src/components/basket/basketItem/index.js
new file mode 100644
index 0000000..2da8fd1
--- /dev/null
+++ b/src/components/basket/basketItem/index.js
@@ -0,0 +1 @@
+export { default } from './basketItem';
diff --git a/src/components/basket/index.js b/src/components/basket/index.js
new file mode 100644
index 0000000..d9c6b0c
--- /dev/null
+++ b/src/components/basket/index.js
@@ -0,0 +1 @@
+export { default } from './basket';
diff --git a/src/redux/actions.js b/src/redux/actions.js
index 92f8715..97398c6 100644
--- a/src/redux/actions.js
+++ b/src/redux/actions.js
@@ -1,4 +1,5 @@
-import { DECREMENT, INCREMENT } from './constants';
+import { DECREMENT, INCREMENT, DELETE } from './constants';
export const increment = (id) => ({ type: INCREMENT, id });
export const decrement = (id) => ({ type: DECREMENT, id });
+export const deleteProduct = (id) => ({ type: DELETE, id });
diff --git a/src/redux/constants.js b/src/redux/constants.js
index 930d9ef..2e78914 100644
--- a/src/redux/constants.js
+++ b/src/redux/constants.js
@@ -1,2 +1,3 @@
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
+export const DELETE = 'DELETE';
diff --git a/src/redux/reducer/order.js b/src/redux/reducer/order.js
index 672d5bb..4d537d9 100644
--- a/src/redux/reducer/order.js
+++ b/src/redux/reducer/order.js
@@ -1,4 +1,4 @@
-import { DECREMENT, INCREMENT } from '../constants';
+import { DECREMENT, INCREMENT, DELETE } from '../constants';
// { [productId]: amount }
export default function (state = {}, action) {
@@ -8,6 +8,8 @@ export default function (state = {}, action) {
return { ...state, [id]: (state[id] || 0) + 1 };
case DECREMENT:
return { ...state, [id]: (state[id] || 0) - 1 };
+ case DELETE:
+ return Object.fromEntries(Object.entries(state).filter(([key]) => key !== id) )
default:
return state;
}