-
Notifications
You must be signed in to change notification settings - Fork 13
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 #42
base: master
Are you sure you want to change the base?
HT4 #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,20 +5,16 @@ 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((id) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Object.keys(restaurants) должно быть в селекторах и мемоизировано |
||
title: restaurants[id].name, | ||
content: <Restaurant id={id} />, | ||
})); | ||
|
||
return <Tabs tabs={tabs} />; | ||
}; | ||
|
||
Restaurants.propTypes = { | ||
restaurants: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.string.isRequired, | ||
}).isRequired | ||
).isRequired, | ||
restaurants: PropTypes.object.isRequired, | ||
}; | ||
|
||
export default connect((state) => ({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
|
||
import Rate from '../../rate'; | ||
import styles from './review.module.css'; | ||
|
@@ -28,8 +29,13 @@ Review.propTypes = { | |
rating: PropTypes.number.isRequired, | ||
}; | ||
|
||
Review.defaultProps = { | ||
user: 'Anonymous', | ||
const mapStateToProps = (state, ownProps) => { | ||
const review = state.reviews[ownProps.id]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это все тоже нужно переносить в селекторы |
||
return { | ||
user: state.users[review.userId].name || 'Anonymous', | ||
text: review.text, | ||
rating: review.rating, | ||
}; | ||
}; | ||
|
||
export default Review; | ||
export default connect(mapStateToProps)(Review); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import { INCREMENT, DECREMENT, REMOVE } from './constants'; | ||
import { INCREMENT, DECREMENT, REMOVE, ADD_REVIEW } 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 addReview = (review) => ({ | ||
type: ADD_REVIEW, | ||
payload: { review }, | ||
}); |
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 ADD_REVIEW = 'ADD_REVIEW'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { ADD_REVIEW } from '../constants'; | ||
|
||
export default (store) => (next) => (action) => { | ||
if (action.type === ADD_REVIEW) { | ||
const userId = uuidv4(); | ||
const reviewId = uuidv4(); | ||
|
||
action.payload = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не стоит мутировать action, лучше создать новый |
||
review: { | ||
userId: userId, | ||
reviewId: reviewId, | ||
name: action.payload.review.name, | ||
text: action.payload.review.text, | ||
rating: action.payload.review.rating, | ||
restaurantId: action.payload.review.restaurantId, | ||
}, | ||
}; | ||
} | ||
|
||
next(action); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { normalizedUsers } from '../../fixtures'; | ||
import { ADD_REVIEW } from '../constants'; | ||
|
||
const defaultUsers = normalizedUsers.reduce( | ||
(acc, user) => ({ ...acc, [user.id]: user }), | ||
{} | ||
); | ||
|
||
export default (users = defaultUsers, action) => { | ||
const { type, payload } = action; | ||
|
||
switch (type) { | ||
case ADD_REVIEW: | ||
return { | ||
...users, | ||
[payload.review.userId]: { | ||
id: payload.review.userId, | ||
name: payload.review.name, | ||
}, | ||
}; | ||
default: | ||
return users; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { createSelector } from 'reselect'; | |
// const restaurantsSelector = (state) => state.restaurants; | ||
const orderSelector = (state) => state.order; | ||
const productsSelector = (state) => state.products; | ||
const reviewSelector = (state) => state.reviews; | ||
|
||
export const orderProductsSelector = createSelector( | ||
productsSelector, | ||
|
@@ -23,3 +24,14 @@ export const totalSelector = createSelector( | |
(orderProducts) => | ||
orderProducts.reduce((acc, { subtotal }) => acc + subtotal, 0) | ||
); | ||
|
||
export const averageRatingSelector = (state, props) => | ||
createSelector(reviewSelector, (reviews) => { | ||
const restaurantReviews = state.restaurants[props.id].reviews; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше не делать одновременно и обычный селектор и createSelector |
||
return Math.round( | ||
restaurantReviews.reduce( | ||
(acc, review) => reviews[review].rating + acc, | ||
0 | ||
) / restaurantReviews.length | ||
); | ||
})(state); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это нужно выносить в селекторы