<commit_before>import React from 'react';
import { Button, Modal, Table, Form, FormGroup, FormControl, Col, ControlLabel } from 'react-bootstrap';
import AddFriendsByUserButton from './addFriendsByUser.js';
import { connect } from 'react-redux';
import { PhoneNumberUtil, PhoneNumberFormat } from 'google-libphonenumber';
import { addDebtor } from '../actions/finalActions.js';

const phoneUtil = PhoneNumberUtil.getInstance();

const mapStateToProps = state => {
  return {
    debtors: state.output.debtors
  };
};

const mapDispatchToProps = dispatch => {
  return {
    addDebtor: (input) => dispatch(
      addDebtor(input)
    ),
  };
};

export class AddFriends extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showModal: false,
      name: null,
      number: null,
      validationState: null,
      isValid: null
    };
  }

  handleAdd(e) {
    e.preventDefault();
    this.saveFriendInfo();
    this.setState({
      name: '',
      number: '',
      validationState: null
    });
    this.toggle();
  }

  handleFailAdd(e) {
    e.preventDefault();
    this.setState({
      validationState: 'error'
    });
  }

  friendName(e) {
    this.setState({
      name: e.target.value
    });
  }

  friendNumber(e) {
    e.target.value = phoneUtil.format(phoneUtil.parse(e.target.value, 'US'), PhoneNumberFormat.NATIONAL);
    this.setState({
      number: e.target.value,
      validationState: null,
      isValid: phoneUtil.isValidNumber(phoneUtil.parse(e.target.value, 'US'))
    });
  }

  saveFriendInfo() {
    let debtor = {
      name: this.state.name,
      phone: this.state.number,
      email: null,
      email: null,
      total: null,
      tax: null,
      tip: null,
      debtTotal: null,
      items: [],
    };
    this.props.addDebtor(debtor);
  }

  toggle() {
    this.setState({
      showModal: !this.state.showModal,
      validationState: null
    });
  }

  render() {
    return (
      <div className="text-center">
        <Button id="add-friends" bsStyle="primary" bsSize="small" onClick={this.toggle.bind(this)}>
          Phone
        </Button>
        <Modal show={this.state.showModal} onHide={this.toggle.bind(this)}>
          <Modal.Header closeButton>
            <Modal.Title>Add a friend</Modal.Title>
          </Modal.Header>
          <Modal.Body>
            <Form horizontal onSubmit={this.state.isValid ? this.handleAdd.bind(this) : this.handleFailAdd.bind(this)}>
              <FormGroup controlId="formName">
                <Col componentClass={ControlLabel} sm={2}>
                  Name
                </Col>
                <Col sm={10}>
                  <FormControl type="text" placeholder="Friend's Name" onChange={this.friendName.bind(this)}/>
                </Col>
              </FormGroup>
            </Form>
            <Form horizontal onSubmit={this.state.isValid ? this.handleAdd.bind(this) : this.handleFailAdd.bind(this)}>
              <FormGroup controlId="formPhoneNumber" validationState={this.state.validationState}>
                <Col componentClass={ControlLabel} sm={2}>
                  Number
                </Col>
                <Col sm={10}>
                  <FormControl type="text" placeholder="(xxx)xxx-xxxx" onChange={this.friendNumber.bind(this)}/>
                </Col>
              </FormGroup>
            </Form>
          </Modal.Body>
          <Modal.Footer>
            <Button bsStyle="primary" onClick={this.state.isValid ? this.handleAdd.bind(this) : this.handleFailAdd.bind(this)}>ADD</Button>
          </Modal.Footer>
        </Modal>
      </div>
    );
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(AddFriends);<commit_msg>(bug) fix bug that allowed friend to make nameless friends (#85)<commit_after>import React from 'react';
import { Button, Modal, Table, Form, FormGroup, FormControl, Col, ControlLabel } from 'react-bootstrap';
import AddFriendsByUserButton from './addFriendsByUser.js';
import { connect } from 'react-redux';
import { PhoneNumberUtil, PhoneNumberFormat } from 'google-libphonenumber';
import { addDebtor } from '../actions/finalActions.js';

const phoneUtil = PhoneNumberUtil.getInstance();

const mapStateToProps = state => {
  return {
    debtors: state.output.debtors
  };
};

const mapDispatchToProps = dispatch => {
  return {
    addDebtor: (input) => dispatch(
      addDebtor(input)
    ),
  };
};

export class AddFriends extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isValidNumber: false,
      name: '',
      number: null,
      showModal: false,
      validationStateName: null,
      validationStateNumber: null
    };
  }

  handleAdd(e) {
    e.preventDefault();
    this.saveFriendInfo();
    this.setState({
      isValidNumber: false,
      name: '',
      number: null,
      validationStateName: null,
      validationStateNumber: null
    });
    this.toggle();
  }

  handleFailAdd(e) {
    e.preventDefault();
    if (!this.state.name.length || !this.state.name.match(/^[a-z0-9]+$/i)) {
      this.setState({
        validationStateName: 'error'
      });
    }
    if (!this.state.isValidNumber) {
      this.setState({
        validationStateNumber: 'error'
      });
    }
  }

  friendName(e) {
    this.setState({
      name: e.target.value,
      validationStateName: null
    });
  }

  friendNumber(e) {
    e.target.value = phoneUtil.format(phoneUtil.parse(e.target.value, 'US'), PhoneNumberFormat.NATIONAL);
    this.setState({
      number: e.target.value,
      validationStateNumber: null,
      isValidNumber: phoneUtil.isValidNumber(phoneUtil.parse(e.target.value, 'US')),
    });
  }

  saveFriendInfo() {
    let debtor = {
      name: this.state.name,
      phone: this.state.number,
      email: null,
      email: null,
      total: null,
      tax: null,
      tip: null,
      debtTotal: null,
      items: [],
    };
    this.props.addDebtor(debtor);
  }

  toggle() {
    this.setState({
      showModal: !this.state.showModal,
      validationState: null
    });
  }

  render() {
    return (
      <div className="text-center">
        <Button id="add-friends" bsStyle="primary" bsSize="small" onClick={this.toggle.bind(this)}>
          Phone
        </Button>
        <Modal show={this.state.showModal} onHide={this.toggle.bind(this)}>
          <Modal.Header closeButton>
            <Modal.Title>Add a friend</Modal.Title>
          </Modal.Header>
          <Modal.Body>
            <Form horizontal onSubmit={(this.state.name.length || this.state.name.match(/^[a-z0-9]+$/i)) && this.state.isValidNumber ? this.handleAdd.bind(this) : this.handleFailAdd.bind(this)}>
              <FormGroup controlId="formName" validationState={this.state.validationStateName}>
                <Col componentClass={ControlLabel} sm={2}>
                  Name
                </Col>
                <Col sm={10}>
                  <FormControl type="text" placeholder="Friend's Name" onChange={this.friendName.bind(this)}/>
                </Col>
              </FormGroup>
            </Form>
            <Form horizontal onSubmit={(this.state.name.length || this.state.name.match(/^[a-z0-9]+$/i)) && this.state.isValidNumber ? this.handleAdd.bind(this) : this.handleFailAdd.bind(this)}>
              <FormGroup controlId="formPhoneNumber" validationState={this.state.validationStateNumber}>
                <Col componentClass={ControlLabel} sm={2}>
                  Number
                </Col>
                <Col sm={10}>
                  <FormControl type="text" placeholder="(xxx)xxx-xxxx" onChange={this.friendNumber.bind(this)}/>
                </Col>
              </FormGroup>
            </Form>
          </Modal.Body>
          <Modal.Footer>
            <Button bsStyle="primary" onClick={(this.state.name.length || this.state.name.match(/^[a-z0-9]+$/i)) && this.state.isValidNumber ? this.handleAdd.bind(this) : this.handleFailAdd.bind(this)}>ADD</Button>
          </Modal.Footer>
        </Modal>
      </div>
    );
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(AddFriends);
{'fraction_non_alphanumeric': 0.14254034741899715, 'fraction_numerical': 0.0027103609708020206, 'mean_word_length': 2.381091211995002, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 77, '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': '6941477', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2425, 'n_words': 440}