import React from 'react'; import PropTypes from 'prop-types'; import { View, ScrollView, SafeAreaView, Dimensions } from 'react-native'; import RNPickerSelect from 'react-native-picker-select'; import { connect } from 'react-redux'; import LoggedView from '../View'; 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 as setUserAction } from '../../actions/login'; @connect(state => ({ userLanguage: state.login.user && state.login.user.language }), dispatch => ({ setUser: params => dispatch(setUserAction(params)) })) /** @extends React.Component */ export default class SettingsView extends LoggedView { static propTypes = { navigator: PropTypes.object, userLanguage: PropTypes.string, setUser: PropTypes.func } constructor(props) { super('SettingsView', props); this.state = { placeholder: {}, language: props.userLanguage ? props.userLanguage : 'en', languages: [{ label: 'English', value: 'en' }, { label: 'Português', value: 'pt-BR' }, { label: 'Russian', value: 'ru' }], saving: false }; props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this)); } componentWillMount() { const { navigator } = this.props; navigator.setButtons({ leftButtons: [{ id: 'settings', icon: { uri: 'settings', scale: Dimensions.get('window').scale } }] }); } componentDidMount() { const { navigator } = this.props; navigator.setDrawerEnabled({ side: 'left', enabled: true }); } onNavigatorEvent(event) { const { navigator } = this.props; if (event.type === 'NavBarButtonPress') { if (event.id === 'settings') { navigator.toggleDrawer({ side: 'left' }); } } } getLabel = (language) => { const { languages } = this.state; const l = languages.find(i => i.value === language); if (l && l.label) { return l.label; } return null; } formIsChanged = () => { const { userLanguage } = this.props; const { language } = this.state; return !(userLanguage === language); } submit = async() => { this.setState({ saving: true }); const { language } = this.state; const { userLanguage, setUser, navigator } = this.props; if (!this.formIsChanged()) { return; } const params = {}; // language if (userLanguage !== language) { params.language = language; } try { await RocketChat.saveUserPreferences(params); setUser({ language: params.language }); this.setState({ saving: false }); setTimeout(() => { showToast(I18n.t('Preferences_saved')); if (params.language) { navigator.setTitle({ title: I18n.t('Settings') }); } }, 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); } } render() { const { language, languages, placeholder, saving } = this.state; return ( { this.setState({ language: value }); }} value={language} placeholder={placeholder} > { this.name = e; }} label={I18n.t('Language')} placeholder={I18n.t('Language')} value={this.getLabel(language)} testID='settings-view-language' />