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: add routing #80

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
15 changes: 13 additions & 2 deletions src/components/basket/basket-item/basket-item.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { createStructuredSelector } from 'reselect';
import cn from 'classnames';
import { productRestaurantSelector } from '../../../redux/selectors';
import { increment, decrement, remove } from '../../../redux/actions';
import Button from '../../button';
import styles from './basket-item.module.css';

function BasketItem({
restaurant,
product,
amount,
subtotal,
Expand All @@ -16,7 +20,9 @@ function BasketItem({
return (
<div className={styles.basketItem}>
<div className={styles.name}>
<span>{product.name}</span>
<Link to={`/restaurants/${restaurant.id}`}>
<span>{product.name}</span>
</Link>
</div>
<div className={styles.info}>
<div className={styles.counter}>
Expand Down Expand Up @@ -46,4 +52,9 @@ function BasketItem({
);
}

export default connect(null, { increment, decrement, remove })(BasketItem);
export default connect(
createStructuredSelector({
restaurant: productRestaurantSelector
}),
{ increment, decrement, remove })
(BasketItem);
1 change: 1 addition & 0 deletions src/components/basket/basket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Basket({ title = 'Basket', total, orderProducts }) {
<h4 className={styles.title}>{title}</h4>
{orderProducts.map(({ product, amount, subtotal }) => (
<BasketItem
id={product.id}
product={product}
amount={amount}
key={product.id}
Expand Down
37 changes: 27 additions & 10 deletions src/components/restaurant/restaurant.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
import React from 'react';
import { connect } from 'react-redux';
import { Route, NavLink } from 'react-router-dom';
import PropTypes from 'prop-types';
import { createStructuredSelector } from 'reselect';

import Menu from '../menu';
import Reviews from '../reviews';
import Banner from '../banner';
import Rate from '../rate';
import Tabs from '../tabs';
import { averageRatingSelector } from '../../redux/selectors';

const Restaurant = ({ id, name, menu, reviews, averageRating }) => {
const tabs = [
{ title: 'Menu', content: <Menu menu={menu} restaurantId={id} /> },
{
title: 'Reviews',
content: <Reviews reviews={reviews} restaurantId={id} />,
},
];
import styles from './restaurant.module.css';

const Restaurant = ({ id, name, menu, reviews, averageRating }) => {
return (
<div>
<Banner heading={name}>
{!!averageRating && <Rate value={averageRating} />}
</Banner>
<Tabs tabs={tabs} />

<div className={styles.tabs}>
<NavLink
exact
to={`/restaurants/${id}`}
className={styles.tab}
activeClassName={styles.active}>
Menu
</NavLink>
<NavLink
to={`/restaurants/${id}/reviews`}
className={styles.tab}
activeClassName={styles.active}>
Reviews
</NavLink>
</div>

<Route exact path="/restaurants/:id">
<Menu menu={menu} restaurantId={id} />
</Route>

<Route path="/restaurants/:id/reviews">
<Reviews reviews={reviews} restaurantId={id} />
</Route>
</div>
);
};
Expand Down
20 changes: 20 additions & 0 deletions src/components/restaurant/restaurant.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.tabs {
height: auto;
text-align: center;
padding: 12px;
background-color: var(--grey);
}

.tabs span {
cursor: pointer;
}

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

.tab.active {
border-bottom: 1px solid var(--black);
}
11 changes: 9 additions & 2 deletions src/redux/reducer/products.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import produce from 'immer';
import { arrToMap } from '../utils';
import { LOAD_PRODUCTS, REQUEST, SUCCESS, FAILURE } from '../constants';

const initialState = {
Expand All @@ -22,7 +21,15 @@ const reducer = (state = initialState, action) =>
draft.loading[restaurantId] = false;
draft.loaded[restaurantId] = true;
draft.error = null;
draft.entities = { ...draft.entities, ...arrToMap(response) };
draft.entities = {
...draft.entities,
...response.reduce(
(products, product) => ({
...products,
[product.id]: {restaurantId, ...product }
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.

это норм проходится по всем ресторанам и их продуктам, чтобы найти ресторан продукта?

Copy link
Owner

Choose a reason for hiding this comment

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

Да, но необходимо сделать мапу, чтобы это оптимизировать, посмотри на мое решение. Если будут вопросы - пиши в общий чат, обсудим на занятии

}),
{})
};
break;
}
case LOAD_PRODUCTS + FAILURE: {
Expand Down
6 changes: 6 additions & 0 deletions src/redux/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ export const averageRatingSelector = createSelector(
);
}
);

export const productRestaurantSelector = createSelector(
restaurantsSelector,
productSelector,
(restaurants, product) => restaurants[product.restaurantId]
);