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

Ht6 #53

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Ht6 #53

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/pages/enum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const TABS_ENUM = {
MENU: 'menu',
REVIEWS: 'reviews',
};

export default TABS_ENUM;
38 changes: 38 additions & 0 deletions src/pages/restaurant-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import PropTypes from 'prop-types';
import { restaurantsListSelector } from '../redux/selectors';
import Menu from '../components/menu';
import Reviews from '../components/reviews';
import Restaurants from '../components/restaurants';
import TABS_ENUM from './enum';

const RestaurantPage = ({ restaurants, match }) => {
const { restId, tab } = match.params;
const restaurant = restaurants.find((restaurant) => restaurant.id === restId);
const { menu, reviews } = restaurant;

switch (tab) {
case TABS_ENUM.MENU:
return <Menu menu={menu} restaurantId={restId} />;
case TABS_ENUM.REVIEWS:
return <Reviews reviews={reviews} restaurantId={restId} />;
default:
return <Restaurants restaurants={restaurants} match={match} />;
}
};

RestaurantPage.propTypes = {
restaurants: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
}).isRequired
).isRequired,
};

export default connect(
createStructuredSelector({
restaurants: restaurantsListSelector,
})
)(RestaurantPage);
10 changes: 8 additions & 2 deletions src/pages/restaurants-page.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { Route } from 'react-router-dom';
import { Route, Link, Switch } from 'react-router-dom';
import { createStructuredSelector } from 'reselect';
import Restaurants from '../components/restaurants';
import Loader from '../components/loader';
Expand All @@ -11,6 +11,7 @@ import {
restaurantsLoadingSelector,
} from '../redux/selectors';
import { loadRestaurants } from '../redux/actions';
import RestaurantPage from './restaurant-page';

function RestaurantsPage({ loading, loaded, loadRestaurants, match }) {
useEffect(() => {
Expand All @@ -28,7 +29,12 @@ function RestaurantsPage({ loading, loaded, loadRestaurants, match }) {
);
}

return <Route path="/restaurants/:restId" component={Restaurants} />;
return (
<Switch>
<Route path="/restaurants/:restId" exact component={Restaurants} />;
<Route path="/restaurants/:restId/:tab" component={RestaurantPage} />;
</Switch>
);
}

export default connect(
Expand Down
12 changes: 12 additions & 0 deletions src/redux/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ export const restaurantsListSelector = createSelector(
Object.values
);

export const restaurantIdByProductIdSelector = (state, props) =>
Object.values(restaurantsSelector(state)).find((rest) =>
Copy link
Owner

Choose a reason for hiding this comment

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

вот это сложное вычисление будет вызываться для каждого элемента в корзине, можно сделать немного лучше - изначальны вычислить соотношение этих id, а потом в этой мапе уже их брать

Copy link
Author

Choose a reason for hiding this comment

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

"Изначально вычислить" немного не поняла. Вычислить ид ресторана где-то выше по родителям и прокинуть в basket item пропсом?

rest.menu.includes(props.product.id)
).id;

export const restaurantSelector = getById(restaurantsSelector);

export const reviewsListSelector = createSelector(
reviewsSelector,
Object.values
);

export const productAmountSelector = getById(orderSelector, 0);
export const productSelector = getById(productsSelector);
const reviewSelector = getById(reviewsSelector);
Expand Down