<commit_before>import React from "react";
import { connect } from "react-redux";
import { push } from "react-router-redux";
import { withRouter } from "react-router-dom";
import { Row, Col, Modal, ButtonToolbar } from "react-bootstrap";
import { get, upperFirst } from "lodash-es";

import { InputError, Button } from "../../base";
import { createOTU } from "../actions";
import { clearError } from "../../errors/actions";

const getInitialState = (props) => ({
    name: "",
    abbreviation: "",
    errorName: "",
    errorAbbreviation: "",
    error: props.error
});

class CreateOTU extends React.Component {

    constructor (props) {
        super(props);
        this.state = getInitialState(this.props);
    }

    static getDerivedStateFromProps (nextProps, prevState) {
        console.log("prevState: ", prevState.error, ", nextProps: ", nextProps.error);
        if (prevState.error !== nextProps.error) {
            if (nextProps.error === "Name already exists") {
                return { errorName: nextProps.error };
            } else if (nextProps.error === "Abbreviation already exists") {
                return { errorAbbreviation: nextProps.error };
            } else if (!nextProps.error.length) {
                return { error: "" };
            }

            return {
                errorName: "Name already exists",
                errorAbbreviation: "Abbrevation already exists"
            };
        }

        return null;
    }

    handleChange = (e) => {
        const { name, value } = e.target;
        const error = `error${upperFirst(name)}`;

        this.setState({
            [name]: value,
            [error]: ""
        });

        if (this.props.error) {
            this.props.onClearError("CREATE_OTU_ERROR");
        }
    };

    handleHide = () => {
        this.props.onHide(this.props);
    };

    handleModalExited = () => {
        this.setState(getInitialState(this.props));

        if (this.props.error) {
            this.props.onClearError("CREATE_OTU_ERROR");
        }
    };

    handleSubmit = (e) => {
        e.preventDefault();

        if (!this.state.name) {
            return this.setState({
                errorName: "Required Field"
            });
        }

        console.log("submit: ", this.state.name, this.state.abbreviation);

        if (!this.state.errorName || !this.state.errorAbbreviation) {<commit_msg>Fix create otu error handling<commit_after>import React from "react";
import { connect } from "react-redux";
import { push } from "react-router-redux";
import { withRouter } from "react-router-dom";
import { Row, Col, Modal, ButtonToolbar } from "react-bootstrap";
import { get, upperFirst } from "lodash-es";

import { InputError, Button } from "../../base";
import { createOTU } from "../actions";
import { clearError } from "../../errors/actions";

const getInitialState = () => ({
    name: "",
    abbreviation: "",
    errorName: "",
    errorAbbreviation: "",
    error: ""
});

class CreateOTU extends React.Component {

    constructor (props) {
        super(props);
        this.state = getInitialState();
    }

    static getDerivedStateFromProps (nextProps, prevState) {
        if (prevState.error !== nextProps.error) {
            if (nextProps.error === "Name already exists") {
                return { errorName: nextProps.error, error: nextProps.error };
            } else if (nextProps.error === "Abbreviation already exists") {
                return { errorAbbreviation: nextProps.error, error: nextProps.error };
            } else if (!nextProps.error.length) {
                return { error: "" };
            }
            return {
                errorName: "Name already exists",
                errorAbbreviation: "Abbrevation already exists",
                error: nextProps.error
            };
        }
        return null;
    }

    handleChange = (e) => {
        const { name, value } = e.target;
        const error = `error${upperFirst(name)}`;

        this.setState({
            [name]: value,
            [error]: ""
        });

        if (this.props.error) {
            this.props.onClearError("CREATE_OTU_ERROR");
        }
    };

    handleHide = () => {
        this.props.onHide(this.props);
    };

    handleModalExited = () => {
        this.setState(getInitialState());

        if (this.props.error) {
            this.props.onClearError("CREATE_OTU_ERROR");
        }
    };

    handleSubmit = (e) => {
        e.preventDefault();

        if (!this.state.name) {
            return this.setState({
                errorName: "Required Field"
            });
        }

        if (!this.state.errorName || !this.state.errorAbbreviation) {
{'fraction_non_alphanumeric': 0.14188470538183365, 'fraction_numerical': 0.0, 'mean_word_length': 2.0276883451384418, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '397229', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1311, 'n_words': 285}