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';
|
2019-05-21 12:12:15 +00:00
|
|
|
import {
|
|
|
|
View, ScrollView, Switch, Text, StyleSheet, AsyncStorage
|
|
|
|
} from 'react-native';
|
2018-06-13 01:33:00 +00:00
|
|
|
import RNPickerSelect from 'react-native-picker-select';
|
|
|
|
import { connect } from 'react-redux';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2019-05-21 12:12:15 +00:00
|
|
|
import { Answers } from 'react-native-fabric';
|
2018-06-05 01:17:02 +00:00
|
|
|
|
|
|
|
import LoggedView from '../View';
|
2019-05-21 12:12:15 +00:00
|
|
|
import RocketChat, { MARKDOWN_KEY } from '../../lib/rocketchat';
|
2018-06-13 01:33:00 +00:00
|
|
|
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';
|
2019-05-15 19:33:30 +00:00
|
|
|
import { showErrorAlert, Toast } from '../../utils/info';
|
2018-06-13 01:33:00 +00:00
|
|
|
import log from '../../utils/log';
|
2018-09-25 19:28:42 +00:00
|
|
|
import { setUser as setUserAction } from '../../actions/login';
|
2019-05-21 12:12:15 +00:00
|
|
|
import { toggleMarkdown as toggleMarkdownAction } from '../../actions/markdown';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { DrawerButton } from '../../containers/HeaderButton';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-05-21 12:12:15 +00:00
|
|
|
import { isAndroid } from '../../utils/deviceInfo';
|
|
|
|
import {
|
|
|
|
COLOR_WHITE, COLOR_SEPARATOR, COLOR_DANGER, COLOR_SUCCESS
|
|
|
|
} from '../../constants/colors';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
swithContainer: {
|
|
|
|
backgroundColor: COLOR_WHITE,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
flexDirection: 'row'
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
fontSize: 17,
|
|
|
|
flex: 1,
|
|
|
|
...sharedStyles.textMedium,
|
|
|
|
...sharedStyles.textColorNormal
|
|
|
|
},
|
|
|
|
separator: {
|
|
|
|
flex: 1,
|
|
|
|
height: 1,
|
|
|
|
backgroundColor: COLOR_SEPARATOR,
|
|
|
|
marginVertical: 10
|
|
|
|
}
|
|
|
|
});
|
2018-06-05 01:17:02 +00:00
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
@connect(state => ({
|
2019-05-21 12:12:15 +00:00
|
|
|
userLanguage: state.login.user && state.login.user.language,
|
|
|
|
useMarkdown: state.markdown.useMarkdown
|
2018-06-13 01:33:00 +00:00
|
|
|
}), dispatch => ({
|
2019-05-21 12:12:15 +00:00
|
|
|
setUser: params => dispatch(setUserAction(params)),
|
|
|
|
toggleMarkdown: params => dispatch(toggleMarkdownAction(params))
|
2018-06-13 01:33:00 +00:00
|
|
|
}))
|
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 {
|
2019-03-12 16:23:06 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
headerLeft: <DrawerButton navigation={navigation} />,
|
|
|
|
title: I18n.t('Settings')
|
|
|
|
})
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
static propTypes = {
|
2018-10-23 21:39:48 +00:00
|
|
|
componentId: PropTypes.string,
|
2018-07-25 13:14:12 +00:00
|
|
|
userLanguage: PropTypes.string,
|
2019-05-21 12:12:15 +00:00
|
|
|
useMarkdown: PropTypes.bool,
|
|
|
|
setUser: PropTypes.func,
|
|
|
|
toggleMarkdown: 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-09-21 17:24:32 +00:00
|
|
|
}, {
|
2019-03-18 19:09:40 +00:00
|
|
|
label: 'Português (BR)',
|
2018-09-21 17:24:32 +00:00
|
|
|
value: 'pt-BR'
|
2018-07-25 13:14:12 +00:00
|
|
|
}, {
|
|
|
|
label: 'Russian',
|
|
|
|
value: 'ru'
|
2018-10-24 19:10:44 +00:00
|
|
|
}, {
|
|
|
|
label: '简体中文',
|
|
|
|
value: 'zh-CN'
|
2019-02-14 16:43:56 +00:00
|
|
|
}, {
|
2019-02-14 15:40:41 +00:00
|
|
|
label: 'Français',
|
|
|
|
value: 'fr'
|
2019-02-21 17:17:03 +00:00
|
|
|
}, {
|
|
|
|
label: 'Deutsch',
|
|
|
|
value: 'de'
|
2019-03-18 19:09:40 +00:00
|
|
|
}, {
|
|
|
|
label: 'Português (PT)',
|
|
|
|
value: 'pt-PT'
|
2019-02-14 16:43:56 +00:00
|
|
|
}],
|
2018-06-13 01:33:00 +00:00
|
|
|
saving: false
|
|
|
|
};
|
2018-11-27 19:40:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { language, saving } = this.state;
|
2019-05-21 12:12:15 +00:00
|
|
|
const { userLanguage, useMarkdown } = this.props;
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.language !== language) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.saving !== saving) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-21 12:12:15 +00:00
|
|
|
if (nextProps.useMarkdown !== useMarkdown) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextProps.userLanguage !== userLanguage) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { userLanguage } = this.props;
|
2018-06-13 01:33:00 +00:00
|
|
|
const { language } = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
return !(userLanguage === language);
|
2018-06-13 01:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
submit = async() => {
|
|
|
|
this.setState({ saving: true });
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
const { language } = this.state;
|
2018-10-23 21:39:48 +00:00
|
|
|
const { userLanguage, setUser } = 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);
|
2018-09-25 19:28:42 +00:00
|
|
|
setUser({ language: params.language });
|
2018-06-13 01:33:00 +00:00
|
|
|
|
|
|
|
this.setState({ saving: false });
|
|
|
|
setTimeout(() => {
|
2019-05-15 19:33:30 +00:00
|
|
|
this.toast.show(I18n.t('Preferences_saved'));
|
2018-06-13 01:33:00 +00:00
|
|
|
}, 300);
|
|
|
|
} catch (e) {
|
|
|
|
this.setState({ saving: false });
|
|
|
|
setTimeout(() => {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-21 12:12:15 +00:00
|
|
|
toggleMarkdown = (value) => {
|
|
|
|
AsyncStorage.setItem(MARKDOWN_KEY, JSON.stringify(value));
|
|
|
|
const { toggleMarkdown } = this.props;
|
|
|
|
toggleMarkdown(value);
|
|
|
|
Answers.logCustom('toggle_markdown', { value });
|
|
|
|
}
|
|
|
|
|
2018-06-05 01:17:02 +00:00
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
language, languages, placeholder, saving
|
|
|
|
} = this.state;
|
2019-05-21 12:12:15 +00:00
|
|
|
const { useMarkdown } = this.props;
|
2018-06-05 01:17:02 +00:00
|
|
|
return (
|
2018-06-13 01:33:00 +00:00
|
|
|
<KeyboardView
|
|
|
|
contentContainerStyle={sharedStyles.container}
|
|
|
|
keyboardVerticalOffset={128}
|
|
|
|
>
|
2019-03-12 16:23:06 +00:00
|
|
|
<StatusBar />
|
2018-06-13 01:33:00 +00:00
|
|
|
<ScrollView
|
|
|
|
contentContainerStyle={sharedStyles.containerScrollView}
|
|
|
|
testID='settings-view-list'
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
>
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={sharedStyles.container} testID='settings-view' forceInset={{ bottom: 'never' }}>
|
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>
|
2019-05-21 12:12:15 +00:00
|
|
|
<View style={styles.separator} />
|
|
|
|
<View style={styles.swithContainer}>
|
|
|
|
<Text style={styles.label}>{I18n.t('Enable_markdown')}</Text>
|
|
|
|
<Switch
|
|
|
|
value={useMarkdown}
|
|
|
|
onValueChange={this.toggleMarkdown}
|
|
|
|
onTintColor={COLOR_SUCCESS}
|
|
|
|
tintColor={isAndroid ? COLOR_DANGER : null}
|
|
|
|
/>
|
|
|
|
</View>
|
2018-09-25 19:28:42 +00:00
|
|
|
<Loading visible={saving} />
|
2019-05-15 19:33:30 +00:00
|
|
|
<Toast ref={toast => this.toast = toast} />
|
2018-06-13 01:33:00 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
|
|
|
</KeyboardView>
|
2018-06-05 01:17:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|