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

homework-2021-12-17: add routes #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions src/components/basket/basket-item/basket-item.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { connect } from 'react-redux';
import { NavLink } from 'react-router-dom';
import cn from 'classnames';
import { increment, decrement, remove } from '../../../redux/actions';
import Button from '../../button';
import styles from './basket-item.module.css';
import { restaurantProductSelector } from '../../../redux/selectors';

function BasketItem({
product,
Expand All @@ -11,11 +13,14 @@ function BasketItem({
increment,
decrement,
remove,
restaurant,
}) {
return (
<div className={styles.basketItem}>
<div className={styles.name}>
<span>{product.name}</span>
<NavLink key={product.id} to={`/restaurants/${restaurant.id}`}>
{product.name}
</NavLink>
</div>
<div className={styles.info}>
<div className={styles.counter}>
Expand All @@ -30,10 +35,14 @@ function BasketItem({
);
}

const mapStateToProps = (state, props) => ({
restaurant: restaurantProductSelector(state, props),
});

const mapDispatchToProps = (dispatch, ownProps) => ({
increment: () => dispatch(increment(ownProps.product.id)),
decrement: () => dispatch(decrement(ownProps.product.id)),
remove: () => dispatch(remove(ownProps.product.id)),
});

export default connect(null, mapDispatchToProps)(BasketItem);
export default connect(mapStateToProps, mapDispatchToProps)(BasketItem);
38 changes: 28 additions & 10 deletions src/components/restaurant/restaurant.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
import { useState } from 'react';
import { connect } from 'react-redux';
import { NavLink, Switch, Route, Redirect } from 'react-router-dom';
import PropTypes from 'prop-types';
import Menu from '../menu';
import Reviews from '../reviews';
import Banner from '../banner';
import Rate from '../rate';
import Tabs from '../tabs';
import {
averageRatingSelector,
restaurantSelector,
} from '../../redux/selectors';

import styles from './restaurant.module.css';

const Restaurant = ({ restaurant, averageRating }) => {
const { id, name, menu, reviews } = restaurant;

const [activeTab, setActiveTab] = useState('menu');

const tabs = [
{ id: 'menu', label: 'Menu' },
{ id: 'reviews', label: 'Reviews' },
{ idTab: 'menu', label: 'Menu' },
{ idTab: 'reviews', label: 'Reviews' },
];

return (
<div>
<Banner heading={name}>
<Rate value={averageRating} />
</Banner>
<Tabs tabs={tabs} activeId={activeTab} onChange={setActiveTab} />
{activeTab === 'menu' && <Menu menu={menu} key={id} restId={id} />}
{activeTab === 'reviews' && <Reviews reviews={reviews} restId={id} />}
<div className={styles.tabs}>
{tabs.map(({ idTab, label }) => (
<NavLink
key={idTab}
to={`/restaurants/${id}/${label.toLowerCase()}`}
className={styles.tab}
activeClassName={styles.active}
>
{label}
</NavLink>
))}
</div>
<Switch>
<Route
path="/restaurants/:restId/menu"
component={() => <Menu menu={menu} key={id} restId={id} />}
/>
<Route
path="/restaurants/:restId/reviews"
component={() => <Reviews reviews={reviews} restId={id} />}
/>
<Redirect to={`/restaurants/${id}/${tabs[0].label.toLowerCase()}`} />
</Switch>
</div>
);
};
Expand Down
19 changes: 19 additions & 0 deletions src/components/restaurant/restaurant.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.tabs {
height: auto;
text-align: center;
padding: 12px;
background-color: var(--grey);
}

.tabs span {
cursor: pointer;
}

.tab {
padding: 4px 12px;
color: var(--black);
}

.tab.active {
border-bottom: 1px solid var(--black);
}
8 changes: 8 additions & 0 deletions src/redux/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export const orderProductsSelector = createSelector(
}))
);

export const restaurantProductSelector = (state, { product }) => {
Copy link
Owner

Choose a reason for hiding this comment

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

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

const restaurants = Object.values(restaurantsSelector(state)).filter(
(restaurant, i) => restaurant.menu.includes(product.id)
);
const id = restaurants[0].id;
return restaurantsSelector(state)[id];
};

export const totalSelector = createSelector(
[orderProductsSelector],
(orderProducts) =>
Expand Down