-
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
Ht4v2 #37
base: master
Are you sure you want to change the base?
Ht4v2 #37
Changes from all commits
b410d4f
ac2669a
b43e1fc
5904974
bed168a
e127914
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 |
---|---|---|
|
@@ -3,17 +3,24 @@ import useForm from '../../../hooks/use-form'; | |
|
||
import Rate from '../../rate'; | ||
import styles from './review-form.module.css'; | ||
import { connect } from 'react-redux'; | ||
import { connect, useSelector } from 'react-redux'; | ||
import Button from '../../button'; | ||
import { postReview } from '../../../redux/actions'; | ||
|
||
const INITIAL_VALUES = { name: '', text: '', rating: 5 }; | ||
const INITIAL_VALUES = { yourName: '', text: '', rating: 5 }; | ||
|
||
const ReviewForm = ({ onSubmit }) => { | ||
const { values, handlers, reset } = useForm(INITIAL_VALUES); | ||
|
||
const activeRestaurantId = useSelector( | ||
(state) => state.restaurants.activeRestaurantId | ||
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. это нужно выносить в селекторы |
||
); | ||
const reviewState = useSelector((state) => state.reviews); | ||
const { error, errorMessage } = reviewState; | ||
|
||
const handleSubmit = (ev) => { | ||
ev.preventDefault(); | ||
onSubmit(values); | ||
onSubmit(values, activeRestaurantId); | ||
reset(); | ||
}; | ||
|
||
|
@@ -25,8 +32,9 @@ const ReviewForm = ({ onSubmit }) => { | |
<input | ||
placeholder="Your name" | ||
className={styles.message} | ||
{...handlers.name} | ||
{...handlers.yourName} | ||
/> | ||
{error && <p className={styles.errorMessage}>{errorMessage}</p>} | ||
</div> | ||
<div className={styles.reviewFormItem}> | ||
<textarea | ||
|
@@ -42,7 +50,7 @@ const ReviewForm = ({ onSubmit }) => { | |
</span> | ||
</div> | ||
<div className={styles.publish}> | ||
<Button primary block> | ||
<Button error={error} primary block> | ||
PUBLISH REVIEW | ||
</Button> | ||
</div> | ||
|
@@ -51,6 +59,10 @@ const ReviewForm = ({ onSubmit }) => { | |
); | ||
}; | ||
|
||
export default connect(null, () => ({ | ||
onSubmit: (values) => console.log(values), // TODO | ||
}))(ReviewForm); | ||
const mapDispatchToProps = (dispatch) => { | ||
return { | ||
onSubmit: (values, activeRestaurantId) => | ||
dispatch(postReview(values, activeRestaurantId)), | ||
}; | ||
}; | ||
export default connect(null, mapDispatchToProps)(ReviewForm); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,3 +67,6 @@ | |
margin-bottom: 10px; | ||
} | ||
} | ||
.errorMessage { | ||
color: red; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
import { INCREMENT, DECREMENT, REMOVE } from './constants'; | ||
import { | ||
INCREMENT, | ||
DECREMENT, | ||
REMOVE, | ||
POST_REVIEW, | ||
SET_ACTIVE_RESTAURANT, | ||
} 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 postReview = ({ yourName, text, rating }, activeRestaurantId) => ({ | ||
type: POST_REVIEW, | ||
payload: { yourName, text, rating, activeRestaurantId }, | ||
}); | ||
export const setRestaurantActive = (id) => ({ | ||
type: SET_ACTIVE_RESTAURANT, | ||
payload: { id }, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const INCREMENT = 'INCREMENT'; | ||
export const DECREMENT = 'DECREMENT'; | ||
export const REMOVE = 'REMOVE'; | ||
export const POST_REVIEW = 'POST_REVIEW'; | ||
export const SET_ACTIVE_RESTAURANT = 'SET_ACTIVE_RESTAURANT'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import restaurants from '../reducer/restaurants'; | ||
|
||
export default (store) => (next) => (action) => { | ||
if (action.type === 'POST_REVIEW') { | ||
const newReviewId = uuidv4(); | ||
const newUserId = uuidv4(); | ||
action.payload.id = newReviewId; | ||
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, лучше создать новый |
||
action.payload.userId = newUserId; | ||
//action.payload.restaurantId = store.getState().restaurants.activeRestaurantId | ||
next(action); | ||
} else { | ||
next(action); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
import { normalizedRestaurants as defaultRestaurants } from '../../fixtures'; | ||
import { normalizedRestaurants } from '../../fixtures'; | ||
import { POST_REVIEW, SET_ACTIVE_RESTAURANT } from '../constants'; | ||
|
||
const defaultRestaurants = normalizedRestaurants.reduce( | ||
(acc, restaurant) => ({ | ||
...acc, | ||
[restaurant.id]: restaurant, | ||
}), | ||
{} | ||
); | ||
|
||
export default (restaurants = defaultRestaurants, action) => { | ||
const { type } = action; | ||
const { type, payload } = action; | ||
|
||
switch (type) { | ||
case SET_ACTIVE_RESTAURANT: | ||
return { ...restaurants, activeRestaurantId: payload.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. теперь знание о какой ресторан активный храниться в двух местах, в этом редьюсере и в компоненте Tabs, я расскажу на занятие какие могут возникнуть с этим проблемы |
||
break; | ||
case POST_REVIEW: | ||
if (!payload.yourName) { | ||
return { ...restaurants }; | ||
break; | ||
} | ||
const activeRestaurantId = payload.activeRestaurantId; | ||
const restaurant = restaurants[activeRestaurantId]; | ||
restaurant.reviews.push(payload.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 { | ||
...restaurants, | ||
[payload.activeRestaurantId]: restaurant, | ||
}; | ||
break; | ||
default: | ||
return restaurants; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,37 @@ | ||
import { normalizedReviews as defaultReviews } from '../../fixtures'; | ||
import { normalizedReviews } from '../../fixtures'; | ||
import { POST_REVIEW } from '../constants'; | ||
|
||
const defaultReviews = normalizedReviews.reduce( | ||
(acc, review) => ({ ...acc, [review.id]: review }), | ||
{} | ||
); | ||
defaultReviews.error = true; | ||
|
||
export default (reviews = defaultReviews, action) => { | ||
const { type } = action; | ||
const { type, payload } = action; | ||
|
||
switch (type) { | ||
case POST_REVIEW: | ||
if (!payload.yourName) { | ||
return { | ||
...reviews, | ||
error: true, | ||
errorMessage: 'name is required', | ||
text: payload.text, | ||
}; | ||
} | ||
return { | ||
...reviews, | ||
error: false, | ||
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. лучше для этого сделать на одну вложенность больше, чтобы на одном уровне не было разных типов данных (и ревью по id и дополнительные данные про ошибки), у нас это как раз будет сегодня на занятии |
||
errorMessage: '', | ||
[payload.id]: { | ||
id: payload.id, | ||
rating: payload.rating, | ||
text: payload.text, | ||
userId: payload.userId, | ||
}, | ||
}; | ||
break; | ||
default: | ||
return reviews; | ||
} | ||
|
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.
все это нужно выносить в селекторы, чтобы компонент занимался только рендером