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-02-19 19:43:47 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { showErrorAlert } from '../../utils/info';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import { setUser as setUserAction } from '../../actions/login';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import ListItem from '../../containers/ListItem';
|
|
|
|
import Separator from '../../containers/Separator';
|
2019-12-11 23:01:12 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { themedHeader } from '../../utils/navigation';
|
2020-02-19 19:43:47 +00:00
|
|
|
import { appStart as appStartAction } from '../../actions';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import database from '../../lib/database';
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
const LANGUAGES = [
|
|
|
|
{
|
|
|
|
label: '简体中文',
|
|
|
|
value: 'zh-CN'
|
|
|
|
}, {
|
|
|
|
label: 'Deutsch',
|
|
|
|
value: 'de'
|
|
|
|
}, {
|
|
|
|
label: 'English',
|
|
|
|
value: 'en'
|
2020-02-19 19:43:47 +00:00
|
|
|
}, {
|
|
|
|
label: 'Español (ES)',
|
|
|
|
value: 'es-ES'
|
2019-06-11 14:01:40 +00:00
|
|
|
}, {
|
|
|
|
label: 'Français',
|
|
|
|
value: 'fr'
|
|
|
|
}, {
|
|
|
|
label: 'Português (BR)',
|
|
|
|
value: 'pt-BR'
|
|
|
|
}, {
|
|
|
|
label: 'Português (PT)',
|
|
|
|
value: 'pt-PT'
|
|
|
|
}, {
|
|
|
|
label: 'Russian',
|
|
|
|
value: 'ru'
|
2020-02-19 19:43:47 +00:00
|
|
|
}, {
|
|
|
|
label: 'Nederlands',
|
|
|
|
value: 'nl'
|
|
|
|
}, {
|
|
|
|
label: 'Italiano',
|
|
|
|
value: 'it'
|
2020-03-18 21:26:17 +00:00
|
|
|
}, {
|
|
|
|
label: '日本語',
|
|
|
|
value: 'ja'
|
2019-06-11 14:01:40 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
class LanguageView extends React.Component {
|
2019-12-11 23:01:12 +00:00
|
|
|
static navigationOptions = ({ screenProps }) => ({
|
|
|
|
title: I18n.t('Change_Language'),
|
|
|
|
...themedHeader(screenProps.theme)
|
2019-06-11 14:01:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
static propTypes = {
|
2020-02-19 19:43:47 +00:00
|
|
|
user: PropTypes.object,
|
2019-12-11 23:01:12 +00:00
|
|
|
setUser: PropTypes.func,
|
2020-02-19 19:43:47 +00:00
|
|
|
appStart: PropTypes.func,
|
2019-12-11 23:01:12 +00:00
|
|
|
theme: PropTypes.string
|
2019-06-11 14:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2020-03-18 21:26:17 +00:00
|
|
|
language: props.user ? props.user.language : 'en'
|
2019-06-11 14:01:40 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2020-03-18 21:26:17 +00:00
|
|
|
const { language } = this.state;
|
2020-02-19 19:43:47 +00:00
|
|
|
const { user, theme } = this.props;
|
2019-12-11 23:01:12 +00:00
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-11 14:01:40 +00:00
|
|
|
if (nextState.language !== language) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-19 19:43:47 +00:00
|
|
|
if (nextProps.user.language !== user.language) {
|
2019-06-11 14:01:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
formIsChanged = (language) => {
|
2020-02-19 19:43:47 +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-03-18 21:26:17 +00:00
|
|
|
const { appStart } = this.props;
|
|
|
|
|
|
|
|
await appStart('loading', I18n.t('Change_language_loading'));
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2020-03-18 21:26:17 +00:00
|
|
|
// shows loading for at least 300ms
|
|
|
|
await Promise.all([this.changeLanguage(language), new Promise(resolve => setTimeout(resolve, 300))]);
|
|
|
|
|
|
|
|
await appStart('inside');
|
|
|
|
}
|
|
|
|
|
|
|
|
changeLanguage = async(language) => {
|
|
|
|
const { user, setUser } = this.props;
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
const params = {};
|
|
|
|
|
|
|
|
// language
|
2020-02-19 19:43:47 +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-19 19:43:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
2019-06-11 14:01:40 +00:00
|
|
|
} catch (e) {
|
2020-02-19 19:43:47 +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-11 23:01:12 +00:00
|
|
|
renderSeparator = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <Separator theme={theme} />;
|
|
|
|
}
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <CustomIcon name='check' size={20} style={{ color: themes[theme].tintColor }} />;
|
|
|
|
}
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { value, label } = item;
|
|
|
|
const { language } = this.state;
|
2019-12-11 23:01:12 +00:00
|
|
|
const { theme } = this.props;
|
2019-06-11 14:01:40 +00:00
|
|
|
const isSelected = language === value;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ListItem
|
|
|
|
title={label}
|
|
|
|
onPress={() => this.submit(value)}
|
|
|
|
testID={`language-view-${ value }`}
|
|
|
|
right={isSelected ? this.renderIcon : null}
|
2019-12-11 23:01:12 +00:00
|
|
|
theme={theme}
|
2019-06-11 14:01:40 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-12-11 23:01:12 +00:00
|
|
|
const { theme } = this.props;
|
2019-06-11 14:01:40 +00:00
|
|
|
return (
|
2019-12-11 23:01:12 +00:00
|
|
|
<SafeAreaView
|
|
|
|
style={[sharedStyles.container, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
|
|
|
forceInset={{ vertical: 'never' }}
|
|
|
|
testID='language-view'
|
|
|
|
>
|
|
|
|
<StatusBar theme={theme} />
|
2019-06-11 14:01:40 +00:00
|
|
|
<FlatList
|
|
|
|
data={LANGUAGES}
|
|
|
|
keyExtractor={item => item.value}
|
2019-12-11 23:01:12 +00:00
|
|
|
contentContainerStyle={[
|
|
|
|
sharedStyles.listContentContainer,
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].auxiliaryBackground,
|
|
|
|
borderColor: themes[theme].separatorColor
|
|
|
|
}
|
|
|
|
]}
|
2019-06-11 14:01:40 +00:00
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 19:24:22 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-19 19:43:47 +00:00
|
|
|
user: getUserSelector(state)
|
2019-08-20 19:24:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
2020-02-19 19:43:47 +00:00
|
|
|
setUser: params => dispatch(setUserAction(params)),
|
2020-03-18 21:26:17 +00:00
|
|
|
appStart: (...params) => dispatch(appStartAction(...params))
|
2019-08-20 19:24:22 +00:00
|
|
|
});
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(LanguageView));
|