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

HT4 #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

HT4 #66

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
16,517 changes: 16,517 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/components/restaurant/restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const Restaurant = ({ restaurant }) => {

const tabs = [
{ title: 'Menu', content: <Menu menu={menu} /> },
{ title: 'Reviews', content: <Reviews reviews={reviews} /> },
{
title: 'Reviews',
content: <Reviews reviews={reviews} restaurantId={restaurant.id} />,
},
];

return (
Expand Down
10 changes: 6 additions & 4 deletions src/components/restaurants/restaurants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import Restaurant from '../restaurant';
import Tabs from '../tabs';

const Restaurants = ({ restaurants }) => {
const tabs = restaurants.map((restaurant) => ({
title: restaurant.name,
content: <Restaurant restaurant={restaurant} />,
}));
const tabs = Object.keys(restaurants).map((restaurantId) => {
Copy link
Owner

Choose a reason for hiding this comment

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

это нужно выносить в селекторы и мемоизировать.

return {
title: restaurants[restaurantId].name,
content: <Restaurant restaurant={restaurants[restaurantId]} />,
};
});

return <Tabs tabs={tabs} />;
};
Expand Down
11 changes: 7 additions & 4 deletions src/components/reviews/review-form/review-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import Rate from '../../rate';
import styles from './review-form.module.css';
import { connect } from 'react-redux';
import Button from '../../button';
import { submitReview } from '../../../redux/actions';

const INITIAL_VALUES = { name: '', text: '', rate: 5 };

const ReviewForm = ({ onSubmit }) => {
const ReviewForm = ({ onSubmit, restaurantId }) => {
const { values, handlers, reset } = useForm(INITIAL_VALUES);

const handleSubmit = (ev) => {
ev.preventDefault();
onSubmit(values);
onSubmit({ ...values, restaurantId: restaurantId });
reset();
};

Expand Down Expand Up @@ -51,6 +52,8 @@ const ReviewForm = ({ onSubmit }) => {
);
};

export default connect(null, () => ({
onSubmit: (values) => console.log(values), // TODO
export default connect(null, (dispatch) => ({
onSubmit: (values) => {
return dispatch(submitReview(values));
},
}))(ReviewForm);
50 changes: 33 additions & 17 deletions src/components/reviews/review/review.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import React from 'react';
import PropTypes from 'prop-types';

import { connect } from 'react-redux';
import Rate from '../../rate';
import styles from './review.module.css';
import {
reviewsRestaurantSelector,
reviewsUsersSelector,
} 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 = ({ text, user, rating }) => {
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,
Expand All @@ -32,4 +38,14 @@ Review.defaultProps = {
user: 'Anonymous',
};

export default Review;
const mapStateToProps = (state, ownProps) => {
const review = reviewsRestaurantSelector(state)[ownProps.id];

return {
text: reviewsRestaurantSelector(state)[ownProps.id].text,
Copy link
Owner

Choose a reason for hiding this comment

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

вот эту всю логику лучше описывать в селекторе, что-то вроде reviewsRestaurantSelector(state, ownProps). На занятии расскажу почему.

user: reviewsUsersSelector(state)[review.userId].name,
rating: reviewsRestaurantSelector(state)[ownProps.id].rating,
};
};

export default connect(mapStateToProps)(Review);
9 changes: 5 additions & 4 deletions src/components/reviews/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import Review from './review';
import ReviewForm from './review-form';
import styles from './reviews.module.css';

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

<ReviewForm restaurantId={restaurantId} />
</div>
);
};
Expand Down
3 changes: 2 additions & 1 deletion src/redux/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { INCREMENT, DECREMENT, REMOVE } from './constants';
import { INCREMENT, DECREMENT, REMOVE, SUBMIT } from './constants';

export const increment = (id) => ({ type: INCREMENT, payload: { id } });
export const decrement = (id) => ({ type: DECREMENT, payload: { id } });
export const remove = (id) => ({ type: REMOVE, payload: { id } });
export const submitReview = (values) => ({ type: SUBMIT, payload: { values } });
1 change: 1 addition & 0 deletions src/redux/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const REMOVE = 'REMOVE';
export const SUBMIT = 'SUBMIT';
20 changes: 20 additions & 0 deletions src/redux/middleware/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { v4 as uuidv4 } from 'uuid';
import { SUBMIT } from '../constants';

const uuid = (store) => (next) => (action) => {
const {
payload: { values },
Copy link
Owner

Choose a reason for hiding this comment

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

такая структура может быть не у всех action и тут может буть ошибка, лучше это делать в каждом конкретно switch case, где понятно с каким екшеном мы работаем

type,
} = action;

switch (type) {
case SUBMIT:
const valuesWithId = { ...values, id: uuidv4(), userId: uuidv4() };
next({ ...action, payload: valuesWithId });
break;
default:
next(action);
}
};

export default uuid;
2 changes: 2 additions & 0 deletions src/redux/reducer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import order from './order';
import restaurants from './restaurants';
import products from './products';
import reviews from './reviews';
import users from './users';

const reducer = combineReducers({
order,
restaurants,
products,
reviews,
users,
});

export default reducer;
20 changes: 17 additions & 3 deletions src/redux/reducer/restaurants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { normalizedRestaurants as defaultRestaurants } from '../../fixtures';
import { normalizedRestaurants } from '../../fixtures';
import { SUBMIT } from '../constants';

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

const reducer = (restaurants = defaultRestaurants, action) => {
const { type, payload } = action;
switch (type) {
case SUBMIT:
const restaurantToUpdate = restaurants[payload.restaurantId];
return {
...restaurants,
[payload.restaurantId]: {
...restaurantToUpdate,
reviews: [...restaurantToUpdate.reviews, payload.id],
},
};
default:
return restaurants;
}
Expand Down
20 changes: 18 additions & 2 deletions src/redux/reducer/reviews.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { normalizedReviews as defaultReviews } from '../../fixtures';
import { normalizedReviews } from '../../fixtures';
import { SUBMIT } from '../constants';

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

const reducer = (reviews = defaultReviews, action) => {
const { type } = action;
const { type, payload } = action;

switch (type) {
case SUBMIT:
return {
...reviews,
[payload.id]: {
id: payload.id,
userId: payload.userId,
text: payload.text,
rating: payload.rate,
},
};
default:
return reviews;
}
Expand Down
25 changes: 25 additions & 0 deletions src/redux/reducer/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { normalizedUsers } from '../../fixtures';
import { SUBMIT } from '../constants';

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

const reducer = (users = defaultUsers, action) => {
const { type, payload } = action;
switch (type) {
case SUBMIT:
return {
...users,
[payload.userId]: {
id: payload.userId,
name: payload.name,
},
};
default:
return users;
}
};

export default reducer;
12 changes: 12 additions & 0 deletions src/redux/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createSelector } from 'reselect';
// const restaurantsSelector = (state) => state.restaurants;
const orderSelector = (state) => state.order;
const productsSelector = (state) => state.products;
const reviewsSelector = (state) => state.reviews;
const usersSelector = (state) => state.users;

export const orderProductsSelector = createSelector(
productsSelector,
Expand All @@ -24,3 +26,13 @@ export const totalSelector = createSelector(
(orderProducts) =>
orderProducts.reduce((acc, { subtotal }) => acc + subtotal, 0)
);

export const reviewsRestaurantSelector = createSelector(
reviewsSelector,
(reviews) => reviews
);

export const reviewsUsersSelector = createSelector(
usersSelector,
(users) => users
);
3 changes: 2 additions & 1 deletion src/redux/store.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { applyMiddleware, createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import logger from './middleware/logger';
import uuid from './middleware/uuid';

import reducer from './reducer';

const store = createStore(
reducer,
composeWithDevTools(applyMiddleware(logger))
composeWithDevTools(applyMiddleware(logger, uuid))
);

export default store;