Skip to content

Commit

Permalink
Merge pull request #2030 from botpress/ya-cms-lang
Browse files Browse the repository at this point in the history
fix(cms): remembering last language on refresh
  • Loading branch information
allardy committed Jun 29, 2019
2 parents 0790eaa + 4dbbfd6 commit 7012e16
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { keyMap } from '~/keyboardShortcuts'
import style from './StatusBar.styl'
import Flag from 'react-world-flags'

const STORAGE_KEY = `bp::${window.BOT_ID}::cmsLanguage`
const langFlagsMap = {
fr: 'fr',
en: 'gb',
Expand All @@ -22,11 +23,30 @@ const langFlagsMap = {
class LangSwitcher extends React.Component {
elems = {}

componentDidUpdate() {
componentDidMount() {
this.restoreLastLanguage()
}

componentDidUpdate(prevProps) {
let idx = this.props.languages.findIndex(l => l == this.props.contentLang)
if (idx != -1 && !_.isEmpty(this.elems)) {
this.elems[idx].focus()
}

if (prevProps.languages !== this.props.languages) {
this.restoreLastLanguage()
}
}

restoreLastLanguage() {
const lastLang = localStorage.getItem(STORAGE_KEY)
if (!this.props.languages || !this.props.languages.length || !lastLang) {
return
}

if (this.props.languages.includes(lastLang)) {
this.props.changeContentLanguage(lastLang)
}
}

componentWillUnmount() {
Expand All @@ -39,9 +59,11 @@ class LangSwitcher extends React.Component {
}
}

switchLang = l => {
this.props.changeContentLanguage(l)
switchLang = lang => {
this.props.changeContentLanguage(lang)
this.props.toggleLangSwitcher()

localStorage.setItem(STORAGE_KEY, lang)
}

//react-bootstrap warning otherwise
Expand Down
2 changes: 1 addition & 1 deletion src/bp/ui-studio/src/web/components/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Layout extends React.Component<ILayoutProps> {
}

toggleLangSwitcher = e => {
e.preventDefault()
e && e.preventDefault()
if (!isInputFocused()) {
const langSwitcherOpen = !this.state.langSwitcherOpen
this.setState({ langSwitcherOpen }, () => {
Expand Down

0 comments on commit 7012e16

Please sign in to comment.