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

Feature/ht5 #75

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
42 changes: 35 additions & 7 deletions src/components/menu/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import PropTypes from 'prop-types';

import Product from '../product';
import Basket from '../basket';
import Loader from '../loader';

import styles from './menu.module.css';
import { connect } from 'react-redux';
import { loadProducts } from '../../redux/actions';
import { menuLoadedSelector, menuLoadingSelector } from '../../redux/selectors';

class Menu extends Component {
static propTypes = {
id: PropTypes.string.isRequired,
menu: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
loading: PropTypes.bool,
loadProducts: PropTypes.func.isRequired,
};

state = { error: null };
Expand All @@ -17,20 +24,32 @@ class Menu extends Component {
this.setState({ error });
}

componentDidMount({ prevId } = {}) {
Copy link
Owner

Choose a reason for hiding this comment

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

в componentDidMount ничего не приходит, это у нас самый первый рендер компонента

Copy link
Author

Choose a reason for hiding this comment

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

@koretskiyav - так и есть. но за счет того, что мы перемонтируем меню каждый раз (key={id}), didMount отрабатывает каждое переключение ресторана и инициирует подгрузку продуктов.

Copy link
Author

Choose a reason for hiding this comment

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

в данном случае, наверное, можно было не делать проверок в принципе, просто дергать за загрузку.

const { id, loading, loadProducts } = this.props;

if ((!!prevId && prevId === id) || loading) return;

loadProducts(id);
}

render() {
const { menu } = this.props;
const { menu, loading, loaded } = this.props;

if (this.state.error) {
return <p>Меню этого ресторана сейчас недоступно :(</p>;
}

const products = loading ? (
<Loader />
) : !loaded ? (
'no data :('
) : (
menu.map((id) => <Product key={id} id={id} />)
);

return (
<div className={styles.menu}>
<div>
{menu.map((id) => (
<Product key={id} id={id} />
))}
</div>
<div>{products}</div>
<div>
<Basket />
</div>
Expand All @@ -39,4 +58,13 @@ class Menu extends Component {
}
}

export default Menu;
const mapStateToProps = (state) => ({
loading: menuLoadingSelector(state),
loaded: menuLoadedSelector(state),
});

const mapDispathcToProps = (dispatch, props) => ({
loadProducts: () => dispatch(loadProducts(props.id)),
});

export default connect(mapStateToProps, mapDispathcToProps)(Menu);
6 changes: 6 additions & 0 deletions src/components/product/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Product.propTypes = {
decrement: PropTypes.func,
};

Product.defaultProps = {
product: {
ingredients: [],
},
};

const mapStateToProps = (state, props) => ({
amount: amountSelector(state, props),
product: productSelector(state, props),
Expand Down
29 changes: 25 additions & 4 deletions src/components/restaurant/restaurant.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Menu from '../menu';
Expand All @@ -9,9 +9,18 @@ import Tabs from '../tabs';
import {
averageRatingSelector,
restaurantSelector,
reviewsLoadedSelector,
reviewsLoadingSelector,
} from '../../redux/selectors';
import { loadReviews } from '../../redux/actions';

const Restaurant = ({ restaurant, averageRating }) => {
const Restaurant = ({
restaurant,
averageRating,
loadReviews,
loading,
loaded,
}) => {
const { id, name, menu, reviews } = restaurant;

const [activeTab, setActiveTab] = useState('menu');
Expand All @@ -21,13 +30,17 @@ const Restaurant = ({ restaurant, averageRating }) => {
{ id: 'reviews', label: 'Reviews' },
];

useEffect(() => {
if (!loading) loadReviews(id); // eslint-disable-next-line
}, [id]);

return (
<div>
<Banner heading={name}>
<Rate value={averageRating} />
</Banner>
<Tabs tabs={tabs} activeId={activeTab} onChange={setActiveTab} />
{activeTab === 'menu' && <Menu menu={menu} key={id} />}
{activeTab === 'menu' && <Menu menu={menu} id={id} key={id} />}
{activeTab === 'reviews' && <Reviews reviews={reviews} restId={id} />}
</div>
);
Expand All @@ -41,11 +54,19 @@ Restaurant.propTypes = {
reviews: PropTypes.array,
}).isRequired,
averageRating: PropTypes.number,
loading: PropTypes.bool,
loaded: PropTypes.bool,
};

const mapStateToProps = (state, props) => ({
restaurant: restaurantSelector(state, props),
averageRating: averageRatingSelector(state, props),
loading: reviewsLoadingSelector(state),
loaded: reviewsLoadedSelector(state),
});

export default connect(mapStateToProps)(Restaurant);
const mapDispatchToProps = {
loadReviews,
};

export default connect(mapStateToProps, mapDispatchToProps)(Restaurant);
1 change: 1 addition & 0 deletions src/components/reviews/review/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Review.propTypes = {

Review.defaultProps = {
user: 'Anonymous',
rating: 0,
};

// const mapStateToProps = (state, props) => ({
Expand Down
53 changes: 44 additions & 9 deletions src/components/reviews/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,41 @@ import Review from './review';
import ReviewForm from './review-form';
import styles from './reviews.module.css';

import { loadReviews } from '../../redux/actions';
import Loader from '../loader';

const Reviews = ({ reviews, restId, loadReviews }) => {
import { loadUsers } from '../../redux/actions';
import {
reviewsLoadedSelector,
reviewsLoadingSelector,
usersLoadedSelector,
usersLoadingSelector,
} from '../../redux/selectors';

const Reviews = ({
reviews,
restId,
loadUsers,
usersLoading,
usersLoaded,
reviewsLoading,
reviewsLoaded,
}) => {
useEffect(() => {
loadReviews(restId);
}, [restId, loadReviews]);
if (!usersLoading && !usersLoaded) loadUsers();
}, [usersLoading, usersLoaded, loadUsers]);

const reviewsEls =
reviewsLoading || usersLoading ? (
<Loader />
) : !reviewsLoaded || !usersLoaded ? (
'no data :('
) : (
reviews.map((id) => <Review key={id} id={id} />)
);

return (
<div className={styles.reviews}>
{reviews.map((id) => (
<Review key={id} id={id} />
))}
{reviewsEls}
<ReviewForm restId={restId} />
</div>
);
Expand All @@ -25,10 +48,22 @@ const Reviews = ({ reviews, restId, loadReviews }) => {
Reviews.propTypes = {
restId: PropTypes.string,
reviews: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
loadUsers: PropTypes.func.isRequired,
usersLoading: PropTypes.bool,
usersLoaded: PropTypes.bool,
reviewsLoading: PropTypes.bool,
reviewsLoaded: PropTypes.bool,
};

const mapStateToProps = (state) => ({
usersLoading: usersLoadingSelector(state),
usersLoaded: usersLoadedSelector(state),
reviewsLoading: reviewsLoadingSelector(state),
reviewsLoaded: reviewsLoadedSelector(state),
});

const mapDispatchToProps = {
loadReviews,
loadUsers,
};

export default connect(null, mapDispatchToProps)(Reviews);
export default connect(mapStateToProps, mapDispatchToProps)(Reviews);
Loading