diff --git a/src/components/menu/menu.js b/src/components/menu/menu.js index 4ee1ded..efa7cee 100644 --- a/src/components/menu/menu.js +++ b/src/components/menu/menu.js @@ -3,12 +3,19 @@ import PropTypes from 'prop-types'; import Product from '../product'; import Basket from '../basket'; +import Loader from '../loader'; import styles from './menu.module.css'; +import { connect } from 'react-redux'; +import { loadProducts } from '../../redux/actions'; +import { menuLoadedSelector, menuLoadingSelector } from '../../redux/selectors'; class Menu extends Component { static propTypes = { + id: PropTypes.string.isRequired, menu: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired, + loading: PropTypes.bool, + loadProducts: PropTypes.func.isRequired, }; state = { error: null }; @@ -17,20 +24,32 @@ class Menu extends Component { this.setState({ error }); } + componentDidMount({ prevId } = {}) { + const { id, loading, loadProducts } = this.props; + + if ((!!prevId && prevId === id) || loading) return; + + loadProducts(id); + } + render() { - const { menu } = this.props; + const { menu, loading, loaded } = this.props; if (this.state.error) { return

Меню этого ресторана сейчас недоступно :(

; } + const products = loading ? ( + + ) : !loaded ? ( + 'no data :(' + ) : ( + menu.map((id) => ) + ); + return (
-
- {menu.map((id) => ( - - ))} -
+
{products}
@@ -39,4 +58,13 @@ class Menu extends Component { } } -export default Menu; +const mapStateToProps = (state) => ({ + loading: menuLoadingSelector(state), + loaded: menuLoadedSelector(state), +}); + +const mapDispathcToProps = (dispatch, props) => ({ + loadProducts: () => dispatch(loadProducts(props.id)), +}); + +export default connect(mapStateToProps, mapDispathcToProps)(Menu); diff --git a/src/components/product/product.js b/src/components/product/product.js index 333ba2b..df87528 100644 --- a/src/components/product/product.js +++ b/src/components/product/product.js @@ -56,6 +56,12 @@ Product.propTypes = { decrement: PropTypes.func, }; +Product.defaultProps = { + product: { + ingredients: [], + }, +}; + const mapStateToProps = (state, props) => ({ amount: amountSelector(state, props), product: productSelector(state, props), diff --git a/src/components/restaurant/restaurant.js b/src/components/restaurant/restaurant.js index 51fe01e..304c01c 100644 --- a/src/components/restaurant/restaurant.js +++ b/src/components/restaurant/restaurant.js @@ -1,4 +1,4 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import Menu from '../menu'; @@ -9,9 +9,18 @@ import Tabs from '../tabs'; import { averageRatingSelector, restaurantSelector, + reviewsLoadedSelector, + reviewsLoadingSelector, } from '../../redux/selectors'; +import { loadReviews } from '../../redux/actions'; -const Restaurant = ({ restaurant, averageRating }) => { +const Restaurant = ({ + restaurant, + averageRating, + loadReviews, + loading, + loaded, +}) => { const { id, name, menu, reviews } = restaurant; const [activeTab, setActiveTab] = useState('menu'); @@ -21,13 +30,17 @@ const Restaurant = ({ restaurant, averageRating }) => { { id: 'reviews', label: 'Reviews' }, ]; + useEffect(() => { + if (!loading) loadReviews(id); // eslint-disable-next-line + }, [id]); + return (
- {activeTab === 'menu' && } + {activeTab === 'menu' && } {activeTab === 'reviews' && }
); @@ -41,11 +54,19 @@ Restaurant.propTypes = { reviews: PropTypes.array, }).isRequired, averageRating: PropTypes.number, + loading: PropTypes.bool, + loaded: PropTypes.bool, }; const mapStateToProps = (state, props) => ({ restaurant: restaurantSelector(state, props), averageRating: averageRatingSelector(state, props), + loading: reviewsLoadingSelector(state), + loaded: reviewsLoadedSelector(state), }); -export default connect(mapStateToProps)(Restaurant); +const mapDispatchToProps = { + loadReviews, +}; + +export default connect(mapStateToProps, mapDispatchToProps)(Restaurant); diff --git a/src/components/reviews/review/review.js b/src/components/reviews/review/review.js index a84c9f0..59c2c76 100644 --- a/src/components/reviews/review/review.js +++ b/src/components/reviews/review/review.js @@ -32,6 +32,7 @@ Review.propTypes = { Review.defaultProps = { user: 'Anonymous', + rating: 0, }; // const mapStateToProps = (state, props) => ({ diff --git a/src/components/reviews/reviews.js b/src/components/reviews/reviews.js index 8dc5628..64a5976 100644 --- a/src/components/reviews/reviews.js +++ b/src/components/reviews/reviews.js @@ -5,18 +5,41 @@ import Review from './review'; import ReviewForm from './review-form'; import styles from './reviews.module.css'; -import { loadReviews } from '../../redux/actions'; +import Loader from '../loader'; -const Reviews = ({ reviews, restId, loadReviews }) => { +import { loadUsers } from '../../redux/actions'; +import { + reviewsLoadedSelector, + reviewsLoadingSelector, + usersLoadedSelector, + usersLoadingSelector, +} from '../../redux/selectors'; + +const Reviews = ({ + reviews, + restId, + loadUsers, + usersLoading, + usersLoaded, + reviewsLoading, + reviewsLoaded, +}) => { useEffect(() => { - loadReviews(restId); - }, [restId, loadReviews]); + if (!usersLoading && !usersLoaded) loadUsers(); + }, [usersLoading, usersLoaded, loadUsers]); + + const reviewsEls = + reviewsLoading || usersLoading ? ( + + ) : !reviewsLoaded || !usersLoaded ? ( + 'no data :(' + ) : ( + reviews.map((id) => ) + ); return (
- {reviews.map((id) => ( - - ))} + {reviewsEls}
); @@ -25,10 +48,22 @@ const Reviews = ({ reviews, restId, loadReviews }) => { Reviews.propTypes = { restId: PropTypes.string, reviews: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired, + loadUsers: PropTypes.func.isRequired, + usersLoading: PropTypes.bool, + usersLoaded: PropTypes.bool, + reviewsLoading: PropTypes.bool, + reviewsLoaded: PropTypes.bool, }; +const mapStateToProps = (state) => ({ + usersLoading: usersLoadingSelector(state), + usersLoaded: usersLoadedSelector(state), + reviewsLoading: reviewsLoadingSelector(state), + reviewsLoaded: reviewsLoadedSelector(state), +}); + const mapDispatchToProps = { - loadReviews, + loadUsers, }; -export default connect(null, mapDispatchToProps)(Reviews); +export default connect(mapStateToProps, mapDispatchToProps)(Reviews); diff --git a/src/fixtures.js b/src/fixtures.js deleted file mode 100644 index ed35fc6..0000000 --- a/src/fixtures.js +++ /dev/null @@ -1,334 +0,0 @@ -export const restaurants = [ - { - id: 'a757a0e9-03c1-4a2a-b384-8ac21dbe2fb2', - name: 'Dishoom', - menu: [ - { - id: 'd75f762a-eadd-49be-8918-ed0daa8dd024', - name: 'Chicken tikka masala', - price: 12, - ingredients: ['chicken', 'rice'], - }, - { - id: 'c3cb8f92-a2ed-4716-92a1-b6ea813e9049', - name: 'Naan', - price: 3, - ingredients: ['bread'], - }, - { - id: 'bd129641-c0eb-432b-84b6-8b81d2930358', - name: 'Samosa', - price: 8, - ingredients: ['chicken', 'bread'], - }, - ], - reviews: [ - { - id: '5909796d-5030-4e36-adec-68b8f9ec2d96', - user: 'Antony', - text: 'Not bad', - rating: 5, - }, - { - id: '429dea85-11dd-4054-a31e-c60c92e17255', - user: 'Sam', - text: 'No burgers', - rating: 3, - }, - ], - }, - { - id: 'bb8afbec-2fec-491f-93e9-7f13950dd80b', - name: 'Homeslice', - menu: [ - { - id: '25402233-0095-49ea-9939-1e67ed89ffb9', - name: 'Margarita', - price: 9, - ingredients: ['bread', 'cheese', 'tomatoes'], - }, - { - id: '90902233-0095-49ea-9939-1e67ed89ffb9', - name: 'Chef pizza', - price: 10, - ingredients: ['bread', 'cheese', 'tomatoes', 'chicken'], - }, - ], - reviews: [ - { - id: '53b642d7-5e86-4717-a466-0640a1dee076', - user: 'Diana', - text: 'Perfect Margarita', - rating: 5, - }, - { - id: 'c27ab88e-375c-4e98-aa94-8a180150a797', - user: 'Sam', - text: 'No burgers again. But Chef Pizza is the best one', - rating: 4, - }, - { - id: 'abc0c5e1-cd57-4f0a-99d9-00e6b4533b3a', - user: 'Lolly', - text: 'Good for lunch', - rating: 5, - }, - ], - }, - { - id: '982bfbce-c5e0-41a0-9f99-d5c20ecee49d', - name: 'Fabrique', - menu: [ - { - id: '08c9ffa0-d003-4310-9e15-20978743296e', - name: 'Cinnamon buns', - price: 5, - ingredients: ['bread'], - }, - { - id: '64a4967c-2080-4a99-9074-4655a4569a95', - name: 'Semlor', - price: 2, - ingredients: ['bread', 'cream'], - }, - { - id: '4bc8528e-26d1-46c3-a522-8e18d10c8c84', - name: 'Saffron bun', - price: 4, - ingredients: ['bread'], - }, - ], - reviews: [ - { - id: '53b642d7-5e86-4717-a466-0640a1dee076', - user: 'Agata', - text: 'Best bakery', - rating: 5, - }, - ], - }, - { - id: 'd9241927-09e1-44f3-8986-a76346869037', - name: 'Flat Iron', - menu: [ - { - id: '6c02c2ce-b868-4191-b4a7-8686429f4bac', - name: 'Flat Iron Steak', - price: 10, - ingredients: ['beef'], - }, - { - id: '99bb6fbb-e53b-4b7e-b9c2-23b63b77385d', - name: 'Flat Iron Burger', - price: 10, - ingredients: ['bread', 'beef'], - }, - ], - reviews: [ - { - id: '5db6247b-ab1c-49db-be1f-8dd27fd38b81', - user: 'Sam', - text: 'Finally! This place is amazing place for breakfast, lunch, dinner and supper', - rating: 5, - }, - { - id: '381b0c31-6360-43ff-80d1-581a116159d8', - user: 'Rebeca', - text: 'Meat here is extremely delicious', - rating: 5, - }, - ], - }, -]; - -export const normalizedRestaurants = [ - { - id: 'a757a0e9-03c1-4a2a-b384-8ac21dbe2fb2', - name: 'Dishoom', - menu: [ - 'd75f762a-eadd-49be-8918-ed0daa8dd024', - 'c3cb8f92-a2ed-4716-92a1-b6ea813e9049', - 'bd129641-c0eb-432b-84b6-8b81d2930358', - ], - reviews: [ - '5909796d-5030-4e36-adec-68b8f9ec2d96', - '429dea85-11dd-4054-a31e-c60c92e17255', - ], - }, - { - id: 'bb8afbec-2fec-491f-93e9-7f13950dd80b', - name: 'Homeslice', - menu: [ - '25402233-0095-49ea-9939-1e67ed89ffb9', - '90902233-0095-49ea-9939-1e67ed89ffb9', - ], - reviews: [ - '53b642d7-5e86-4717-a466-0640a1dee076', - 'c27ab88e-375c-4e98-aa94-8a180150a797', - 'abc0c5e1-cd57-4f0a-99d9-00e6b4533b3a', - ], - }, - { - id: '982bfbce-c5e0-41a0-9f99-d5c20ecee49d', - name: 'Fabrique', - menu: [ - '08c9ffa0-d003-4310-9e15-20978743296e', - '64a4967c-2080-4a99-9074-4655a4569a95', - '4bc8528e-26d1-46c3-a522-8e18d10c8c84', - ], - reviews: ['13b642d7-5e86-4717-a466-0640a1dee076'], - }, - { - id: 'd9241927-09e1-44f3-8986-a76346869037', - name: 'Flat Iron', - menu: [ - '6c02c2ce-b868-4191-b4a7-8686429f4bac', - '99bb6fbb-e53b-4b7e-b9c2-23b63b77385d', - ], - reviews: [ - '5db6247b-ab1c-49db-be1f-8dd27fd38b81', - '381b0c31-6360-43ff-80d1-581a116159d8', - ], - }, -]; - -export const normalizedProducts = [ - { - id: 'd75f762a-eadd-49be-8918-ed0daa8dd024', - name: 'Chicken tikka masala', - price: 12, - ingredients: ['chicken', 'rice'], - }, - { - id: 'c3cb8f92-a2ed-4716-92a1-b6ea813e9049', - name: 'Naan', - price: 3, - ingredients: ['bread'], - }, - { - id: 'bd129641-c0eb-432b-84b6-8b81d2930358', - name: 'Samosa', - price: 8, - ingredients: ['chicken', 'bread'], - }, - { - id: '25402233-0095-49ea-9939-1e67ed89ffb9', - name: 'Margarita', - price: 9, - ingredients: ['bread', 'cheese', 'tomatoes'], - }, - { - id: '90902233-0095-49ea-9939-1e67ed89ffb9', - name: 'Chef pizza', - price: 10, - ingredients: ['bread', 'cheese', 'tomatoes', 'chicken'], - }, - { - id: '08c9ffa0-d003-4310-9e15-20978743296e', - name: 'Cinnamon buns', - price: 5, - ingredients: ['bread'], - }, - { - id: '64a4967c-2080-4a99-9074-4655a4569a95', - name: 'Semlor', - price: 2, - ingredients: ['bread', 'cream'], - }, - { - id: '4bc8528e-26d1-46c3-a522-8e18d10c8c84', - name: 'Saffron bun', - price: 4, - ingredients: ['bread'], - }, - { - id: '6c02c2ce-b868-4191-b4a7-8686429f4bac', - name: 'Flat Iron Steak', - price: 10, - ingredients: ['beef'], - }, - { - id: '99bb6fbb-e53b-4b7e-b9c2-23b63b77385d', - name: 'Flat Iron Burger', - price: 10, - ingredients: ['bread', 'beef'], - }, -]; - -export const normalizedReviews = [ - { - id: '5909796d-5030-4e36-adec-68b8f9ec2d96', - userId: 'a304959a-76c0-4b34-954a-b38dbf310360', - text: 'Not bad', - rating: 5, - }, - { - id: '429dea85-11dd-4054-a31e-c60c92e17255', - userId: 'dfb982e9-b432-4b7d-aec6-7f6ff2e6af54', - text: 'No burgers', - rating: 3, - }, - { - id: '53b642d7-5e86-4717-a466-0640a1dee076', - userId: '20bed9b5-9c7b-4771-8221-75b74ed1904a', - text: 'Perfect Margarita', - rating: 5, - }, - { - id: 'c27ab88e-375c-4e98-aa94-8a180150a797', - userId: 'dfb982e9-b432-4b7d-aec6-7f6ff2e6af54', - text: 'No burgers again. But Chef Pizza is the best one', - rating: 4, - }, - { - id: 'abc0c5e1-cd57-4f0a-99d9-00e6b4533b3a', - userId: 'c3d4abd4-c3ef-46e1-8719-eb17db1d6e99', - text: 'Good for lunch', - rating: 5, - }, - { - id: '13b642d7-5e86-4717-a466-0640a1dee076', - userId: '52a63cc0-5a6f-41f3-9774-0161ea4c9b0c', - text: 'Best bakery', - rating: 5, - }, - { - id: '5db6247b-ab1c-49db-be1f-8dd27fd38b81', - userId: 'dfb982e9-b432-4b7d-aec6-7f6ff2e6af54', - text: 'Finally! This place is amazing place for breakfast, lunch, dinner and supper', - rating: 5, - }, - { - id: '381b0c31-6360-43ff-80d1-581a116159d8', - userId: '1547335a-ea18-4547-a73d-32bd6e9f651c', - text: 'Meat here is extremely delicious', - rating: 5, - }, -]; - -export const normalizedUsers = [ - { - id: 'a304959a-76c0-4b34-954a-b38dbf310360', - name: 'Antony', - }, - { - id: '20bed9b5-9c7b-4771-8221-75b74ed1904a', - name: 'Diana', - }, - { - id: 'c3d4abd4-c3ef-46e1-8719-eb17db1d6e99', - name: 'Lolly', - }, - { - id: '52a63cc0-5a6f-41f3-9774-0161ea4c9b0c', - name: 'Agata', - }, - { - id: '1547335a-ea18-4547-a73d-32bd6e9f651c', - name: 'Rebeca', - }, - { - id: 'dfb982e9-b432-4b7d-aec6-7f6ff2e6af54', - name: 'Sam', - }, -]; diff --git a/src/redux/actions.js b/src/redux/actions.js index ae2a5c2..2244a99 100644 --- a/src/redux/actions.js +++ b/src/redux/actions.js @@ -9,8 +9,26 @@ import { SUCCESS, FAILURE, LOAD_REVIEWS, + LOAD_PRODUCTS, + LOAD_USERS, } from './constants'; +const loadedItems = {}; + +const load = async ({ url, eventName, dispatch, ...rest }) => { + if (loadedItems[url]) return; + + dispatch({ type: eventName + REQUEST, ...rest }); + + try { + const data = await fetch(url).then((res) => res.json()); + dispatch({ type: eventName + SUCCESS, data, ...rest }); + loadedItems[url] = true; + } catch (error) { + dispatch({ type: eventName + FAILURE, error, ...rest }); + } +}; + export const increment = (id) => ({ type: INCREMENT, id }); export const decrement = (id) => ({ type: DECREMENT, id }); export const remove = (id) => ({ type: REMOVE, id }); @@ -32,15 +50,24 @@ export const loadRestaurants = () => ({ CallAPI: '/api/restaurants', }); +export const loadProducts = (id) => ({ + type: LOAD_PRODUCTS, + CallAPI: `/api/products?id=${id}`, +}); + export const loadReviews = (restId) => async (dispatch) => { - dispatch({ type: LOAD_REVIEWS + REQUEST, restId }); + await load({ + url: `/api/reviews?id=${restId}`, + eventName: LOAD_REVIEWS, + dispatch, + restId, + }); +}; - try { - const data = await fetch(`/api/reviews?id=${restId}`).then((res) => - res.json() - ); - dispatch({ type: LOAD_REVIEWS + SUCCESS, restId, data }); - } catch (error) { - dispatch({ type: LOAD_REVIEWS + FAILURE, restId, error }); - } +export const loadUsers = () => async (dispatch) => { + await load({ + url: '/api/users', + eventName: LOAD_USERS, + dispatch, + }); }; diff --git a/src/redux/constants.js b/src/redux/constants.js index ba58347..15285a9 100644 --- a/src/redux/constants.js +++ b/src/redux/constants.js @@ -6,6 +6,8 @@ export const CHANGE_RESTAURANT = 'CHANGE_RESTAURANT'; export const LOAD_RESTAURANTS = 'LOAD_RESTAURANTS'; export const LOAD_REVIEWS = 'LOAD_REVIEWS'; +export const LOAD_PRODUCTS = 'LOAD_PRODUCTS'; +export const LOAD_USERS = 'LOAD_USERS'; export const REQUEST = '_REQUEST'; export const SUCCESS = '_SUCCESS'; diff --git a/src/redux/middleware/api.js b/src/redux/middleware/api.js index da44ff0..dc4dad9 100644 --- a/src/redux/middleware/api.js +++ b/src/redux/middleware/api.js @@ -1,7 +1,9 @@ import { FAILURE, REQUEST, SUCCESS } from '../constants'; +const loadedItems = {}; + export default (store) => (next) => async (action) => { - if (!action.CallAPI) return next(action); + if (!action.CallAPI || loadedItems[action.CallAPI]) return next(action); const { CallAPI, type, ...rest } = action; @@ -10,6 +12,7 @@ export default (store) => (next) => async (action) => { try { const data = await fetch(CallAPI).then((res) => res.json()); next({ ...rest, type: type + SUCCESS, data }); + loadedItems[action.CallAPI] = true; } catch (error) { next({ ...rest, type: type + FAILURE, error }); } diff --git a/src/redux/reducer/products.js b/src/redux/reducer/products.js index 55bc808..1bf2f4f 100644 --- a/src/redux/reducer/products.js +++ b/src/redux/reducer/products.js @@ -1,10 +1,21 @@ -import { normalizedProducts } from '../../fixtures'; -import { arrToMap } from '../utils'; +import { LOAD_PRODUCTS, REQUEST, SUCCESS } from '../constants'; +import { arrToMap, getInitialState } from '../utils'; -export default (state = arrToMap(normalizedProducts), action) => { - const { type } = action; +const initialState = getInitialState(); + +export default (state = initialState, action) => { + const { type, data } = action; switch (type) { + case LOAD_PRODUCTS + REQUEST: + return { ...state, loading: true, loaded: false, error: null }; + case LOAD_PRODUCTS + SUCCESS: + return { + ...state, + entities: { ...state.entities, ...arrToMap(data) }, + loading: false, + loaded: true, + }; default: return state; } diff --git a/src/redux/reducer/restaurants.js b/src/redux/reducer/restaurants.js index 6241a68..6016fdc 100644 --- a/src/redux/reducer/restaurants.js +++ b/src/redux/reducer/restaurants.js @@ -7,14 +7,11 @@ import { REQUEST, SUCCESS, } from '../constants'; -import { arrToMap } from '../utils'; +import { arrToMap, getInitialState } from '../utils'; const initialState = { + ...getInitialState(), activeId: null, - loading: false, - loaded: false, - entities: {}, - error: null, }; export default (state = initialState, action) => { diff --git a/src/redux/reducer/reviews.js b/src/redux/reducer/reviews.js index e79ab91..e6f3906 100644 --- a/src/redux/reducer/reviews.js +++ b/src/redux/reducer/reviews.js @@ -1,16 +1,43 @@ -import { ADD_REVIEW } from '../constants'; -import { normalizedReviews } from '../../fixtures'; -import { arrToMap } from '../utils'; +import { + ADD_REVIEW, + LOAD_REVIEWS, + REQUEST, + SUCCESS, + FAILURE, +} from '../constants'; +import { arrToMap, getInitialState } from '../utils'; -export default (state = arrToMap(normalizedReviews), action) => { - const { type, review, reviewId, userId } = action; +const initialState = getInitialState(); + +export default (state = initialState, action) => { + const { type, review, reviewId, userId, data, error } = action; switch (type) { + case LOAD_REVIEWS + REQUEST: + return { ...state, loading: true, error: null }; + case LOAD_REVIEWS + SUCCESS: + return { + ...state, + activeId: data[0].id, + entities: arrToMap(data), + loading: false, + loaded: true, + }; + case LOAD_REVIEWS + FAILURE: + return { + ...state, + loading: false, + loaded: false, + error, + }; case ADD_REVIEW: const { text, rating } = review; return { ...state, - [reviewId]: { id: reviewId, userId, text, rating }, + entities: { + ...state.entities, + [reviewId]: { id: reviewId, userId, text, rating }, + }, }; default: return state; diff --git a/src/redux/reducer/users.js b/src/redux/reducer/users.js index 1143dff..1519bf0 100644 --- a/src/redux/reducer/users.js +++ b/src/redux/reducer/users.js @@ -1,15 +1,39 @@ import produce from 'immer'; -import { ADD_REVIEW } from '../constants'; -import { normalizedUsers } from '../../fixtures'; -import { arrToMap } from '../utils'; +import { + ADD_REVIEW, + LOAD_USERS, + REQUEST, + SUCCESS, + FAILURE, +} from '../constants'; +import { arrToMap, getInitialState } from '../utils'; -export default produce((draft = arrToMap(normalizedUsers), action) => { - const { type, review, userId } = action; +const initialState = getInitialState(); + +export default produce((draft = initialState, action) => { + const { type, review, userId, data, error } = action; switch (type) { + case LOAD_USERS + REQUEST: + return { ...draft, loading: true, error: null }; + case LOAD_USERS + SUCCESS: + return { + ...draft, + activeId: data[0].id, + entities: arrToMap(data), + loading: false, + loaded: true, + }; + case LOAD_USERS + FAILURE: + return { + ...draft, + loading: false, + loaded: false, + error, + }; case ADD_REVIEW: const { name } = review; - draft[userId] = { id: userId, name }; + draft.entities[userId] = { id: userId, name }; break; default: diff --git a/src/redux/selectors.js b/src/redux/selectors.js index 547712d..5704474 100644 --- a/src/redux/selectors.js +++ b/src/redux/selectors.js @@ -1,15 +1,24 @@ import { createSelector } from 'reselect'; const restaurantsSelector = (state) => state.restaurants.entities; -const productsSelector = (state) => state.products; +const productsSelector = (state) => state.products.entities; const orderSelector = (state) => state.order; -const reviewsSelector = (state) => state.reviews; -const usersSelector = (state) => state.users; +const reviewsSelector = (state) => state.reviews.entities; +const usersSelector = (state) => state.users.entities; export const activeIdRestaurantSelector = (state) => state.restaurants.activeId; export const restaurantsLoadingSelector = (state) => state.restaurants.loading; export const restaurantsLoadedSelector = (state) => state.restaurants.loaded; +export const menuLoadingSelector = (state) => state.products.loading; +export const menuLoadedSelector = (state) => state.products.loaded; + +export const reviewsLoadingSelector = (state) => state.reviews.loading; +export const reviewsLoadedSelector = (state) => state.reviews.loaded; + +export const usersLoadingSelector = (state) => state.users.loading; +export const usersLoadedSelector = (state) => state.users.loaded; + export const restaurantsListSelector = createSelector( restaurantsSelector, Object.values @@ -45,7 +54,7 @@ export const reviewWitUserSelector = createSelector( usersSelector, (review, users) => ({ ...review, - user: users[review.userId]?.name, + user: users[review?.userId]?.name, }) ); @@ -53,7 +62,7 @@ export const averageRatingSelector = createSelector( reviewsSelector, restaurantSelector, (reviews, restaurant) => { - const ratings = restaurant.reviews.map((id) => reviews[id].rating); + const ratings = restaurant.reviews.map((id) => reviews[id]?.rating ?? 0); return Math.round( ratings.reduce((acc, rating) => acc + rating) / ratings.length ); diff --git a/src/redux/utils.js b/src/redux/utils.js index 498aa5a..a6b0eb9 100644 --- a/src/redux/utils.js +++ b/src/redux/utils.js @@ -1,2 +1,9 @@ export const arrToMap = (arr) => arr.reduce((acc, item) => ({ ...acc, [item.id]: item }), {}); + +export const getInitialState = () => ({ + loading: false, + loaded: false, + entities: {}, + error: null, +});