-
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-5: fetch data asynchronously #71
base: master
Are you sure you want to change the base?
Conversation
@@ -9,3 +10,36 @@ export const getById = (selector, defaultValue) => | |||
(_, props) => props.id, | |||
(entity, id) => entity[id] || defaultValue | |||
); | |||
|
|||
export const listByIdReducer = (actionType) => (state = {}, action) => { |
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.
прикольное решение
return Math.round( | ||
ratings.reduce((acc, rating) => acc + rating) / ratings.length | ||
); | ||
} | ||
); | ||
|
||
const restaurantProductsSelector = (state, props) => state.products.listByRestaurant[props.restaurantId]; | ||
const restaurantReviewsSelector = (state, props) => state.reviews.listByRestaurant[props.restaurantId || props.id]; |
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.
лучше пропу переименовать, чтобы везде было, к примеру, props.restaurantId
} | ||
|
||
componentDidUpdate() { | ||
this.loadProducts(); |
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.
тут еще нужно проверить поменялся ли restaurantId, чтобы логика из loadProducts вызывалась только при смене ресторана
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.
она не будет вызываться для уже загруженных ресторанов из-за этой проверки (isLoading и isLoaded будут передаваться для текущего ресторана) https://github.com/koretskiyav/react-2020-11-13/pull/71/files#diff-4d8fc5329143e8a6d6c5486e865a6ac6ad20a19f415fbf280fab745160f398aeR62
loadProducts() {
const { isLoading, isLoaded, loadProducts } = this.props;
if (!isLoading && !isLoaded) loadProducts();
}
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.
сейчас да, но код часто меняется и это менее очевидно, чем вызывать это только при смене ресторана. То есть если я зайду в этот код, через 3 месяца, я хочу понять что тут происходит и какая логика заложена. Я хочу в коде видеть, буквально "если у нас поменялся ресторан идем грузить продукты".
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.
явное всегда лучше неявного
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.
согласен
No description provided.