import React, {Component} from 'react';
import {Container, Header, Content, Form, Item, Input, Label, Spinner, Button, Text, Icon, Toast} from 'native-base';
import {Platform, StyleSheet, View} from "react-native";
import EmailValidator from 'email-validator';
import platform from "native-base/src/theme/variables/platform";
import theme from "../../theme";
export default class UserRegister extends Component {
constructor(props) {
super(props);
this.state = {
firstName: '',
lastName: '',
email: '',
phoneNumber: '',
password: '',
errors: {},
touched: new Set()
}
};
validateForm(triggeredBy) {
const fieldNames = ['firstName', 'lastName', 'email', 'phoneNumber', 'password'];
let errors = Object.assign({}, this.state.errors);
let touched = new Set(this.state.touched).add(triggeredBy);
/* No empty fields */
for(let key of fieldNames) {
const value = this.state[key];
if(value === '') {
errors[key] = 'kan ikke være tomt';
} else if(key === 'email' && !EmailValidator.validate(value)) {
errors[key] = 'er ikke en gyldig e-post';
} else {
delete errors[key];
}
}
this.setState({errors: errors, touched});
};
fieldHasError(field) {
return this.state.touched.has(field) && field in this.state.errors;
}
canSubmitForm() {
return Object.keys(this.state.errors).length === 0;
}
firstNameInput() {
return -
{
this.setState({firstName}, () => { this.validateForm('firstName'); });
}}
onSubmitEditing={() => {
this.refs.lastNameInput._root.focus()
}}
value={this.state.firstName}
/>
{this.fieldHasError('firstName') && }
;
}
lastNameInput() {
return -
{
this.setState({lastName}, () => { this.validateForm('lastName'); });
}}
onSubmitEditing={() => {
this.refs.emailInput._root.focus()
}}
value={this.state.lastName}
/>
{this.fieldHasError('lastName') && }
;
}
emailInput() {
return -
{
this.setState({email}, () => { this.validateForm('email'); });
}}
onSubmitEditing={() => {
this.refs.phoneInput._root.focus()
}}
value={this.state.email}
/>
{this.fieldHasError('email') && }
;
}
phoneNumberInput() {
return -
{
this.setState({phoneNumber}, () => { this.validateForm('phoneNumber'); });
}}
onSubmitEditing={() => {
this.refs.passwordInput._root.focus()
}}
value={this.state.phoneNumber}
/>
{this.fieldHasError('phoneNumber') && }
;
}
passwordInput() {
return -
{
this.setState({password}, () => { this.validateForm('password'); });
}}
onSubmitEditing={this.onRegisterPress}
value={this.state.password}
/>
{this.fieldHasError('password') && }
}
componentWillReceiveProps(nextProps) {
if(this.props.registerError !== nextProps.registerError) {
this.setState({'errors': nextProps.registerError})
}
}
showNonFieldError = () => {
const errors = this.props.registerError;
if (Object.keys(errors).length !== 0 && 'non_field_errors' in errors) {
const err = errors.non_field_errors;
let errorFormatted = err ? err[0] : 'Kunne ikke registrere bruker, prøv igjen...';
return {errorFormatted}
}
return
};
showFieldError = (field) => {
if(!this.fieldHasError(field) ) {
return;
}
return - {this.state.errors[field]}
;
};
showSpinner = () => {
if (this.props.isRegisteringUser) {
return
}
};
render() {
return (
);
}
onRegisterPress = () => {
if( !this.canSubmitForm() ) {
Toast.show({
text: 'Noen av feltene er ikke fylt ut riktig',
position: 'bottom',
buttonText: 'OK',
duration: 1500,
});
return;
}
this.props.onRegisterPress(
this.state.firstName,
this.state.lastName,
this.state.email,
this.state.phoneNumber,
this.state.password,
);
};
registerbutton() {
const isDisabled = this.state.touched.size === 0 || this.props.isRegisteringUser || !this.canSubmitForm();
return ();
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
input: {
width: 320,
fontSize: 20,
height: 52,
},
registerButton: {
marginBottom: 16,
},
errorBox: {
marginVertical: 10,
},
errorMessage: {
textAlign: 'center',
color: platform.btnDangerBg,
fontSize: 14,
},
card: theme.card,
errorText: {
paddingTop: 2,
paddingBottom: 12,
}
});