import React from 'react';
import PropTypes from 'prop-types';
import {
Keyboard, Text, View, ScrollView
} from 'react-native';
import { connect, Provider } from 'react-redux';
import { Navigation } from 'react-native-navigation';
import SafeAreaView from 'react-native-safe-area-view';
import { registerSubmit as registerSubmitAction, setUsernameSubmit as setUsernameSubmitAction } from '../actions/login';
import TextInput from '../containers/TextInput';
import Button from '../containers/Button';
import Loading from '../containers/Loading';
import KeyboardView from '../presentation/KeyboardView';
import styles from './Styles';
import { showToast } from '../utils/info';
import scrollPersistTaps from '../utils/scrollPersistTaps';
import LoggedView from './View';
import I18n from '../i18n';
import store from '../lib/createStore';
let TermsServiceView = null;
let PrivacyPolicyView = null;
@connect(state => ({
server: state.server.server,
Accounts_NamePlaceholder: state.settings.Accounts_NamePlaceholder,
Accounts_EmailOrUsernamePlaceholder: state.settings.Accounts_EmailOrUsernamePlaceholder,
Accounts_PasswordPlaceholder: state.settings.Accounts_PasswordPlaceholder,
Accounts_RepeatPasswordPlaceholder: state.settings.Accounts_RepeatPasswordPlaceholder,
login: state.login
}), dispatch => ({
registerSubmit: params => dispatch(registerSubmitAction(params)),
setUsernameSubmit: params => dispatch(setUsernameSubmitAction(params))
}))
/** @extends React.Component */
export default class RegisterView extends LoggedView {
static propTypes = {
componentId: PropTypes.string,
server: PropTypes.string,
registerSubmit: PropTypes.func.isRequired,
setUsernameSubmit: PropTypes.func,
Accounts_UsernamePlaceholder: PropTypes.string,
Accounts_NamePlaceholder: PropTypes.string,
Accounts_EmailOrUsernamePlaceholder: PropTypes.string,
Accounts_PasswordPlaceholder: PropTypes.string,
Accounts_RepeatPasswordPlaceholder: PropTypes.string,
login: PropTypes.object
}
constructor(props) {
super('RegisterView', props);
this.state = {
name: '',
email: '',
password: '',
confirmPassword: '',
username: ''
};
}
valid = () => {
const {
name, email, password, confirmPassword
} = this.state;
return name.trim() && email.trim()
&& password && confirmPassword && password === confirmPassword;
}
invalidEmail = () => {
const { login } = this.props;
return login.failure && /Email/.test(login.error && login.error.reason) ? login.error : {};
}
submit = () => {
const {
name, email, password, code
} = this.state;
const { registerSubmit } = this.props;
if (!this.valid()) {
showToast(I18n.t('Some_field_is_invalid_or_empty'));
return;
}
registerSubmit({
name, email, pass: password, code
});
Keyboard.dismiss();
}
usernameSubmit = () => {
const { username } = this.state;
const { setUsernameSubmit } = this.props;
if (!username) {
showToast(I18n.t('Username_is_empty'));
return;
}
setUsernameSubmit({ username });
Keyboard.dismiss();
}
termsService = () => {
if (TermsServiceView == null) {
TermsServiceView = require('./TermsServiceView').default;
Navigation.registerComponentWithRedux('TermsServiceView', () => TermsServiceView, Provider, store);
}
const { componentId } = this.props;
Navigation.push(componentId, {
component: {
name: 'TermsServiceView',
options: {
topBar: {
title: {
text: I18n.t('Terms_of_Service')
}
}
}
}
});
}
privacyPolicy = () => {
if (PrivacyPolicyView == null) {
PrivacyPolicyView = require('./PrivacyPolicyView').default;
Navigation.registerComponentWithRedux('PrivacyPolicyView', () => PrivacyPolicyView, Provider, store);
}
const { componentId } = this.props;
Navigation.push(componentId, {
component: {
name: 'PrivacyPolicyView',
options: {
topBar: {
title: {
text: I18n.t('Privacy_Policy')
}
}
}
}
});
}
_renderRegister() {
const { password, confirmPassword } = this.state;
const {
login, Accounts_NamePlaceholder, Accounts_EmailOrUsernamePlaceholder, Accounts_PasswordPlaceholder, Accounts_RepeatPasswordPlaceholder
} = this.props;
if (login.token) {
return null;
}
return (
{ this.name = e; }}
label={Accounts_NamePlaceholder || I18n.t('Name')}
placeholder={Accounts_NamePlaceholder || I18n.t('Name')}
returnKeyType='next'
iconLeft='account'
onChangeText={name => this.setState({ name })}
onSubmitEditing={() => { this.email.focus(); }}
testID='register-view-name'
/>
{ this.email = e; }}
label={Accounts_EmailOrUsernamePlaceholder || I18n.t('Email')}
placeholder={Accounts_EmailOrUsernamePlaceholder || I18n.t('Email')}
returnKeyType='next'
keyboardType='email-address'
iconLeft='email'
onChangeText={email => this.setState({ email })}
onSubmitEditing={() => { this.password.focus(); }}
error={this.invalidEmail()}
testID='register-view-email'
/>
{ this.password = e; }}
label={Accounts_PasswordPlaceholder || I18n.t('Password')}
placeholder={Accounts_PasswordPlaceholder || I18n.t('Password')}
returnKeyType='next'
iconLeft='key-variant'
secureTextEntry
onChangeText={value => this.setState({ password: value })}
onSubmitEditing={() => { this.confirmPassword.focus(); }}
testID='register-view-password'
/>
{ this.confirmPassword = e; }}
inputStyle={
password
&& confirmPassword
&& confirmPassword !== password ? { borderColor: 'red' } : {}
}
label={Accounts_RepeatPasswordPlaceholder || I18n.t('Repeat_Password')}
placeholder={Accounts_RepeatPasswordPlaceholder || I18n.t('Repeat_Password')}
returnKeyType='done'
iconLeft='key-variant'
secureTextEntry
onChangeText={value => this.setState({ confirmPassword: value })}
onSubmitEditing={this.submit}
testID='register-view-repeat-password'
/>
{I18n.t('By_proceeding_you_are_agreeing')}
{I18n.t('Terms_of_Service')}
{I18n.t('and')}
{I18n.t('Privacy_Policy')}
);
}
_renderUsername() {
const { login, Accounts_UsernamePlaceholder } = this.props;
if (!login.token) {
return null;
}
return (
{ this.username = e; }}
label={Accounts_UsernamePlaceholder || I18n.t('Username')}
placeholder={Accounts_UsernamePlaceholder || I18n.t('Username')}
returnKeyType='done'
iconLeft='at'
onChangeText={username => this.setState({ username })}
onSubmitEditing={() => { this.usernameSubmit(); }}
testID='register-view-username'
/>
);
}
render() {
const { login } = this.props;
return (
{I18n.t('Sign_Up')}
{this._renderRegister()}
{this._renderUsername()}
{login.failure
? (
{login.error.reason}
)
: null
}
);
}
}