Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

vskosp
Copy link

@vskosp vskosp commented Dec 4, 2020

No description provided.

@@ -9,3 +10,36 @@ export const getById = (selector, defaultValue) =>
(_, props) => props.id,
(entity, id) => entity[id] || defaultValue
);

export const listByIdReducer = (actionType) => (state = {}, action) => {
Copy link
Owner

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];
Copy link
Owner

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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут еще нужно проверить поменялся ли restaurantId, чтобы логика из loadProducts вызывалась только при смене ресторана

Copy link
Author

@vskosp vskosp Dec 4, 2020

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();
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

сейчас да, но код часто меняется и это менее очевидно, чем вызывать это только при смене ресторана. То есть если я зайду в этот код, через 3 месяца, я хочу понять что тут происходит и какая логика заложена. Я хочу в коде видеть, буквально "если у нас поменялся ресторан идем грузить продукты".

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

явное всегда лучше неявного

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

согласен

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants