import React, { Component } from 'react'; import { Container, Header, Content, Form, Item, Input, Label, Spinner, Button, Text, Icon, Toast } from 'native-base'; import { StyleSheet, View } from 'react-native'; import EmailValidator from 'email-validator'; 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']; const errors = Object.assign({}, this.state.errors); const touched = new Set(this.state.touched).add(triggeredBy); /* No empty fields */ for (const 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, 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; const errorFormatted = err ? err[0] : 'Kunne ikke registrere bruker, prøv igjen...'; return ( {errorFormatted} ); } if (Object.keys(errors).length !== 0 && 'detail' in errors) { const err = errors.detail; const errorFormatted = err || '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 (
{this.firstNameInput()} {this.showFieldError('firstName')} {this.lastNameInput()} {this.showFieldError('lastName')} {this.emailInput()} {this.showFieldError('email')} {this.phoneNumberInput()} {this.showFieldError('phoneNumber')} {this.passwordInput()} {this.showFieldError('password')} {this.showNonFieldError()} {this.registerbutton()} {this.showSpinner()}
); } 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: theme.colors.danger, fontSize: 14, }, card: theme.card, errorText: { paddingTop: 2, paddingBottom: 12, }, });