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-6 Kostenevich #83

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
8 changes: 6 additions & 2 deletions src/components/app/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PureComponent } from 'react';
import { Route, Switch } from 'react-router-dom';
import { Route, Switch, Redirect } from 'react-router-dom';
import Restaurants from '../restaurants';
import Header from '../header';
import Basket from '../basket';
Expand All @@ -10,7 +10,11 @@ export default class App extends PureComponent {
<div>
<Header />
<Switch>
<Route path="/" exact component={() => <h2>Home page</h2>} />
<Route
path="/"
exact
component={() => <Redirect to={`/restaurants`} />}
/>
Comment on lines +13 to +17
Copy link
Owner

Choose a reason for hiding this comment

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

вот так

Suggested change
<Route
path="/"
exact
component={() => <Redirect to={`/restaurants`} />}
/>
<Redirect exact from="/" to="/restaurants" />

Copy link
Author

Choose a reason for hiding this comment

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

понял, спасибо

<Route path="/checkout" component={Basket} />
<Route path="/restaurants" component={Restaurants} />
<Route component={() => <h2>404 - Not found :(</h2>} />
Expand Down
11 changes: 9 additions & 2 deletions src/components/basket/basket-item/basket-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import cn from 'classnames';
import { increment, decrement, remove } from '../../../redux/actions';
import Button from '../../button';
import styles from './basket-item.module.css';
import { restaurantByProductIdSelector } from '../../../redux/selectors';
import { Link } from 'react-router-dom';

function BasketItem({
product,
Expand All @@ -11,11 +13,12 @@ function BasketItem({
increment,
decrement,
remove,
restId,
}) {
return (
<div className={styles.basketItem}>
<div className={styles.name}>
<span>{product.name}</span>
<Link to={`/restaurants/${restId}/menu`}>{product.name}</Link>
</div>
<div className={styles.info}>
<div className={styles.counter}>
Expand All @@ -36,4 +39,8 @@ const mapDispatchToProps = (dispatch, ownProps) => ({
remove: () => dispatch(remove(ownProps.product.id)),
});

export default connect(null, mapDispatchToProps)(BasketItem);
const mapStateToProps = (state, ownProps) => ({
restId: restaurantByProductIdSelector(state, ownProps),
});

export default connect(mapStateToProps, mapDispatchToProps)(BasketItem);
43 changes: 34 additions & 9 deletions src/components/restaurant/restaurant.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
import { useState } from 'react';
import { connect } from 'react-redux';
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';
import { NavLink, Switch, Route, Redirect } from 'react-router-dom';

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' },
{ id: 'menu', label: 'Menu', restId: id },
{ id: 'reviews', label: 'Reviews', restId: id },
];

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(({ id, label, restId }) => (
<NavLink
key={id}
to={`/restaurants/${restId}/${id}`}
className={styles.tab}
activeClassName={styles.active}
>
{label}
</NavLink>
))}
</div>
<Switch>
<Route path="/restaurants/:restId/:id">
{({ match }) => {
console.log(match.params);

switch (match.params.id) {
Copy link
Owner

Choose a reason for hiding this comment

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

проще сделать несколько Route, покажу на своей домашке

case 'menu':
return <Menu menu={menu} key={id} restId={id} />;
case 'reviews':
return <Reviews reviews={reviews} restId={id} />;
default:
return 'no data((';
}
}}
</Route>
<Redirect to={`/restaurants/:restId/menu`} />
</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);
}
2 changes: 1 addition & 1 deletion src/components/restaurants/restaurants.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Restaurants({ restaurants, loading, loaded, loadRestaurants }) {
<Route path="/restaurants/:restId">
{({ match }) => <Restaurant id={match.params.restId} />}
</Route>
<Redirect to={`/restaurants/${restaurants[0]?.id}`} />
<Redirect to={`/restaurants/${restaurants[0]?.id}/menu`} />
</Switch>
</div>
);
Expand Down
10 changes: 10 additions & 0 deletions src/redux/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ export const averageRatingSelector = createSelector(
);
}
);

export const restaurantByProductIdSelector = createSelector(
[restaurantsSelector, (state, ownProps) => ownProps.product.id],
(restaurants, id) => {
const restaurant = Object.values(restaurants).find((restaurant) =>
Copy link
Owner

Choose a reason for hiding this comment

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

Вот это пересчитывается для каждого элемента корзины при каждом рендере. Можно сделать немного оптимальнее, разделив на 2 селектора. Покажу на своей домашке.

restaurant.menu.find((value) => value === id)
);
return restaurant.id;
}
);