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

homework-2021-12-10 #49

Open
wants to merge 1 commit 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
22 changes: 10 additions & 12 deletions src/components/restaurants/restaurants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import Restaurant from '../restaurant';
import Tabs from '../tabs';

function Restaurants({ restaurants }) {
const [activeId, setActiveId] = useState(restaurants[0].id);
const [activeId, setActiveId] = useState(
restaurants[Object.keys(restaurants)[0]].id
);

const tabs = useMemo(
() => restaurants.map(({ id, name }) => ({ id, label: name })),
() =>
Object.keys(restaurants).map((id) => ({
Copy link
Owner

Choose a reason for hiding this comment

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

это лучше вынести в селекторы

id,
label: restaurants[id].name,
})),
[restaurants]
);

const activeRestaurant = useMemo(
() => restaurants.find((restaurant) => restaurant.id === activeId),
[activeId, restaurants]
);
const activeRestaurant = restaurants[activeId];

return (
<div>
Expand All @@ -26,12 +29,7 @@ function Restaurants({ restaurants }) {
}

Restaurants.propTypes = {
restaurants: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string,
}).isRequired
).isRequired,
restaurants: PropTypes.object.isRequired,
};

const mapStateToProps = (state) => ({
Expand Down
24 changes: 13 additions & 11 deletions src/components/reviews/review/review.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
import PropTypes from 'prop-types';
import { connect } from 'react-redux';

import Rate from '../../rate';
import styles from './review.module.css';

const Review = ({ user, text, rating }) => (
const Review = ({ id, user, review }) => (
<div className={styles.review} data-id="review">
<div className={styles.content}>
<div>
<h4 className={styles.name} data-id="review-user">
{user}
{user.name}
</h4>
<p className={styles.comment} data-id="review-text">
{text}
{review.text}
</p>
</div>
<div className={styles.rate}>
<Rate value={rating} />
<Rate value={review.rating} />
</div>
</div>
</div>
);

Review.propTypes = {
user: PropTypes.string,
text: PropTypes.string,
rating: PropTypes.number.isRequired,
id: PropTypes.string.isRequired,
user: PropTypes.shape,
review: PropTypes.shape,
};

Review.defaultProps = {
user: 'Anonymous',
};
const mapStateToProps = (state, props) => ({
review: state.reviews[props.id],
user: state.users[state.reviews[props.id].userId],
});

export default Review;
export default connect(mapStateToProps)(Review);
2 changes: 1 addition & 1 deletion src/components/reviews/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Reviews = ({ reviews }) => {
return (
<div className={styles.reviews}>
{reviews.map((review) => (
<Review key={review.id} {...review} />
<Review key={review} id={review} />
))}
<ReviewForm />
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/redux/reducer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import order from './order';
import restaurants from './restaurants';
import products from './products';
import reviews from './reviews';
import users from './users';

export default combineReducers({
order,
restaurants,
products,
reviews,
users,
});
7 changes: 6 additions & 1 deletion src/redux/reducer/restaurants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { normalizedRestaurants as defaultRestaurants } from '../../fixtures';
import { normalizedRestaurants } from '../../fixtures';

const defaultRestaurants = normalizedRestaurants.reduce(
(acc, restaurant) => ({ ...acc, [restaurant.id]: restaurant }),
{}
);

export default (restaurants = defaultRestaurants, action) => {
const { type } = action;
Expand Down
7 changes: 6 additions & 1 deletion src/redux/reducer/reviews.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { normalizedReviews as defaultReviews } from '../../fixtures';
import { normalizedReviews } from '../../fixtures';

const defaultReviews = normalizedReviews.reduce(
(acc, review) => ({ ...acc, [review.id]: review }),
{}
);

export default (reviews = defaultReviews, action) => {
const { type } = action;
Expand Down
15 changes: 15 additions & 0 deletions src/redux/reducer/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { normalizedUsers } from '../../fixtures';

const defaultUsers = normalizedUsers.reduce(
(acc, user) => ({ ...acc, [user.id]: user }),
{}
);

export default (users = defaultUsers, action) => {
const { type } = action;

switch (type) {
default:
return users;
}
};