<commit_before>import React, { PropTypes } from 'react';

import omit from 'lodash/omit';

import AddPairingNoticeFormComponent from './AddPairingNoticeForm';
import noticeStore from '../../stores/notices';

const PAIRING_TECHS = ['ScreenHero', 'TeamViewer', 'GoogleHangouts', 'Skype'];

export default class AddPairingNoticeForm extends React.Component {
  constructor( props ) {
    super( props );

    this.state = {
      username: '',
      availableTime: '',
      'setup[]': [],
      other: '',
      interests: '',
    };

    this.handleFieldChange = this.handleFieldChange.bind( this );
    this.handleSubmit = this.handleSubmit.bind( this );
  }

  handleFieldChange( event ) {
    let value;
    if ( event.target.name === 'setup[]' ) {
      // TODO HACK this should be doable without touching dom nodes
      const inputs = [...document.getElementsByName( 'setup[]' )]; // getElementsByName returns an iterable, spread it into an array for access to filter and map
      value = inputs
        .filter( input => input.checked )
        .map( input => input.value );
    } else {
      value = event.target.value;
    }
    this.setState( {
      [event.target.name]: value,
    } );
  }

  handleSubmit( event ) {
    event.preventDefault();
    const data = {
      username: this.state.username,
      availableTime: this.state.availableTime,
      setup: this.state.other ? [...this.state['setup[]'], this.state.other] : this.state['setup[]'],
      interests: this.state.interests,
    };

    noticeStore.createNotice( data );

    this.props.handleDidSubmit && this.props.handleDidSubmit();
  }

  render() {
    const rest = omit( this.props, ['handleDidSubmit'] );

    return (
      <AddPairingNoticeFormComponent
        handleFieldChange={this.handleFieldChange}
        handleSubmit={this.handleSubmit}
        pairingTechs={PAIRING_TECHS}
        {...omit( this.state, ['setup[]'] )} // pass in the values of the form fields except `setup[]`
        {...rest}
      />
    );<commit_msg>track checkboxes without querying dom nodes in AddPairingNoticeForm<commit_after>import React, { PropTypes } from 'react';

import omit from 'lodash/omit';
import uniq from 'lodash/uniq';
import without from 'lodash/without';
import intersection from 'lodash/intersection';

import AddPairingNoticeFormComponent from './AddPairingNoticeForm';
import noticeStore from '../../stores/notices';

const PAIRING_TECHS = ['ScreenHero', 'TeamViewer', 'GoogleHangouts', 'Skype'];

export default class AddPairingNoticeForm extends React.Component {
  constructor( props ) {
    super( props );

    this.state = {
      username: '',
      availableTime: '',
      'setup[]': [],
      other: '',
      interests: '',
    };

    this.handleFieldChange = this.handleFieldChange.bind( this );
    this.handleSubmit = this.handleSubmit.bind( this );
  }

  handleFieldChange( event ) {
    let value;
    if ( event.target.name === 'setup[]' ) {
      if ( event.target.checked ) {
        value = uniq( [...this.state['setup[]'], event.target.value] );
      } else {
        value = without( this.state['setup[]'], [event.target.value] );
      }
    } else {
      value = event.target.value;
    }
    this.setState( {
      [event.target.name]: value,
    } );
  }

  handleSubmit( event ) {
    event.preventDefault();
    const data = {
      username: this.state.username,
      availableTime: this.state.availableTime,
      setup: this.state.other
        ? [...intersection( PAIRING_TECHS, this.state['setup[]'] ), this.state.other]
        : intersection( PAIRING_TECHS, this.state['setup[]'] ),
        // intersection ensures the same order as PAIRING_TECHS
      interests: this.state.interests,
    };

    noticeStore.createNotice( data );

    this.props.handleDidSubmit && this.props.handleDidSubmit();
  }

  render() {
    const rest = omit( this.props, ['handleDidSubmit'] );

    return (
      <AddPairingNoticeFormComponent
        handleFieldChange={this.handleFieldChange}
        handleSubmit={this.handleSubmit}
        pairingTechs={PAIRING_TECHS}
        {...omit( this.state, ['setup[]'] )} // pass in the values of the form fields except `setup[]`
        {...rest}
      />
    );
{'fraction_non_alphanumeric': 0.14218009478672985, 'fraction_numerical': 0.0, 'mean_word_length': 2.8831646734130634, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3743676', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1245, 'n_words': 284}