-
Notifications
You must be signed in to change notification settings - Fork 20
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
HT-4: fix reviews & post new review #64
base: master
Are you sure you want to change the base?
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { INCREMENT, DECREMENT, REMOVE } from './constants'; | ||
import { INCREMENT, DECREMENT, REMOVE, REVIEW_ADD, RESTAURANT_ACTIVATE } 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: REVIEW_ADD, payload: review }); | ||
export const selectRestaurant = (id) => ({ type: RESTAURANT_ACTIVATE, payload: { id } }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export const INCREMENT = 'INCREMENT'; | ||
export const DECREMENT = 'DECREMENT'; | ||
export const REMOVE = 'REMOVE'; | ||
|
||
export const RESTAURANT_ACTIVATE = 'RESTAURANT_ACTIVATE'; | ||
export const REVIEW_ADD = 'REVIEW_ADD'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { REVIEW_ADD } from '../constants'; | ||
|
||
const review = (store) => (next) => (action) => { | ||
const { type, payload } = action; | ||
|
||
if (type === REVIEW_ADD) { | ||
next({ | ||
type: type, | ||
payload: { | ||
restaurantId: store.getState().activeRestaurant, | ||
userId: uuidv4(), | ||
id: uuidv4(), | ||
...payload | ||
} | ||
}); | ||
} else { | ||
next(action); | ||
} | ||
}; | ||
|
||
export default review; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { normalizedRestaurants } from '../../fixtures'; | ||
import { RESTAURANT_ACTIVATE } from '../constants'; | ||
|
||
const defaultActiveRestaurant = normalizedRestaurants[0]?.id; | ||
|
||
const reducer = (activeRestaurant = defaultActiveRestaurant, action) => { | ||
const { type, payload } = action; | ||
|
||
switch (type) { | ||
case RESTAURANT_ACTIVATE: | ||
return payload.id; | ||
default: | ||
return activeRestaurant; | ||
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. теперь знание о том, какой ресторан активный, храниться в двух местах - в этом редьюсере и в компоненте |
||
} | ||
}; | ||
|
||
export default reducer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { normalizedUsers } from '../../fixtures'; | ||
import { REVIEW_ADD } from '../constants'; | ||
|
||
const defaultUsers = normalizedUsers.reduce( | ||
(acc, user) => ({...acc, [user.id]: user}), | ||
{}); | ||
|
||
const reducer = (users = defaultUsers, action) => { | ||
const { type, payload } = action; | ||
|
||
switch (type) { | ||
case REVIEW_ADD: | ||
return { | ||
...users, | ||
[payload.userId]: { | ||
id: payload.userId, | ||
name: payload.name | ||
} | ||
}; | ||
default: | ||
return users; | ||
} | ||
}; | ||
|
||
export default reducer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import { createSelector } from 'reselect'; | ||
|
||
// const restaurantsSelector = (state) => state.restaurants; | ||
const restaurantsSelector = (state) => state.restaurants; | ||
const orderSelector = (state) => state.order; | ||
const productsSelector = (state) => state.products; | ||
const reviewsSelector = (state) => state.reviews; | ||
const restaurantReviewsSelector = (state, restaurantId) => state.restaurants[restaurantId].reviews; | ||
|
||
export const restaurantListSelector = createSelector( | ||
restaurantsSelector, | ||
restaurants => Object.keys(restaurants).map(id => restaurants[id]) | ||
); | ||
|
||
export const orderProductsSelector = createSelector( | ||
productsSelector, | ||
|
@@ -24,3 +31,15 @@ export const totalSelector = createSelector( | |
(orderProducts) => | ||
orderProducts.reduce((acc, { subtotal }) => acc + subtotal, 0) | ||
); | ||
|
||
export const makeRatingSelector = () => createSelector( | ||
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. а в чем смысл этой функции? 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. на случай, если он будет использоваться несколькими компонентами https://github.com/reduxjs/reselect#sharing-selectors-with-props-across-multiple-component-instances |
||
reviewsSelector, | ||
restaurantReviewsSelector, | ||
(reviews, restaurantReviews) => { | ||
const total = restaurantReviews | ||
.map(id => reviews[id]) | ||
.reduce((acc, { rating }) => acc + rating, 0); | ||
|
||
return Math.round(total / restaurantReviews.length); | ||
} | ||
); |
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.
это нужно выносить в селекторы