<commit_before>import React, { Component } from 'react';
import { connect } from 'react-redux';
import { compact, get, join, sample, isEmpty } from 'lodash';
import Select from '../../components/SweetSelect/SweetSelect';
import Input from '../../components/SweetInput/SweetInput';
import Button from '../../components/Button/Button';
import FormGroup from '../../components/Form/FormGroup';
import EmailEditor from '../../components/EmailEditor/EmailEditor';
import ErrorMessages from '../../components/ErrorMessages';
import ConsentComponent from '../../components/consent/ConsentComponent';
import { showConsentRequired } from '../../state/consent/index';
import { FormattedMessage } from 'react-intl';
import './EmailToolView.scss';
import { MailerClient } from '../../util/ChampaignClient';
import {
  convertHtmlToPlainText,
  copyToClipboard,
  composeEmailLink,
  buildToEmailForCompose,
} from '../../util/util';

import './EmailToolView';
import consent from '../../modules/consent/consent';
import { changeConsent } from '../../state/consent';

function emailTargetAsSelectOption(target) {
  return {
    label: join(compact([target.name, target.title]), ', '),
    value: target.id,
  };
}

export class EmailToolView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: this.props.name,
      email: this.props.email,
      isRequiredNew: this.props.isRequiredNew,
      emailService: null,
      subject: this.props.emailSubject,
      body: '', // this is the complete body: header + body + footer
      target: sample(this.props.targets),
      targetsForSelection: this.props.targets.map(emailTargetAsSelectOption),
      clickedCopyBodyButton: false,
      errors: {},
      isSubmitting: false,
      allTargetEmails: this.props.targets,
    };
  }

  targetId() {
    if (this.props.manualTargeting) {
      return get(this.state.target, 'id', undefined);
    } else {
      // EmailToolSender.rb relies on 'all' as target_id in case we're targeting all of the targets.
      return 'all';
    }
  }

  payload() {
    const payload = {
      page_id: this.props.pageId,
      email: {
        body: this.state.body,
        subject: this.state.subject,
        from_name: this.state.name,
        from_email: this.state.email,
        target_id: this.targetId(),
        country: this.props.country,
        email_service: this.state.emailService,
        clicked_copy_body_button: this.state.clickedCopyBodyButton,
      },
      tracking_params: {
        ...this.props.trackingParams,
      },
    };
    // For double optin countries consented field should not have a value
    if (this.props.consented !== null) {
      payload.consented = this.props.consented ? 1 : 0;
    }
    return payload;
  }

  validateForm() {
    const errors = {};
    // For GDPR countries alone this field should have value
    if (this.state.isRequiredNew && this.props.consented === null) {
      this.props.setShowConsentRequired(true);
      errors['consented'] = true;
    }
    this.setState({ errors: errors });
    return isEmpty(errors);
  }

  // Event Handlers<commit_msg>Don't show error if member have already consented<commit_after>import React, { Component } from 'react';
import { connect } from 'react-redux';
import { compact, get, join, sample, isEmpty } from 'lodash';
import Select from '../../components/SweetSelect/SweetSelect';
import Input from '../../components/SweetInput/SweetInput';
import Button from '../../components/Button/Button';
import FormGroup from '../../components/Form/FormGroup';
import EmailEditor from '../../components/EmailEditor/EmailEditor';
import ErrorMessages from '../../components/ErrorMessages';
import ConsentComponent from '../../components/consent/ConsentComponent';
import { showConsentRequired } from '../../state/consent/index';
import { FormattedMessage } from 'react-intl';
import './EmailToolView.scss';
import { MailerClient } from '../../util/ChampaignClient';
import {
  convertHtmlToPlainText,
  copyToClipboard,
  composeEmailLink,
  buildToEmailForCompose,
} from '../../util/util';

import './EmailToolView';
import consent from '../../modules/consent/consent';
import { changeConsent } from '../../state/consent';

function emailTargetAsSelectOption(target) {
  return {
    label: join(compact([target.name, target.title]), ', '),
    value: target.id,
  };
}

export class EmailToolView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: this.props.name,
      email: this.props.email,
      isRequiredNew: this.props.isRequiredNew,
      emailService: null,
      subject: this.props.emailSubject,
      body: '', // this is the complete body: header + body + footer
      target: sample(this.props.targets),
      targetsForSelection: this.props.targets.map(emailTargetAsSelectOption),
      clickedCopyBodyButton: false,
      errors: {},
      isSubmitting: false,
      allTargetEmails: this.props.targets,
    };
  }

  targetId() {
    if (this.props.manualTargeting) {
      return get(this.state.target, 'id', undefined);
    } else {
      // EmailToolSender.rb relies on 'all' as target_id in case we're targeting all of the targets.
      return 'all';
    }
  }

  payload() {
    const payload = {
      page_id: this.props.pageId,
      email: {
        body: this.state.body,
        subject: this.state.subject,
        from_name: this.state.name,
        from_email: this.state.email,
        target_id: this.targetId(),
        country: this.props.country,
        email_service: this.state.emailService,
        clicked_copy_body_button: this.state.clickedCopyBodyButton,
      },
      tracking_params: {
        ...this.props.trackingParams,
      },
    };
    // For double optin countries consented field should not have a value
    if (this.props.consented !== null) {
      payload.consented = this.props.consented ? 1 : 0;
    }
    return payload;
  }

  validateForm() {
    const errors = {};
    // For GDPR countries alone this field should have value
    if (
      consent.isRequired(
        this.props.countryCode,
        window.champaign.personalization.member
      ) &&
      this.props.consented === null
    ) {
      this.props.setShowConsentRequired(true);
      errors['consented'] = true;
    }
    this.setState({ errors: errors });
    return isEmpty(errors);
  }

  // Event Handlers
{'fraction_non_alphanumeric': 0.13456423094977313, 'fraction_numerical': 0.0006258801439524331, 'mean_word_length': 2.96772191185599, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3428458', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1839, 'n_words': 440}