Skip to content

Commit

Permalink
fix: Fix language switch
Browse files Browse the repository at this point in the history
  • Loading branch information
leoliu committed May 21, 2020
1 parent dc17f4b commit 2c4f57c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 45 deletions.
3 changes: 2 additions & 1 deletion server/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ const getUserDetail = async (username, token) => {

if (resp) {
user = {
username: get(resp, 'metadata.name'),
email: get(resp, 'spec.email'),
lang: get(resp, 'spec.lang'),
username: get(resp, 'metadata.name'),
globalrole: get(
resp,
'metadata.annotations["iam.kubesphere.io/globalrole"]'
Expand Down
16 changes: 1 addition & 15 deletions src/components/Layout/LoginInfo/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { inject, observer } from 'mobx-react'
import classnames from 'classnames'
import { omit } from 'lodash'
import { Dropdown, Menu, Icon } from '@pitrix/lego-ui'

import AboutModal from 'components/Modals/About'
Expand Down Expand Up @@ -72,19 +71,7 @@ export default class LoginInfo extends Component {
}

handleUserEdit = data => {
this.store.update(
{
...omit(globals.user, [
'_update_time_',
'workspace_rules',
'rules',
'workspaces',
'cluster_rules',
]),
...data,
},
globals.user
)
this.store.update({ name: globals.user.username }, data)
}

renderDropDown() {
Expand Down Expand Up @@ -112,7 +99,6 @@ export default class LoginInfo extends Component {
/>
<UserEditModal
module="users"
detail={globals.user}
visible={this.state.showUserEdit}
onOk={this.handleUserEdit}
onCancel={this.hideUserEditModal}
Expand Down
35 changes: 9 additions & 26 deletions src/components/Modals/UserSetting/BaseInfo/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,10 @@ export default class BaseInfo extends React.Component {
registerUpdate: PropTypes.func,
}

constructor(props) {
super(props)

this.state = {
formData: this.getInitialData(),
}
}

get name() {
return 'basicInfo'
}

getInitialData() {
return {
avatar_url: globals.user.avatar_url,
username: globals.user.username,
email: globals.user.email,
lang: cookie('lang') || getBrowserLang(),
}
}

resetData = () => {
this.setState({
formData: this.getInitialData(),
Expand All @@ -62,26 +45,26 @@ export default class BaseInfo extends React.Component {
}

render() {
const { formRef } = this.props
const { formRef, formData } = this.props
return (
<div className={styles.wrapper}>
<div className="h4">{t('Basic Info')}</div>
<Form
data={this.state.formData}
ref={formRef}
onChange={this.handleFormChange}
>
<Form data={formData} ref={formRef} onChange={this.handleFormChange}>
<Columns>
<Column>
<Form.Item label={t('User Name')}>
<Input name="username" placeholder="username" disabled />
<Input name="metadata.name" placeholder="username" disabled />
</Form.Item>
<Form.Item label={t('Email')} desc={t('USER_SETTING_EMAIL_DESC')}>
<Input name="email" placeholder="User@example.com" />
<Input name="spec.email" placeholder="User@example.com" />
</Form.Item>
{globals.config.supportLangs && (
<Form.Item label={t('Language')}>
<Select name="lang" options={globals.config.supportLangs} />
<Select
name="spec.lang"
options={globals.config.supportLangs}
defaultValue={cookie('lang') || getBrowserLang()}
/>
</Form.Item>
)}
</Column>
Expand Down
20 changes: 18 additions & 2 deletions src/components/Modals/UserSetting/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with KubeSphere Console. If not, see <https://www.gnu.org/licenses/>.
*/

import { get, has, isEmpty } from 'lodash'
import { get, has, cloneDeep, isEmpty, set } from 'lodash'
import React from 'react'
import classnames from 'classnames'
import PropTypes from 'prop-types'
Expand All @@ -25,6 +25,8 @@ import { Icon, Tooltip } from '@pitrix/lego-ui'
import { Modal, Button } from 'components/Base'
import Confirm from 'components/Forms/Base/Confirm'

import UserStore from 'stores/user'

import TABS from './tabs'

import styles from './index.scss'
Expand Down Expand Up @@ -62,6 +64,8 @@ export default class UserSettingModal extends React.Component {
activeTab: get(this.tabs, '[0].name'),
updatedTabs: {},
}

this.store = new UserStore()
}

get updateTip() {
Expand All @@ -78,6 +82,17 @@ export default class UserSettingModal extends React.Component {
return TABS[module] || []
}

componentDidMount() {
this.store.fetchDetail({ name: globals.user.username }).then(() => {
this.setState({
formData: Object.assign(
{ apiVersion: 'iam.kubesphere.io/v1alpha2', kind: 'User' },
cloneDeep(this.store.detail._originData)
),
})
})
}

registerUpdate = (name, params) => {
const { updatedTabs } = this.state

Expand All @@ -102,6 +117,7 @@ export default class UserSettingModal extends React.Component {
if (form) {
form.validate(() => {
const data = form.getData()
set(data, 'metadata.resourceVersion', this.store.detail.resourceVersion)
onOk(data)
this.cancelUpdate(name)
})
Expand Down Expand Up @@ -195,7 +211,7 @@ export default class UserSettingModal extends React.Component {
module={module}
ref={this[refName]}
formRef={this[formRefName]}
formData={this.formData}
formData={this.state.formData}
/>
</div>
)
Expand Down
4 changes: 3 additions & 1 deletion src/stores/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with KubeSphere Console. If not, see <https://www.gnu.org/licenses/>.
*/

import { get } from 'lodash'
import { observable, action } from 'mobx'
import { Notify } from 'components/Base'
import cookie from 'utils/cookie'
Expand Down Expand Up @@ -87,7 +88,8 @@ export default class UsersStore extends Base {
return await request.post('logout')
}

if (data.lang && data.lang !== cookie('lang')) {
const lang = get(data, 'spec.lang')
if (lang && data.lang !== cookie('lang')) {
cookie('lang', data.lang, { path: '/' })
window.location.reload()
}
Expand Down

0 comments on commit 2c4f57c

Please sign in to comment.