[IMPROVEMENT] Reset app when language is changed (#1702)

This commit is contained in:
Djorkaeff Alexandre 2020-02-11 17:28:40 -03:00 committed by GitHub
parent 42a53b4c9d
commit 1d79746eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 22 deletions

View File

@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import { FlatList } from 'react-native';
import { connect } from 'react-redux';
import { SafeAreaView, NavigationActions } from 'react-navigation';
import { SafeAreaView } from 'react-navigation';
import RocketChat from '../../lib/rocketchat';
import I18n from '../../i18n';
@ -18,7 +18,9 @@ import Separator from '../../containers/Separator';
import { themes } from '../../constants/colors';
import { withTheme } from '../../theme';
import { themedHeader } from '../../utils/navigation';
import { appStart as appStartAction } from '../../actions';
import { getUserSelector } from '../../selectors/login';
import database from '../../lib/database';
const LANGUAGES = [
{
@ -58,23 +60,23 @@ class LanguageView extends React.Component {
})
static propTypes = {
userLanguage: PropTypes.string,
navigation: PropTypes.object,
user: PropTypes.object,
setUser: PropTypes.func,
appStart: PropTypes.func,
theme: PropTypes.string
}
constructor(props) {
super(props);
this.state = {
language: props.userLanguage ? props.userLanguage : 'en',
language: props.user ? props.user.language : 'en',
saving: false
};
}
shouldComponentUpdate(nextProps, nextState) {
const { language, saving } = this.state;
const { userLanguage, theme } = this.props;
const { user, theme } = this.props;
if (nextProps.theme !== theme) {
return true;
}
@ -84,15 +86,15 @@ class LanguageView extends React.Component {
if (nextState.saving !== saving) {
return true;
}
if (nextProps.userLanguage !== userLanguage) {
if (nextProps.user.language !== user.language) {
return true;
}
return false;
}
formIsChanged = (language) => {
const { userLanguage } = this.props;
return (userLanguage !== language);
const { user } = this.props;
return (user.language !== language);
}
submit = async(language) => {
@ -102,12 +104,12 @@ class LanguageView extends React.Component {
this.setState({ saving: true });
const { userLanguage, setUser, navigation } = this.props;
const { user, setUser, appStart } = this.props;
const params = {};
// language
if (userLanguage !== language) {
if (user.language !== language) {
params.language = language;
}
@ -115,18 +117,27 @@ class LanguageView extends React.Component {
await RocketChat.saveUserPreferences(params);
setUser({ language: params.language });
this.setState({ saving: false });
setTimeout(() => {
navigation.reset([NavigationActions.navigate({ routeName: 'SettingsView' })], 0);
navigation.navigate('RoomsListView');
}, 300);
const serversDB = database.servers;
const usersCollection = serversDB.collections.get('users');
await serversDB.action(async() => {
try {
const userRecord = await usersCollection.find(user.id);
await userRecord.update((record) => {
record.language = params.language;
});
} catch (e) {
// do nothing
}
});
await appStart('loading');
await appStart('inside');
} catch (e) {
this.setState({ saving: false });
setTimeout(() => {
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t('saving_preferences') }));
log(e);
}, 300);
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t('saving_preferences') }));
log(e);
}
this.setState({ saving: false });
}
renderSeparator = () => {
@ -186,11 +197,12 @@ class LanguageView extends React.Component {
}
const mapStateToProps = state => ({
userLanguage: getUserSelector(state).language
user: getUserSelector(state)
});
const mapDispatchToProps = dispatch => ({
setUser: params => dispatch(setUserAction(params))
setUser: params => dispatch(setUserAction(params)),
appStart: params => dispatch(appStartAction(params))
});
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(LanguageView));