Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Component } from 'react';
- import { signup, checkUsernameAvailability, checkEmailAvailability } from '../../util/APIUtils';
- import './Signup.css';
- import { Link } from 'react-router-dom';
- import {
- NAME_MIN_LENGTH, NAME_MAX_LENGTH,
- USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH,
- EMAIL_MAX_LENGTH,
- PASSWORD_MIN_LENGTH, PASSWORD_MAX_LENGTH
- } from '../../constants';
- import { Form, Input, Button, notification } from 'antd';
- const FormItem = Form.Item;
- class Signup extends Component {
- constructor(props) {
- super(props);
- this.state = {
- name: {
- value: ''
- },
- username: {
- value: ''
- },
- email: {
- value: ''
- },
- password: {
- value: ''
- }
- }
- this.handleInputChange = this.handleInputChange.bind(this);
- this.handleSubmit = this.handleSubmit.bind(this);
- this.validateUsernameAvailability = this.validateUsernameAvailability.bind(this);
- this.validateEmailAvailability = this.validateEmailAvailability.bind(this);
- this.isFormInvalid = this.isFormInvalid.bind(this);
- }
- handleInputChange(event, validationFun) {
- const target = event.target;
- const inputName = target.name;
- const inputValue = target.value;
- this.setState({
- [inputName] : {
- value: inputValue,
- ...validationFun(inputValue)
- }
- });
- }
- handleSubmit(event) {
- event.preventDefault();
- const signupRequest = {
- name: this.state.name.value,
- email: this.state.email.value,
- username: this.state.username.value,
- password: this.state.password.value
- };
- signup(signupRequest)
- .then(response => {
- notification.success({
- message: 'Demo Drop',
- description: "Je bent succesvol geregistreerd!",
- });
- this.props.history.push("/login");
- }).catch(error => {
- notification.error({
- message: 'Demo Drop',
- description: error.message || 'Sorry! Er is iets misgegaan. Probeer het opnieuw.'
- });
- });
- }
- isFormInvalid() {
- return !(this.state.name.validateStatus === 'success' &&
- this.state.username.validateStatus === 'success' &&
- this.state.email.validateStatus === 'success' &&
- this.state.password.validateStatus === 'success'
- );
- }
- render() {
- return (
- <div className="signup-container">
- <h1 className="page-title">Registreren</h1>
- <div className="signup-content">
- <Form onSubmit={this.handleSubmit} className="signup-form">
- <FormItem
- validateStatus={this.state.name.validateStatus}
- help={this.state.name.errorMsg}>
- <Input
- size="large"
- name="name"
- autoComplete="off"
- placeholder="Volledige naam"
- value={this.state.name.value}
- onChange={(event) => this.handleInputChange(event, this.validateName)} />
- </FormItem>
- <FormItem
- hasFeedback
- validateStatus={this.state.username.validateStatus}
- help={this.state.username.errorMsg}>
- <Input
- size="large"
- name="username"
- autoComplete="off"
- placeholder="Gebruikersnaam"
- value={this.state.username.value}
- onBlur={this.validateUsernameAvailability}
- onChange={(event) => this.handleInputChange(event, this.validateUsername)}/>
- </FormItem>
- <FormItem
- hasFeedback
- validateStatus={this.state.email.validateStatus}
- help={this.state.email.errorMsg}>
- <Input
- size="large"
- name="email"
- type="email"
- autoComplete="off"
- placeholder="E-mailadres"
- value={this.state.email.value}
- onBlur={this.validateEmailAvailability}
- onChange={(event) => this.handleInputChange(event, this.validateEmail)}/>
- </FormItem>
- <FormItem
- validateStatus={this.state.password.validateStatus}
- help={this.state.password.errorMsg}>
- <Input
- size="large"
- name="password"
- type="password"
- autoComplete="off"
- placeholder="Wachtwoord moet 6 tot 20 tekens bevatten"
- value={this.state.password.value}
- onChange={(event) => this.handleInputChange(event, this.validatePassword)}/>
- </FormItem>
- <FormItem>
- <Button type="primary"
- htmlType="submit"
- size="large"
- className="signup-form-button"
- disabled={this.isFormInvalid()}>registreer</Button>
- Al geregistreerd? <Link to="/login">Login!</Link>
- </FormItem>
- </Form>
- </div>
- </div>
- );
- }
- // Validatie functies
- validateName = (name) => {
- if(name.length < NAME_MIN_LENGTH) {
- return {
- validateStatus: 'error',
- errorMsg: `Naam te kort (Minimaal ${NAME_MIN_LENGTH} karakters invoeren.)`
- }
- } else if (name.length > NAME_MAX_LENGTH) {
- return {
- validationStatus: 'error',
- errorMsg: `Naam is te lang (Maximaal ${NAME_MAX_LENGTH} karakters toegestaan.)`
- }
- } else {
- return {
- validateStatus: 'success',
- errorMsg: null,
- };
- }
- };
- validateEmail = (email) => {
- if (!email) {
- return {
- validateStatus: 'error',
- errorMsg: 'Email-veld mag niet leeg zijn.'
- }
- }
- const EMAIL_REGEX = RegExp('[^@ ]+@[^@ ]+\\.[^@ ]+');
- if (!EMAIL_REGEX.test(email)) {
- return {
- validateStatus: 'error',
- errorMsg: 'Email niet valide'
- }
- }
- if (email.length > EMAIL_MAX_LENGTH) {
- return {
- validateStatus: 'error',
- errorMsg: `Email te lang (Maximaal ${EMAIL_MAX_LENGTH} karakters toegestaan)`
- }
- }
- return {
- validateStatus: null,
- errorMsg: null
- }
- };
- validateUsername = (username) => {
- if (username.length < USERNAME_MIN_LENGTH) {
- return {
- validateStatus: 'error',
- errorMsg: `Gebruikersnaam is te kort (Minimaal ${USERNAME_MIN_LENGTH} karakters.)`
- }
- } else if (username.length > USERNAME_MAX_LENGTH) {
- return {
- validationStatus: 'error',
- errorMsg: `Gebruikersnaam is te lang (Maximaal ${USERNAME_MAX_LENGTH} karakters toegestaan.)`
- }
- } else {
- return {
- validateStatus: null,
- errorMsg: null
- }
- }
- };
- validateUsernameAvailability() {
- // First check for client side errors in username
- const usernameValue = this.state.username.value;
- const usernameValidation = this.validateUsername(usernameValue);
- if (usernameValidation.validateStatus === 'error') {
- this.setState({
- username: {
- value: usernameValue,
- ...usernameValidation
- }
- });
- return;
- }
- this.setState({
- username: {
- value: usernameValue,
- validateStatus: 'validating',
- errorMsg: null
- }
- });
- checkUsernameAvailability(usernameValue)
- .then(response => {
- if (response.available) {
- this.setState({
- username: {
- value: usernameValue,
- validateStatus: 'success',
- errorMsg: null
- }
- });
- } else {
- this.setState({
- username: {
- value: usernameValue,
- validateStatus: 'error',
- errorMsg: 'Deze gebruikersnaam is al in gebruik'
- }
- });
- }
- }).catch(error => {
- this.setState({
- username: {
- value: usernameValue,
- validateStatus: 'success',
- errorMsg: null
- }
- });
- });
- };
- validateEmailAvailability() {
- const emailValue = this.state.email.value;
- const emailValidation = this.validateEmail(emailValue);
- if (emailValidation.validateStatus === 'error') {
- this.setState({
- email: {
- value: emailValue,
- ...emailValidation
- }
- });
- return;
- }
- this.setState({
- email: {
- value: emailValue,
- validateStatus: 'validating',
- errorMsg: null
- }
- });
- checkEmailAvailability(emailValue)
- .then(response => {
- if (response.available) {
- this.setState({
- email: {
- value: emailValue,
- validateStatus: 'success',
- errorMsg: null
- }
- });
- } else {
- this.setState({
- email: {
- value: emailValue,
- validateStatus: 'error',
- errorMsg: 'Dit e-mailadres is al in gebruik.'
- }
- });
- }
- }).catch(error => {
- this.setState({
- email: {
- value: emailValue,
- validateStatus: 'success',
- errorMsg: null
- }
- });
- });
- }
- validatePassword = (password) => {
- if (password.length < PASSWORD_MIN_LENGTH) {
- return {
- validateStatus: 'error',
- errorMsg: `Wachtwoord is te kort! (Minimaal ${PASSWORD_MIN_LENGTH} karakters lang.)`
- }
- } else if (password.length > PASSWORD_MAX_LENGTH) {
- return {
- validationStatus: 'error',
- errorMsg: `Wachtwoord is te lang (Maximaal ${PASSWORD_MAX_LENGTH} karakters lang.)`
- }
- } else {
- return {
- validateStatus: 'success',
- errorMsg: null,
- };
- }
- }
- }
- export default Signup;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement