Browse Source

Fix updateTab when we change Route with TopBar

master
Loëck Vézien 7 years ago
committed by meriadec
parent
commit
e0606e4399
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 25
      src/components/SettingsPage/index.js
  2. 17
      src/components/TopBar.js

25
src/components/SettingsPage/index.js

@ -6,7 +6,7 @@ import { connect } from 'react-redux'
import { translate } from 'react-i18next' import { translate } from 'react-i18next'
import { Switch, Route } from 'react-router' import { Switch, Route } from 'react-router'
import type { RouterHistory, Match } from 'react-router' import type { RouterHistory, Match, Location } from 'react-router'
import type { Settings, T } from 'types/common' import type { Settings, T } from 'types/common'
import type { SaveSettings } from 'actions/settings' import type { SaveSettings } from 'actions/settings'
import type { FetchCounterValues } from 'actions/counterValues' import type { FetchCounterValues } from 'actions/counterValues'
@ -35,6 +35,7 @@ type Props = {
fetchCounterValues: FetchCounterValues, fetchCounterValues: FetchCounterValues,
history: RouterHistory, history: RouterHistory,
i18n: Object, i18n: Object,
location: Location,
match: Match, match: Match,
saveSettings: SaveSettings, saveSettings: SaveSettings,
settings: Settings, settings: Settings,
@ -73,16 +74,32 @@ class SettingsPage extends PureComponent<Props, State> {
] ]
this.state = { this.state = {
tab: this._items[0], tab: this.getCurrentTab({ url: props.match.url, pathname: props.location.pathname }),
} }
} }
componentWillReceiveProps(nextProps) {
if (nextProps.location !== this.props.location) {
this.setState({
tab: this.getCurrentTab({
url: nextProps.match.url,
pathname: nextProps.location.pathname,
}),
})
}
}
getCurrentTab = ({ url, pathname }) =>
this._items.find(i => `${url}/${i.key}` === pathname) || this._items[0]
_items = [] _items = []
handleChangeTab = (item: any) => { handleChangeTab = (item: any) => {
const { match, history } = this.props const { match, history, location } = this.props
const url = `${match.url}/${item.key}`
if (location.pathname !== url) {
history.push(`${match.url}/${item.key}`) history.push(`${match.url}/${item.key}`)
this.setState({ tab: item }) }
} }
handleSaveSettings = newSettings => { handleSaveSettings = newSettings => {

17
src/components/TopBar.js

@ -8,7 +8,7 @@ import styled from 'styled-components'
import { withRouter } from 'react-router' import { withRouter } from 'react-router'
import { ipcRenderer } from 'electron' import { ipcRenderer } from 'electron'
import type { RouterHistory } from 'react-router' import type { Location, RouterHistory } from 'react-router'
import type { T } from 'types/common' import type { T } from 'types/common'
import { rgba } from 'styles/helpers' import { rgba } from 'styles/helpers'
@ -87,11 +87,12 @@ const mapDispatchToProps = {
} }
type Props = { type Props = {
history: RouterHistory,
t: T,
hasAccounts: boolean, hasAccounts: boolean,
hasPassword: boolean, hasPassword: boolean,
history: RouterHistory,
location: Location,
lock: Function, lock: Function,
t: T,
username: string, username: string,
} }
@ -150,7 +151,7 @@ class TopBar extends PureComponent<Props, State> {
handleLock = () => this.props.lock() handleLock = () => this.props.lock()
render() { render() {
const { hasPassword, history, hasAccounts, username, t } = this.props const { location, hasPassword, history, hasAccounts, username, t } = this.props
const { sync } = this.state const { sync } = this.state
return ( return (
@ -176,7 +177,13 @@ class TopBar extends PureComponent<Props, State> {
key: 'profile', key: 'profile',
label: t('common:editProfile'), label: t('common:editProfile'),
icon: <IconUser size={16} />, icon: <IconUser size={16} />,
onClick: () => history.push('/settings/profile'), onClick: () => {
const url = '/settings/profile'
if (location.pathname !== url) {
history.push(url)
}
},
}, },
...(hasPassword ...(hasPassword
? [ ? [

Loading…
Cancel
Save