-
Notifications
You must be signed in to change notification settings - Fork 16
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
base: master
Are you sure you want to change the base?
HT-6 Kostenevich #83
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
); | ||
}; | ||
|
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот это пересчитывается для каждого элемента корзины при каждом рендере. Можно сделать немного оптимальнее, разделив на 2 селектора. Покажу на своей домашке. |
||
restaurant.menu.find((value) => value === id) | ||
); | ||
return restaurant.id; | ||
} | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
вот так
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
понял, спасибо