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

Lesson5 #70

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
69 changes: 38 additions & 31 deletions src/components/menu/menu.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
import { Component } from 'react';
import { connect } from 'react-redux';
import { useEffect } from 'react';
import PropTypes from 'prop-types';

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

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

class Menu extends Component {
static propTypes = {
menu: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
};
const Menu = ({ menu, restId, loadProducts, loading, loaded }) => {
useEffect(() => {
if (!loading && !loaded) loadProducts(restId);
}, [restId, loadProducts, loading, loaded]);

state = { error: null };
if (loading) return <Loader />;
if (!loaded) return 'No data :(';

componentDidCatch(error) {
this.setState({ error });
}
return (
<div className={styles.menu}>
<div>
{menu.map((id) => (
<Product key={id} id={id} />
))}
</div>
<div>
<Basket />
</div>
</div>
);
};

render() {
const { menu } = this.props;
Menu.propTypes = {
menu: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
};

if (this.state.error) {
return <p>Меню этого ресторана сейчас недоступно :(</p>;
}
const mapStateToProps = (state, props) => ({
loading: productsLoadingSelector(state, props),
loaded: productsLoadedSelector(state, props),
});

return (
<div className={styles.menu}>
<div>
{menu.map((id) => (
<Product key={id} id={id} />
))}
</div>
<div>
<Basket />
</div>
</div>
);
}
}
const mapDispatchToProps = {
loadProducts,
};

export default Menu;
export default connect(mapStateToProps, mapDispatchToProps)(Menu);
47 changes: 37 additions & 10 deletions src/components/restaurant/restaurant.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
import { useState } from 'react';
import { useState, useEffect, useMemo } from 'react';
import { connect } 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 Loader from '../loader';
import {
averageRatingSelector,
restaurantSelector,
reviewsLoadedSelector,
reviewsLoadingSelector,
} from '../../redux/selectors';
import { loadReviews } from '../../redux/actions';

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

const [activeTab, setActiveTab] = useState('menu');

const tabs = [
{ id: 'menu', label: 'Menu' },
{ id: 'reviews', label: 'Reviews' },
];

useEffect(() => {
if (!reviwesLoading && !reviewsLoaded) loadReviews(id);
}, [id, loadReviews, reviwesLoading, reviewsLoaded]);

const rateComponent = useMemo(() => {
Copy link
Owner

Choose a reason for hiding this comment

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

useMemo стоит использовать именно для хранения данных, а не JSX разметки

if (reviwesLoading) return <Loader />;
if (!reviewsLoaded) return <Rate value={0} />;
return <Rate value={averageRating} />;
}, [reviwesLoading, reviewsLoaded, averageRating]);

const reviewsComponent = useMemo(() => {
if (reviwesLoading) return <Loader />;
if (!reviewsLoaded) return <Reviews reviews={[]} restId={id} />;
return <Reviews reviews={reviews} restId={id} />;
}, [reviwesLoading, reviewsLoaded, id, reviews]);

return (
<div>
<Banner heading={name}>
<Rate value={averageRating} />
</Banner>
<Banner heading={name}>{rateComponent}</Banner>
<Tabs tabs={tabs} activeId={activeTab} onChange={setActiveTab} />
{activeTab === 'menu' && <Menu menu={menu} key={id} />}
{activeTab === 'reviews' && <Reviews reviews={reviews} restId={id} />}
{activeTab === 'menu' && <Menu menu={menu} restId={id} />}
{activeTab === 'reviews' && reviewsComponent}
</div>
);
};
Expand All @@ -46,6 +68,11 @@ Restaurant.propTypes = {
const mapStateToProps = (state, props) => ({
restaurant: restaurantSelector(state, props),
averageRating: averageRatingSelector(state, props),
reviwesLoading: reviewsLoadingSelector(state, props),
reviewsLoaded: reviewsLoadedSelector(state, props),
});

export default connect(mapStateToProps)(Restaurant);
const mapDispatchToProps = {
loadReviews,
};
export default connect(mapStateToProps, mapDispatchToProps)(Restaurant);
25 changes: 19 additions & 6 deletions src/components/reviews/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ 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 {
usersLoadingSelector,
usersLoadedSelector,
} from '../../redux/selectors';

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

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

if (usersLoading) return <Loader />;
if (!usersLoaded) return 'No data :(';

return (
<div className={styles.reviews}>
Expand All @@ -27,8 +35,13 @@ Reviews.propTypes = {
reviews: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
};

const mapStateToProps = (state, props) => ({
usersLoading: usersLoadingSelector(state),
usersLoaded: usersLoadedSelector(state),
});

const mapDispatchToProps = {
loadReviews,
loadUsers,
};

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