<commit_before>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './WidgetView.css';
import ErrorList from './ErrorList';
import ProvinceSelect from './ProvinceSelect';
import Client from '../Client';


class WidgetView extends Component {
  state = {
    fields: {
      region: '',
      province: '',
    },
    fieldErrors: [],
    showProvince: false,
    isGenerated: false,
    headCode: '',
    bodyCode: '',
    token: '',
  }


  componentWillMount() {
    const uri = '/api/token';
    Client.get(uri)
      .then((res) => {
        const token = res.token;
        this.setState({ token });
      });
  }

  onFormSubmit(event) {
    event.preventDefault();
    const fieldErrors = this.validate(this.state.fields);
    this.setState({ fieldErrors });

    // Return on Errors
    if (fieldErrors.length) return;

    const bodyCode = this.generateBodyCode(this.state.fields.region, this.state.fields.province);

    this.setState({ isGenerated: true });
    this.setState({ headCode: this.generateHeadCode() });
    this.setState({ bodyCode });
  }

  generateHeadCode() {
    return `<script src="${location.protocol}//${location.host}/widget.js}></script>`;
  }

  generateBodyCode(region, province) {
    return `<div id="visa-widget" data-country="${region}" data-region="${province}" data-token="${this.state.token}"></div>`;
  }

  onInputChange(event) {
    const fields = this.state.fields;
    fields[event.target.name] = event.target.value;
    this.setState({ fields });

    let showProvince;

    if (event.target.value !== '') {
      showProvince = true;
    } else {
      showProvince = false;
    }

    this.setState({ showProvince });
  }

  validate(fields) {
    const errors = [];
    if (!fields.region) errors.push(this.context.language.Errors.chooseRegion);
    return errors;
  }<commit_msg>Reorder methods<commit_after>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './WidgetView.css';
import ErrorList from './ErrorList';
import ProvinceSelect from './ProvinceSelect';
import Client from '../Client';


class WidgetView extends Component {
  state = {
    fields: {
      region: '',
      province: '',
    },
    fieldErrors: [],
    showProvince: false,
    isGenerated: false,
    headCode: '',
    bodyCode: '',
    token: '',
  }


  componentWillMount() {
    const uri = '/api/token';
    Client.get(uri)
      .then((res) => {
        const token = res.token;
        this.setState({ token });
      });
  }

  onFormSubmit(event) {
    event.preventDefault();
    const fieldErrors = this.validate(this.state.fields);
    this.setState({ fieldErrors });

    // Return on Errors
    if (fieldErrors.length) return;

    const bodyCode = this.generateBodyCode(this.state.fields.region, this.state.fields.province);

    this.setState({ isGenerated: true });
    this.setState({ headCode: this.generateHeadCode() });
    this.setState({ bodyCode });
  }

  onInputChange(event) {
    const fields = this.state.fields;
    fields[event.target.name] = event.target.value;
    this.setState({ fields });

    let showProvince;

    if (event.target.value !== '') {
      showProvince = true;
    } else {
      showProvince = false;
    }

    this.setState({ showProvince });
  }

  validate(fields) {
    const errors = [];
    if (!fields.region) errors.push(this.context.language.Errors.chooseRegion);
    return errors;
  }

  generateHeadCode() {
    return `<script src="${location.protocol}//${location.host}/widget.js}></script>`;
  }

  generateBodyCode(region, province) {
    return `<div id="visa-widget" data-country="${region}" data-region="${province}" data-token="${this.state.token}"></div>`;
  }
{'fraction_non_alphanumeric': 0.16014997321906801, 'fraction_numerical': 0.0, 'mean_word_length': 3.0953947368421053, 'pattern_counts': {'":': 0, '<': 11, '<?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': '348871', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1125, 'n_words': 229}