-
Notifications
You must be signed in to change notification settings - Fork 13
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
HT5 done #46
base: master
Are you sure you want to change the base?
HT5 done #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,43 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import React, { useEffect } from 'react'; | ||
import { connect, useDispatch, useSelector } from 'react-redux'; | ||
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 { averageRatingSelector } from '../../redux/selectors'; | ||
import { averageRatingSelector, productsSelector } from '../../redux/selectors'; | ||
import { loadProducts } from '../../redux/actions'; | ||
|
||
const Restaurant = ({ restaurant, averageRating }) => { | ||
const Restaurant = ({ restaurant, allProducts, loadProducts }) => { | ||
const { id, name, menu, reviews } = restaurant; | ||
useEffect(() => { | ||
const allProdArray = Object.keys(allProducts); | ||
if (!allProdArray.includes(menu[0])) { | ||
loadProducts(id); | ||
} | ||
}, [id, menu]); | ||
|
||
const tabs = [ | ||
{ title: 'Menu', content: <Menu menu={menu} /> }, | ||
{ | ||
title: 'Reviews', | ||
content: <Reviews reviews={reviews} restaurantId={id} />, | ||
}, | ||
]; | ||
const allReviews = useSelector((state) => state.reviews.entities); | ||
const filteredReviewsIdArr = Object.keys(allReviews).filter((reviewId) => | ||
reviews.includes(reviewId) | ||
); | ||
|
||
//todo fix loop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше это сразу попровлять в селекторе averageRatingSelector |
||
let averageRating = 0; | ||
for (const reviewId of filteredReviewsIdArr) { | ||
const review = allReviews[reviewId]; | ||
const rating = review.rating; | ||
averageRating = averageRating + rating; | ||
} | ||
averageRating = averageRating / filteredReviewsIdArr.length; | ||
|
||
return ( | ||
<div> | ||
|
@@ -38,6 +59,10 @@ Restaurant.propTypes = { | |
averageRating: PropTypes.number, | ||
}; | ||
|
||
export default connect((state, props) => ({ | ||
averageRating: averageRatingSelector(state, props), | ||
}))(Restaurant); | ||
export default connect( | ||
(state, props) => ({ | ||
//averageRating: averageRatingSelector(state, props), | ||
allProducts: productsSelector(state, props), | ||
}), | ||
{ loadProducts } | ||
)(Restaurant); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,18 @@ import { | |
restaurantsLoadedSelector, | ||
restaurantsLoadingSelector, | ||
} from '../../redux/selectors'; | ||
import { loadRestaurants } from '../../redux/actions'; | ||
import { loadRestaurants, loadUsers } from '../../redux/actions'; | ||
|
||
const Restaurants = ({ restaurants, loading, loaded, loadRestaurants }) => { | ||
const Restaurants = ({ | ||
restaurants, | ||
loading, | ||
loaded, | ||
loadRestaurants, | ||
loadUsers, | ||
}) => { | ||
useEffect(() => { | ||
loadUsers(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут все равно нужно проверку делать на loading, т.к. этот компонент может быть размонтирован и смонтирован повторно |
||
}, []); | ||
useEffect(() => { | ||
if (!loading && !loaded) loadRestaurants(); | ||
}, [loading, loaded, loadRestaurants]); | ||
|
@@ -22,7 +31,6 @@ const Restaurants = ({ restaurants, loading, loaded, loadRestaurants }) => { | |
title: restaurant.name, | ||
content: <Restaurant restaurant={restaurant} />, | ||
})); | ||
|
||
return <Tabs tabs={tabs} />; | ||
}; | ||
|
||
|
@@ -40,5 +48,5 @@ export default connect( | |
loading: restaurantsLoadingSelector(state), | ||
loaded: restaurantsLoadedSelector(state), | ||
}), | ||
{ loadRestaurants } | ||
{ loadRestaurants, loadUsers } | ||
)(Restaurants); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,49 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { connect, useSelector } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import Rate from '../../rate'; | ||
import styles from './review.module.css'; | ||
import { reviewWitUserSelector } from '../../../redux/selectors'; | ||
import Loader from '../../loader'; | ||
import { usersSelector } from '../../../redux/selectors'; | ||
|
||
const Review = ({ user, text, rating }) => ( | ||
<div className={styles.review} data-id="review"> | ||
<div className={styles.content}> | ||
<div> | ||
<h4 className={styles.name} data-id="review-user"> | ||
{user} | ||
</h4> | ||
<p className={styles.comment} data-id="review-text"> | ||
{text} | ||
</p> | ||
</div> | ||
<div className={styles.rate}> | ||
<Rate value={rating} /> | ||
const Review = ({ id }) => { | ||
const allUsers = useSelector((state) => usersSelector(state)); | ||
const allReviews = useSelector((state) => state.reviews.entities); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не забываем использовать селекторы |
||
const review = allReviews[id]; | ||
if (!review) { | ||
return <Loader />; | ||
} | ||
const { userId, text, rating } = review; | ||
const user = allUsers[userId].name; | ||
|
||
return ( | ||
<div className={styles.review} data-id="review"> | ||
<div className={styles.content}> | ||
<div> | ||
<h4 className={styles.name} data-id="review-user"> | ||
{user} | ||
</h4> | ||
<p className={styles.comment} data-id="review-text"> | ||
{text} | ||
</p> | ||
</div> | ||
<div className={styles.rate}> | ||
<Rate value={rating} /> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
); | ||
}; | ||
|
||
Review.propTypes = { | ||
user: PropTypes.string, | ||
text: PropTypes.string, | ||
rating: PropTypes.number.isRequired, | ||
//rating: PropTypes.number.isRequired, | ||
}; | ||
|
||
Review.defaultProps = { | ||
user: 'Anonymous', | ||
}; | ||
|
||
export default connect((state, props) => ({ | ||
...reviewWitUserSelector(state, props), | ||
}))(Review); | ||
export default connect((state, props) => ({}))(Review); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
я в своей домашке покажу, как это можно сделать немного красивее