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

HT5 #70

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

HT5 #70

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/components/menu/menu.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Product from '../product';
import Basket from '../basket';

import { loadProducts } from '../../redux/actions';
import {
productsKeysListSelector,
productsLoadingSelector,
isProductsLoadedSelector,
} from '../../redux/selectors';
import Loader from '../loader';
import styles from './menu.module.css';

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

state = { error: null };
@@ -16,17 +23,27 @@ class Menu extends React.Component {
this.setState({ error });
}

componentDidMount() {
if (!this.props.loading && !this.props.loaded) {
this.props.loadProducts(this.props.id);
}
}
componentDidUpdate() {
if (!this.props.loading && !this.props.loaded) {
this.props.loadProducts(this.props.id);
}
}

render() {
const { menu } = this.props;
if (this.props.loading || !this.props.loaded) return <Loader />;

if (this.state.error) {
return <p>В этом ресторане меню не доступно</p>;
}

return (
<div className={styles.menu}>
<div>
{menu.map((id) => (
{this.props.menu.map((id) => (
<Product key={id} id={id} />
))}
</div>
@@ -38,4 +55,11 @@ class Menu extends React.Component {
}
}

export default Menu;
export default connect(
(state, props) => ({
menu: productsKeysListSelector(state, props),
loading: productsLoadingSelector(state),
loaded: isProductsLoadedSelector(state, props),
}),
{ loadProducts }
)(Menu);
9 changes: 5 additions & 4 deletions src/components/restaurant/restaurant.js
Original file line number Diff line number Diff line change
@@ -8,12 +8,12 @@ import Rate from '../rate';
import Tabs from '../tabs';
import { averageRatingSelector } from '../../redux/selectors';

const Restaurant = ({ id, name, menu, reviews, averageRating }) => {
const Restaurant = ({ id, name, reviews, averageRating }) => {
const tabs = [
{ title: 'Menu', content: <Menu menu={menu} /> },
{ title: 'Menu', content: <Menu id={id} /> },
{
title: 'Reviews',
content: <Reviews reviews={reviews} restaurantId={id} />,
content: <Reviews reviews={reviews} id={id} />,
},
];

@@ -36,5 +36,6 @@ Restaurant.propTypes = {
};

export default connect((state, props) => ({
averageRating: averageRatingSelector(state, props),
// TODO
averageRating: 5,
}))(Restaurant);
44 changes: 36 additions & 8 deletions src/components/reviews/reviews.js
Original file line number Diff line number Diff line change
@@ -3,28 +3,56 @@ import PropTypes from 'prop-types';
import Review from './review';
import ReviewForm from './review-form';
import styles from './reviews.module.css';
import Loader from '../loader';
import { connect } from 'react-redux';
import {
reviewsKeysListSelector,
isReviewsLoadedSelector,
reviewsLoadingSelector,
usersLoadingSelector,
usersLoadedSelector,
} from '../../redux/selectors';
import { loadReviews, loadUsers } from '../../redux/actions';

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

const Reviews = ({ reviews, restaurantId, loadReviews }) => {
const Reviews = ({
reviews,
id,
loadReviews,
loadUsers,
loading,
loaded,
loadingUsers,
loadedUsers,
}) => {
useEffect(() => {
loadReviews(restaurantId);
}, [loadReviews, restaurantId]);
if (!loading && !loaded) loadReviews(id);
if (!loadingUsers && !loadedUsers) loadUsers();
}, [loadReviews, id]);

if (loading || !loaded || loadingUsers || !loadedUsers) return <Loader />;

return (
<div className={styles.reviews}>
{reviews.map((id) => (
<Review key={id} id={id} />
))}
<ReviewForm restaurantId={restaurantId} />
<ReviewForm restaurantId={id} />
</div>
);
};

Reviews.propTypes = {
restaurantId: PropTypes.string.isRequired,
id: PropTypes.string.isRequired,
reviews: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
};

export default connect(null, { loadReviews })(Reviews);
export default connect(
(state, props) => ({
reviews: reviewsKeysListSelector(state, props), // get only reviews by restaurant id
loading: reviewsLoadingSelector(state),
loaded: isReviewsLoadedSelector(state, props),
loadingUsers: usersLoadingSelector(state),
loadedUsers: usersLoadedSelector(state),
}),
{ loadReviews, loadUsers }
)(Reviews);
Loading