-
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
Lesson6 #84
base: master
Are you sure you want to change the base?
Lesson6 #84
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,13 +1,19 @@ | ||
import { connect } from 'react-redux'; | ||
import { Link } from 'react-router-dom'; | ||
import { ReactComponent as Logo } from '../../icons/logo.svg'; | ||
import styles from './header.module.css'; | ||
import { changeActiveRestaurantTab } from '../../redux/actions'; | ||
|
||
const Header = () => ( | ||
const Header = ({ changeActiveRestaurantTab }) => ( | ||
<header className={styles.header}> | ||
<Link to="/restaurants"> | ||
<Link to="/restaurants" onClick={() => changeActiveRestaurantTab('menu')}> | ||
<Logo /> | ||
</Link> | ||
</header> | ||
); | ||
|
||
export default Header; | ||
const mapDispatchToProps = { | ||
changeActiveRestaurantTab, | ||
}; | ||
|
||
export default connect(null, mapDispatchToProps)(Header); |
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 |
---|---|---|
|
@@ -11,13 +11,15 @@ import { | |
REQUEST, | ||
SUCCESS, | ||
FAILURE, | ||
CHANGE_RESTAURANT_TAB, | ||
} from './constants'; | ||
|
||
import { | ||
usersLoadingSelector, | ||
usersLoadedSelector, | ||
reviewsLoadingSelector, | ||
reviewsLoadedSelector, | ||
activeTabSelector, | ||
} from './selectors'; | ||
|
||
export const increment = (id) => ({ type: INCREMENT, id }); | ||
|
@@ -75,3 +77,15 @@ export const loadUsers = () => async (dispatch, getState) => { | |
|
||
dispatch(_loadUsers()); | ||
}; | ||
|
||
export const changeActiveRestaurantTab = (tab) => { | ||
return { | ||
type: CHANGE_RESTAURANT_TAB, | ||
restaurantActiveTab: tab, | ||
}; | ||
}; | ||
|
||
export const getActiveRestaurantTab = () => (_dispatch, getState) => { | ||
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. это не экшен, а селетор. 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. Тут собственно так сделано, чтобы получить значение однократно при перерендере компоненты с новым рестораном, а не при каждой смене таба. Не очень понимаю, как можно для этого использовать селектор, можно подробнее? |
||
const state = getState(); | ||
return activeTabSelector(state); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,8 @@ export default (state = initialState, action) => | |
draft.loading[restId] = false; | ||
draft.loaded[restId] = true; | ||
draft.error = null; | ||
Object.assign(draft.entities, arrToMap(data)); | ||
const dataWithRestId = data.map((obj) => ({ ...obj, restId })); | ||
Object.assign(draft.entities, arrToMap(dataWithRestId)); | ||
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. Из данных, которые у нас лежат в редьюсере ресторанов мы можем однозначно сказать какой продукт из какого ресторана. Теперь эта информация у нас есть в двух местах, тут и в ресторанах. Так делать не стоит. 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. Вот про это очень интересно, мне казалось, что обратные ссылки это нормально :)) |
||
break; | ||
} | ||
case LOAD_PRODUCTS + FAILURE: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import produce from 'immer'; | ||
import { CHANGE_RESTAURANT_TAB } from '../constants'; | ||
|
||
export default produce((draft = { restaurantActiveTab: 'menu' }, action) => { | ||
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. у нас эта вкладка есть в роутинге, не совсем понимаю зачем ее сохранять в редакс 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. Хотелось сохранить логику, чтобы при переключении ресторана не менялась выбраная подвкладка и при этом не хотелось показывать эту подвкладку родительским компонентам. |
||
const { type, restaurantActiveTab } = action; | ||
|
||
switch (type) { | ||
case CHANGE_RESTAURANT_TAB: | ||
draft.restaurantActiveTab = restaurantActiveTab; | ||
break; | ||
default: | ||
return draft; | ||
} | ||
}); |
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.
Вот собственно ради этого все и затевалось :)
если по умолчанию редиректить на меню, у нас относительно текущей версии приложения потеряется сохранение выбранной подвкладки ресторана. А менять эту логику не хотелось.