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