2018-06-05 01:17:02 +00:00
|
|
|
import React from 'react';
|
2018-06-13 01:33:00 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-08-31 18:13:30 +00:00
|
|
|
import { View, ScrollView, SafeAreaView, Dimensions } from 'react-native';
|
2018-06-13 01:33:00 +00:00
|
|
|
import RNPickerSelect from 'react-native-picker-select';
|
|
|
|
import { connect } from 'react-redux';
|
2018-06-05 01:17:02 +00:00
|
|
|
|
|
|
|
import LoggedView from '../View';
|
2018-06-13 01:33:00 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import KeyboardView from '../../presentation/KeyboardView';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import RCTextInput from '../../containers/TextInput';
|
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import Button from '../../containers/Button';
|
|
|
|
import Loading from '../../containers/Loading';
|
|
|
|
import { showErrorAlert, showToast } from '../../utils/info';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import { setUser } from '../../actions/login';
|
2018-06-05 01:17:02 +00:00
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
@connect(state => ({
|
2018-07-25 13:14:12 +00:00
|
|
|
userLanguage: state.login.user && state.login.user.language
|
2018-06-13 01:33:00 +00:00
|
|
|
}), dispatch => ({
|
|
|
|
setUser: params => dispatch(setUser(params))
|
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
2018-06-05 01:17:02 +00:00
|
|
|
export default class SettingsView extends LoggedView {
|
2018-06-13 01:33:00 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
2018-07-25 13:14:12 +00:00
|
|
|
userLanguage: PropTypes.string,
|
2018-06-13 01:33:00 +00:00
|
|
|
setUser: PropTypes.func
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-06-13 01:33:00 +00:00
|
|
|
|
2018-06-05 01:17:02 +00:00
|
|
|
constructor(props) {
|
|
|
|
super('SettingsView', props);
|
2018-06-13 01:33:00 +00:00
|
|
|
this.state = {
|
|
|
|
placeholder: {},
|
2018-07-25 13:14:12 +00:00
|
|
|
language: props.userLanguage ? props.userLanguage : 'en',
|
2018-06-13 01:33:00 +00:00
|
|
|
languages: [{
|
|
|
|
label: 'English',
|
|
|
|
value: 'en'
|
2018-07-25 13:14:12 +00:00
|
|
|
}, {
|
|
|
|
label: 'Russian',
|
|
|
|
value: 'ru'
|
2018-06-13 01:33:00 +00:00
|
|
|
}],
|
|
|
|
saving: false
|
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
this.props.navigator.setButtons({
|
|
|
|
leftButtons: [{
|
2018-08-31 18:13:30 +00:00
|
|
|
id: 'settings',
|
2018-08-31 16:46:33 +00:00
|
|
|
icon: { uri: 'settings', scale: Dimensions.get('window').scale }
|
2018-07-10 13:40:32 +00:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.props.navigator.setDrawerEnabled({
|
|
|
|
side: 'left',
|
|
|
|
enabled: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onNavigatorEvent(event) {
|
|
|
|
if (event.type === 'NavBarButtonPress') {
|
2018-08-31 18:13:30 +00:00
|
|
|
if (event.id === 'settings') {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.props.navigator.toggleDrawer({
|
2018-08-31 16:46:33 +00:00
|
|
|
side: 'left'
|
2018-07-10 13:40:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 01:33:00 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
getLabel = (language) => {
|
|
|
|
const { languages } = this.state;
|
|
|
|
const l = languages.find(i => i.value === language);
|
|
|
|
if (l && l.label) {
|
|
|
|
return l.label;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
formIsChanged = () => {
|
|
|
|
const { language } = this.state;
|
2018-07-25 13:14:12 +00:00
|
|
|
return !(this.props.userLanguage === language);
|
2018-06-13 01:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
submit = async() => {
|
|
|
|
this.setState({ saving: true });
|
|
|
|
|
|
|
|
const {
|
|
|
|
language
|
|
|
|
} = this.state;
|
2018-07-25 13:14:12 +00:00
|
|
|
const { userLanguage } = this.props;
|
2018-06-13 01:33:00 +00:00
|
|
|
|
|
|
|
if (!this.formIsChanged()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = {};
|
|
|
|
|
|
|
|
// language
|
2018-07-25 13:14:12 +00:00
|
|
|
if (userLanguage !== language) {
|
2018-06-13 01:33:00 +00:00
|
|
|
params.language = language;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await RocketChat.saveUserPreferences(params);
|
|
|
|
this.props.setUser({ language: params.language });
|
|
|
|
|
|
|
|
this.setState({ saving: false });
|
|
|
|
setTimeout(() => {
|
|
|
|
showToast(I18n.t('Preferences_saved'));
|
2018-08-01 19:35:06 +00:00
|
|
|
|
|
|
|
if (params.language) {
|
|
|
|
this.props.navigator.setTitle({ title: I18n.t('Settings') });
|
|
|
|
}
|
2018-06-13 01:33:00 +00:00
|
|
|
}, 300);
|
|
|
|
} catch (e) {
|
|
|
|
this.setState({ saving: false });
|
|
|
|
setTimeout(() => {
|
|
|
|
if (e && e.error) {
|
|
|
|
return showErrorAlert(I18n.t(e.error, e.details));
|
|
|
|
}
|
|
|
|
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t('saving_preferences') }));
|
|
|
|
log('saveUserPreferences', e);
|
|
|
|
}, 300);
|
|
|
|
}
|
2018-06-05 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-06-13 01:33:00 +00:00
|
|
|
const { language, languages, placeholder } = this.state;
|
2018-06-05 01:17:02 +00:00
|
|
|
return (
|
2018-06-13 01:33:00 +00:00
|
|
|
<KeyboardView
|
|
|
|
contentContainerStyle={sharedStyles.container}
|
|
|
|
keyboardVerticalOffset={128}
|
|
|
|
>
|
|
|
|
<ScrollView
|
|
|
|
contentContainerStyle={sharedStyles.containerScrollView}
|
|
|
|
testID='settings-view-list'
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
>
|
2018-08-01 19:35:06 +00:00
|
|
|
<SafeAreaView style={sharedStyles.container} testID='settings-view'>
|
2018-06-13 01:33:00 +00:00
|
|
|
<RNPickerSelect
|
|
|
|
items={languages}
|
|
|
|
onValueChange={(value) => {
|
|
|
|
this.setState({ language: value });
|
|
|
|
}}
|
|
|
|
value={language}
|
|
|
|
placeholder={placeholder}
|
|
|
|
>
|
|
|
|
<RCTextInput
|
|
|
|
inputRef={(e) => { this.name = e; }}
|
|
|
|
label={I18n.t('Language')}
|
|
|
|
placeholder={I18n.t('Language')}
|
2018-08-01 19:35:06 +00:00
|
|
|
value={this.getLabel(language)}
|
2018-06-13 01:33:00 +00:00
|
|
|
testID='settings-view-language'
|
|
|
|
/>
|
|
|
|
</RNPickerSelect>
|
|
|
|
<View style={sharedStyles.alignItemsFlexStart}>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Save_Changes')}
|
|
|
|
type='primary'
|
|
|
|
onPress={this.submit}
|
|
|
|
disabled={!this.formIsChanged()}
|
|
|
|
testID='settings-view-button'
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<Loading visible={this.state.saving} />
|
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
|
|
|
</KeyboardView>
|
2018-06-05 01:17:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|