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} +