<commit_before>import React, { Component } from 'react';
import Redirect from 'react-router/Redirect';
import PropTypes from 'prop-types';
import { isEmail } from 'validator';
import './RegisterView.css';
import Client from '../Client';
import Loader from './Loader';
import PageTitle from './PageTitle';
import ViewContainer from './ViewContainer';
import RegisterForm from './RegisterForm';

const contextTypes = {
  language: PropTypes.object,
};

class RegisterView extends Component {
  constructor() {
    super();

    this.onInputChange = this.onInputChange.bind(this);
    this.onFormSubmit = this.onFormSubmit.bind(this);
  }
  state = {
    fields: {
      email: '',
      password: '',
      confirmpassword: '',
      org: '',
      neworg: '',
      recaptcha: '',
    },
    organizations: [],
    fieldErrors: [],
    loading: false,
    redirect: false,
  }

  componentDidMount() {
    const uri = '/api/visitor/organizations';
    Client.get(uri)
      .then((organizations) => {
        if (organizations.length > 0) {
          this.setState({ organizations });
        }
      });
  }


  onInputChange(event) {
    const value = event.target.value;
    const name = event.target.name;
    const fields = this.state.fields;
    fields[name] = value;
    this.setState({ fields });

    if (this.state.fields.org === 'new') {
      const newOrg = '';
      this.setState({ newOrg });
    } else {
      const newOrg = 'hidden';
      this.setState({ newOrg });
    }
  }

  onFormSubmit(event) {
    event.preventDefault();
    const fieldErrors = this.validate(this.state.fields);
    this.setState({ fieldErrors });


    // Return on Errors
    if (fieldErrors.length) return;

    // Do registration
    this.setState({ loading: true });
    const uri = '/api/visitor/registration';
    const user = {
      email: this.state.fields.email,
      password: <PASSWORD>.<PASSWORD>.password,
      passwordConfirmation: this.state.fields.confirmpassword,
      organization: this.state.fields.neworg || this.state.fields.org,
      gRecaptchaResponse: this.state.fields.recaptcha,
    };

    Client.post(user, uri).then((res) => {
      this.setState({ loading: false });
      if (Object.prototype.hasOwnProperty.call(res, 'message')) {
        fieldErrors.push(res.message);
        this.setState({ fieldErrors });
      } else {
        this.setState({ redirect: true });
        this.setState({ fields: {} });
      }
    });
  }

  callback = (key) => {
    const fields = this.state.fields;<commit_msg>fixed mutable state issue<commit_after>import React, { Component } from 'react';
import Redirect from 'react-router/Redirect';
import PropTypes from 'prop-types';
import { isEmail } from 'validator';
import './RegisterView.css';
import Client from '../Client';
import Loader from './Loader';
import PageTitle from './PageTitle';
import ViewContainer from './ViewContainer';
import RegisterForm from './RegisterForm';

const contextTypes = {
  language: PropTypes.object,
};

class RegisterView extends Component {
  constructor() {
    super();

    this.onInputChange = this.onInputChange.bind(this);
    this.onFormSubmit = this.onFormSubmit.bind(this);
  }
  state = {
    fields: {
      email: '',
      password: '',
      confirmpassword: '',
      org: '',
      neworg: '',
      recaptcha: '',
    },
    organizations: [],
    fieldErrors: [],
    loading: false,
    redirect: false,
  }

  componentDidMount() {
    const uri = '/api/visitor/organizations';
    Client.get(uri)
      .then((organizations) => {
        if (organizations.length > 0) {
          this.setState({ organizations });
        }
      });
  }


  onInputChange(event) {
    const value = event.target.value;
    const name = event.target.name;
    const fields = this.state.fields;
    fields[name] = value;
    this.setState({ fields });

    if (this.state.fields.org === 'new') {
      const newOrg = '';
      this.setState({ newOrg });
    } else {
      const newOrg = 'hidden';
      this.setState({ newOrg });
    }
  }

  onFormSubmit(event) {
    event.preventDefault();
    const fieldErrors = this.validate(this.state.fields);
    this.setState({ fieldErrors });


    // Return on Errors
    if (fieldErrors.length) return;

    // Do registration
    this.setState({ loading: true });
    const uri = '/api/visitor/registration';
    const user = {
      email: this.state.fields.email,
      password: <PASSWORD>,
      passwordConfirmation: <PASSWORD>,
      organization: this.state.fields.neworg || this.state.fields.org,
      gRecaptchaResponse: this.state.fields.recaptcha,
    };

    Client.post(user, uri).then((res) => {
      this.setState({ loading: false });
      if (Object.prototype.hasOwnProperty.call(res, 'message')) {
        fieldErrors.push(res.message);
        this.setState({ fieldErrors });
      } else {
        this.setState({ redirect: true });
        this.setState({ fields: {} });
      }
    });
  }

  callback = (key) => {
    const fields = Object.assign({}, this.state.fields);
{'fraction_non_alphanumeric': 0.1487128712871287, 'fraction_numerical': 0.00039603960396039607, 'mean_word_length': 2.7331855136733187, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '916086', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1492, 'n_words': 325}