<commit_before>import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import reduxForm from 'redux-form'

import { editWidget } from '../../../actions'
import { FormFooter, InputTag } from '../../../components'
import { Control, Input, Textarea } from '../../../components/FormUtils'

import { Base as PressureBase } from '../components/settings'


const EMAIL_TEXT_PLACEHOLDER = "Obrigado por apostar na força da ação coletiva em rede. Sua participação é muito importante e, agora, precisamos da sua ajuda para que mais gente colabore com esta mobilização.\nCompartilhe nas suas redes clicando em um dos links abaixo.\n\nUm abraço."

// Regex to validate Target (Ex.: <NAME> <<EMAIL>>)
const patternTarget = /[\w ]+<(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))>/

const widgetFormValidation = (data) => {
  const errors = { valid: true }
  if (!data.pressure_subject) {
    errors.pressure_subject = 'Insira um assunto para o e-mail'
    errors.valid = false
  }
  if (!data.pressure_body) {
    errors.pressure_body = 'Insira um corpo para o e-mail'
    errors.valid = false
  }
  return errors
}

const mapStateToProps = state => (
  {
    form: state.widgetForm,
    saving: state.widgets.saving,
    requestError: state.widgets.error
  }
)

@connect(mapStateToProps, { editWidgetAction: editWidget })
@reduxForm('widgetForm', widgetFormValidation)
class EmailPage extends Component {

  constructor(props) {
    super(props)

    this.state = { submitted: false, targets: this.getTargetList() || [] }

    const { pressure_subject, pressure_body } = this.props.widget.settings || {
      pressure_subject: '',
      pressure_body: ''
    }

    this.props.initializeForm({ pressure_subject, pressure_body })
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.saving && nextProps.saving && !nextProps.requestError) {
      this.setState({ submitted: true })
    }
  }

  getTargetList() {
    const { targets } = this.props.widget.settings || { targets: '' }
    return targets && targets.split(';')
  }

  getTargetString() {
    const { targets } = this.state
    return targets.join(';')
  }

  handleSubmit(e) {
    e.preventDefault()
    const { data, valid, touchAll } = this.props      // redux form
    const { auth, mobilization, widget } = this.props // containers
    const { editWidgetAction } = this.props           // connect actions
    if (valid) {
      const targets = this.getTargetString()
      editWidgetAction({
        mobilization_id: mobilization.id,
        widget_id: widget.id,
        credentials: auth.credentials,
        widget: { settings: {
          ...widget.settings,
          ...data,
          targets
        }}
      })
    } else {
      touchAll()
    }
  }

  render() {
    const {
      location, mobilization, widget, saving,
      data: { pressure_subject, pressure_body },
      ...inputProps
    } = this.props
    const { handleChange } = this.props
    return (
      <PressureBase location={location} mobilization={mobilization} widget={widget}>
        <form onSubmit={::this.handleSubmit}>
          <InputTag
            label="Alvos"
            values={this.state.targets}
            onInsertTag={value => this.setState({ targets: [...this.state.targets, value] })}
            onRemoveTag={value => this.setState({ targets: this.state.targets.filter(tag => tag !== value) })}
            validate={value => {
              const errors = { valid: true }
              if (!value.match(patternTarget)) {
                errors.valid = false
                errors.message = 'Alvo fora do formato padrão. Ex.: Nome do alvo <<EMAIL>>'
              }
              return errors
            }}
          />
          <Control id="email-subject-id" label="Assunto do email" name="pressure_subject" {...inputProps}>
            <Input type="text" value={pressure_subject} placeholder="Envie um e-mail para quem pode tomar essa decisão" />
          </Control>
          <Control id="email-subject-id" label="Corpo do email que será enviado para o alvo" name="pressure_body" {...inputProps}>
            <Textarea value={pressure_body} placeholder={EMAIL_TEXT_PLACEHOLDER} />
          </Control>
          <FormFooter submitted={this.state.submitted} saving={saving} />
        </form>
      </PressureBase>
    )
  }
}

EmailPage.propTypes = {
  location: PropTypes.shape({
    pathname: PropTypes.string
  }).isRequired,<commit_msg>Fix email-subject-id duplicate on page. Close #149<commit_after>import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import reduxForm from 'redux-form'

import { editWidget } from '../../../actions'
import { FormFooter, InputTag } from '../../../components'
import { Control, Input, Textarea } from '../../../components/FormUtils'

import { Base as PressureBase } from '../components/settings'


const EMAIL_TEXT_PLACEHOLDER = "Obrigado por apostar na força da ação coletiva em rede. Sua participação é muito importante e, agora, precisamos da sua ajuda para que mais gente colabore com esta mobilização.\nCompartilhe nas suas redes clicando em um dos links abaixo.\n\nUm abraço."

// Regex to validate Target (Ex.: <NAME> <<EMAIL>>)
const patternTarget = /[\w ]+<(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))>/

const widgetFormValidation = (data) => {
  const errors = { valid: true }
  if (!data.pressure_subject) {
    errors.pressure_subject = 'Insira um assunto para o e-mail'
    errors.valid = false
  }
  if (!data.pressure_body) {
    errors.pressure_body = 'Insira um corpo para o e-mail'
    errors.valid = false
  }
  return errors
}

const mapStateToProps = state => (
  {
    form: state.widgetForm,
    saving: state.widgets.saving,
    requestError: state.widgets.error
  }
)

@connect(mapStateToProps, { editWidgetAction: editWidget })
@reduxForm('widgetForm', widgetFormValidation)
class EmailPage extends Component {

  constructor(props) {
    super(props)

    this.state = { submitted: false, targets: this.getTargetList() || [] }

    const { pressure_subject, pressure_body } = this.props.widget.settings || {
      pressure_subject: '',
      pressure_body: ''
    }

    this.props.initializeForm({ pressure_subject, pressure_body })
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.saving && nextProps.saving && !nextProps.requestError) {
      this.setState({ submitted: true })
    }
  }

  getTargetList() {
    const { targets } = this.props.widget.settings || { targets: '' }
    return targets && targets.split(';')
  }

  getTargetString() {
    const { targets } = this.state
    return targets.join(';')
  }

  handleSubmit(e) {
    e.preventDefault()
    const { data, valid, touchAll } = this.props      // redux form
    const { auth, mobilization, widget } = this.props // containers
    const { editWidgetAction } = this.props           // connect actions
    if (valid) {
      const targets = this.getTargetString()
      editWidgetAction({
        mobilization_id: mobilization.id,
        widget_id: widget.id,
        credentials: auth.credentials,
        widget: { settings: {
          ...widget.settings,
          ...data,
          targets
        }}
      })
    } else {
      touchAll()
    }
  }

  render() {
    const {
      location, mobilization, widget, saving,
      data: { pressure_subject, pressure_body },
      ...inputProps
    } = this.props
    const { handleChange } = this.props
    return (
      <PressureBase location={location} mobilization={mobilization} widget={widget}>
        <form onSubmit={::this.handleSubmit}>
          <InputTag
            label="Alvos"
            values={this.state.targets}
            onInsertTag={value => this.setState({ targets: [...this.state.targets, value] })}
            onRemoveTag={value => this.setState({ targets: this.state.targets.filter(tag => tag !== value) })}
            validate={value => {
              const errors = { valid: true }
              if (!value.match(patternTarget)) {
                errors.valid = false
                errors.message = 'Alvo fora do formato padrão. Ex.: Nome do alvo <<EMAIL>>'
              }
              return errors
            }}
          />
          <Control id="email-subject-id" label="Assunto do email" name="pressure_subject" {...inputProps}>
            <Input type="text" value={pressure_subject} placeholder="Envie um e-mail para quem pode tomar essa decisão" />
          </Control>
          <Control id="email-body-id" label="Corpo do email que será enviado para o alvo" name="pressure_body" {...inputProps}>
            <Textarea value={pressure_body} placeholder={EMAIL_TEXT_PLACEHOLDER} />
          </Control>
          <FormFooter submitted={this.state.submitted} saving={saving} />
        </form>
      </PressureBase>
    )
  }
}

EmailPage.propTypes = {
  location: PropTypes.shape({
    pathname: PropTypes.string
  }).isRequired,
{'fraction_non_alphanumeric': 0.15148861646234676, 'fraction_numerical': 0.004487740805604203, 'mean_word_length': 2.640239043824701, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 55, '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': '531463', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2764, 'n_words': 641}