-
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
HT4 #66
base: master
Are you sure you want to change the base?
HT4 #66
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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, | ||
|
@@ -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, | ||
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. вот эту всю логику лучше описывать в селекторе, что-то вроде |
||
user: reviewsUsersSelector(state)[review.userId].name, | ||
rating: reviewsRestaurantSelector(state)[ownProps.id].rating, | ||
}; | ||
}; | ||
|
||
export default connect(mapStateToProps)(Review); |
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 } }); |
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'; |
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 }, | ||
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 и тут может буть ошибка, лучше это делать в каждом конкретно 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; |
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; |
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; |
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.
это нужно выносить в селекторы и мемоизировать.