2019-06-11 14:01:40 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { FlatList } from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
2020-11-30 21:47:05 +00:00
|
|
|
import RNRestart from 'react-native-restart';
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2020-11-30 21:47:05 +00:00
|
|
|
import I18n, { LANGUAGES, isRTL } from '../../i18n';
|
2019-06-11 14:01:40 +00:00
|
|
|
import { showErrorAlert } from '../../utils/info';
|
2020-07-30 19:51:13 +00:00
|
|
|
import log, { logEvent, events } from '../../utils/log';
|
2019-06-11 14:01:40 +00:00
|
|
|
import { setUser as setUserAction } from '../../actions/login';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { appStart as appStartAction, ROOT_LOADING, ROOT_INSIDE } from '../../actions/app';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2020-02-11 20:28:40 +00:00
|
|
|
import database from '../../lib/database';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class LanguageView extends React.Component {
|
2020-07-31 18:30:36 +00:00
|
|
|
static navigationOptions = () => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Change_Language')
|
2020-07-31 18:30:36 +00:00
|
|
|
})
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
2020-02-11 20:28:40 +00:00
|
|
|
user: PropTypes.object,
|
2019-12-04 16:39:53 +00:00
|
|
|
setUser: PropTypes.func,
|
2020-02-11 20:28:40 +00:00
|
|
|
appStart: PropTypes.func,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string
|
2019-06-11 14:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2020-02-28 20:11:08 +00:00
|
|
|
language: props.user ? props.user.language : 'en'
|
2019-06-11 14:01:40 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2020-02-28 20:11:08 +00:00
|
|
|
const { language } = this.state;
|
2020-02-11 20:28:40 +00:00
|
|
|
const { user, theme } = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-11 14:01:40 +00:00
|
|
|
if (nextState.language !== language) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-11 20:28:40 +00:00
|
|
|
if (nextProps.user.language !== user.language) {
|
2019-06-11 14:01:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
formIsChanged = (language) => {
|
2020-02-11 20:28:40 +00:00
|
|
|
const { user } = this.props;
|
|
|
|
return (user.language !== language);
|
2019-06-11 14:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
submit = async(language) => {
|
|
|
|
if (!this.formIsChanged(language)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-30 21:47:05 +00:00
|
|
|
const { appStart, user } = this.props;
|
|
|
|
|
|
|
|
const shouldRestart = isRTL(language) || isRTL(user.language);
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
await appStart({ root: ROOT_LOADING, text: I18n.t('Change_language_loading') });
|
2020-02-28 20:11:08 +00:00
|
|
|
|
|
|
|
// shows loading for at least 300ms
|
|
|
|
await Promise.all([this.changeLanguage(language), new Promise(resolve => setTimeout(resolve, 300))]);
|
|
|
|
|
2020-11-30 21:47:05 +00:00
|
|
|
if (shouldRestart) {
|
|
|
|
await RNRestart.Restart();
|
|
|
|
} else {
|
|
|
|
await appStart({ root: ROOT_INSIDE });
|
|
|
|
}
|
2020-02-28 20:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
changeLanguage = async(language) => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE);
|
2020-02-28 20:11:08 +00:00
|
|
|
const { user, setUser } = this.props;
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
const params = {};
|
|
|
|
|
|
|
|
// language
|
2020-02-11 20:28:40 +00:00
|
|
|
if (user.language !== language) {
|
2019-06-11 14:01:40 +00:00
|
|
|
params.language = language;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await RocketChat.saveUserPreferences(params);
|
|
|
|
setUser({ language: params.language });
|
|
|
|
|
2020-02-11 20:28:40 +00:00
|
|
|
const serversDB = database.servers;
|
2021-03-15 20:16:34 +00:00
|
|
|
const usersCollection = serversDB.get('users');
|
2020-02-11 20:28:40 +00:00
|
|
|
await serversDB.action(async() => {
|
|
|
|
try {
|
|
|
|
const userRecord = await usersCollection.find(user.id);
|
|
|
|
await userRecord.update((record) => {
|
|
|
|
record.language = params.language;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE_F);
|
2020-02-11 20:28:40 +00:00
|
|
|
}
|
|
|
|
});
|
2019-06-11 14:01:40 +00:00
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE_F);
|
2020-02-11 20:28:40 +00:00
|
|
|
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t('saving_preferences') }));
|
|
|
|
log(e);
|
2019-06-11 14:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
2020-10-30 13:59:44 +00:00
|
|
|
return <List.Icon name='check' color={themes[theme].tintColor} />;
|
2019-12-04 16:39:53 +00:00
|
|
|
}
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { value, label } = item;
|
|
|
|
const { language } = this.state;
|
|
|
|
const isSelected = language === value;
|
|
|
|
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
2019-06-11 14:01:40 +00:00
|
|
|
title={label}
|
|
|
|
onPress={() => this.submit(value)}
|
|
|
|
testID={`language-view-${ value }`}
|
|
|
|
right={isSelected ? this.renderIcon : null}
|
2020-10-30 13:59:44 +00:00
|
|
|
translateTitle={false}
|
2019-06-11 14:01:40 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='language-view'>
|
|
|
|
<StatusBar />
|
2019-06-11 14:01:40 +00:00
|
|
|
<FlatList
|
|
|
|
data={LANGUAGES}
|
|
|
|
keyExtractor={item => item.value}
|
2020-10-30 13:59:44 +00:00
|
|
|
ListHeaderComponent={List.Separator}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
contentContainerStyle={List.styles.contentContainerStyleFlatList}
|
2019-06-11 14:01:40 +00:00
|
|
|
renderItem={this.renderItem}
|
2020-10-30 13:59:44 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2019-06-11 14:01:40 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 20:28:40 +00:00
|
|
|
user: getUserSelector(state)
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
2020-02-11 20:28:40 +00:00
|
|
|
setUser: params => dispatch(setUserAction(params)),
|
2020-06-15 14:00:46 +00:00
|
|
|
appStart: params => dispatch(appStartAction(params))
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(LanguageView));
|