diff --git a/README.md b/README.md index cd66da7..ab2c4dd 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ ## HT4 -1. Переписать редьюсеры **review** и **restaurant** на key=>value (аналогично **products**) +1. Переписать редьюсеры **review** и **restaurant** + на key=>value (аналогично **products**) 2. Добавить **users** редьюсер 3. Починить отображение **Review** компонента (взять данные из редьюсеров **review** и **users**) 4. Написать **middleware** для генерации **[uuid](https://github.com/uuidjs/uuid)** diff --git a/package.json b/package.json index a1ec9c4..d10360c 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "redux": "^4.0.5", "redux-devtools-extension": "^2.13.8", "reselect": "^4.0.0", + "uuid": "^8.3.2", "web-vitals": "^0.2.4" }, "scripts": { diff --git a/src/components/basket/basket.js b/src/components/basket/basket.js index 665c3d2..1e698a0 100644 --- a/src/components/basket/basket.js +++ b/src/components/basket/basket.js @@ -42,10 +42,17 @@ function Basket({ title = 'Basket', total, orderProducts }) { ); } +const mapStateToProps = (state) => { + return { + total: totalSelector(state), + orderProducts: orderProductsSelector(state), + }; +}; +export default connect(mapStateToProps)(Basket); -export default connect((state) => { +/*export default connect((state) => { return { total: totalSelector(state), orderProducts: orderProductsSelector(state), }; -})(Basket); +})(Basket);*/ diff --git a/src/components/restaurant/restaurant.js b/src/components/restaurant/restaurant.js index 3c0d586..89615a0 100644 --- a/src/components/restaurant/restaurant.js +++ b/src/components/restaurant/restaurant.js @@ -1,22 +1,41 @@ -import React, { useMemo } from 'react'; +import React, { useEffect, useMemo } from 'react'; import PropTypes from 'prop-types'; import Menu from '../menu'; import Reviews from '../reviews'; import Banner from '../banner'; import Rate from '../rate'; import Tabs from '../tabs'; +import { useDispatch, useSelector } from 'react-redux'; +import { setRestaurantActive } from '../../redux/actions'; const Restaurant = ({ restaurant }) => { + const dispatch = useDispatch(); + useEffect(() => { + dispatch(setRestaurantActive(restaurant.id)); + }, [restaurant.id]); + const { name, menu, reviews } = restaurant; + const allReviews = useSelector((state) => state.reviews); + const filteredReviews = useMemo(() => { + return Object.keys(allReviews).filter((allReviewKey) => + reviews.includes(allReviewKey) + ); + }, [allReviews, reviews]); const averageRating = useMemo(() => { - const total = reviews.reduce((acc, { rating }) => acc + rating, 0); + const total = filteredReviews + .map((reviewId) => allReviews[reviewId].rating) + .reduce((acc, rating) => acc + rating, 0); + return Math.round(total / reviews.length); - }, [reviews]); + }, [reviews, allReviews]); const tabs = [ { title: 'Menu', content: }, - { title: 'Reviews', content: }, + { + title: 'Reviews', + content: , + }, ]; return ( diff --git a/src/components/restaurants/restaurants.js b/src/components/restaurants/restaurants.js index 1e20ee0..65d3f8a 100644 --- a/src/components/restaurants/restaurants.js +++ b/src/components/restaurants/restaurants.js @@ -3,6 +3,7 @@ import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import Restaurant from '../restaurant'; import Tabs from '../tabs'; +import { allRestaurantsSelector } from '../../redux/selectors'; const Restaurants = ({ restaurants }) => { const tabs = restaurants.map((restaurant) => ({ @@ -14,13 +15,9 @@ const Restaurants = ({ restaurants }) => { }; Restaurants.propTypes = { - restaurants: PropTypes.arrayOf( - PropTypes.shape({ - id: PropTypes.string.isRequired, - }).isRequired - ).isRequired, + restaurants: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired, }; export default connect((state) => ({ - restaurants: state.restaurants, + restaurants: allRestaurantsSelector(state), }))(Restaurants); diff --git a/src/components/reviews/review-form/review-form.js b/src/components/reviews/review-form/review-form.js index 88cbf6d..3400af8 100644 --- a/src/components/reviews/review-form/review-form.js +++ b/src/components/reviews/review-form/review-form.js @@ -3,17 +3,24 @@ import useForm from '../../../hooks/use-form'; import Rate from '../../rate'; import styles from './review-form.module.css'; -import { connect } from 'react-redux'; +import { connect, useSelector } from 'react-redux'; import Button from '../../button'; +import { postReview } from '../../../redux/actions'; -const INITIAL_VALUES = { name: '', text: '', rating: 5 }; +const INITIAL_VALUES = { yourName: '', text: '', rating: 5 }; const ReviewForm = ({ onSubmit }) => { const { values, handlers, reset } = useForm(INITIAL_VALUES); + const activeRestaurantId = useSelector( + (state) => state.restaurants.activeRestaurantId + ); + const reviewState = useSelector((state) => state.reviews); + const { error, errorMessage } = reviewState; + const handleSubmit = (ev) => { ev.preventDefault(); - onSubmit(values); + onSubmit(values, activeRestaurantId); reset(); }; @@ -25,8 +32,9 @@ const ReviewForm = ({ onSubmit }) => { + {error &&

{errorMessage}

}