<commit_before>import React from 'react';
import { models } from 'snoode';
import throttle from 'lodash/function/throttle';

import BasePage from './BasePage';
import CaptchaBox from '../components/CaptchaBox';
import Modal from '../components/Modal';
import SeashellsDropdown from '../components/SeashellsDropdown';
import SubredditSelectionButton from '../components/SubredditSelectionButton';

function _removeNewLines(title) {
  return title.replace(/(\r\n|\n|\r)/gm,' ');
}

function _determineKind(body) {
  const words = body.trim(' ').split(' ');
  const hasProtocol = /^(http|https):\/\/[^ "]+$/.test(words[0]);

  if (words.length === 1 && hasProtocol) {
    return 'link';
  }

  return 'self';
}

const debouncedDetermineKind = throttle(_determineKind, 1000);

class SubmitPage extends BasePage {
  static propTypes = {
    // apiOptions: React.PropTypes.object,
    kind: React.PropTypes.string,
    resubmit: React.PropTypes.bool,
    subredditName: React.PropTypes.string,
    thingId: React.PropTypes.string,
    type: React.PropTypes.string,
  };

  constructor(props) {
    super(props);

    Object.assign(this.state, {
      subredditName: props.subredditName || 'choose a subreddit',
      subredditSelectionOpen: false,
      title: props.postTitle || '',
      body: props.body || '',
      kind: props.type || 'self',
      sendReplies: true,
      captchaIden: '',
      captchaAnswer: '',
      requiresCaptcha: false,
      captchaCount: 0,
      error: {
        type: '',
        message: '',
        fields: [],
      },
    });

    this.updateCaptchaInfo = this.updateCaptchaInfo.bind(this);
    this.submit = this.submit.bind(this);
    this.goToAboutPage = this.goToAboutPage.bind(this);
    this.toggleSubSelect = this.toggleSubSelect.bind(this);
    this.changeSubreddit = this.changeSubreddit.bind(this);
    this.changeSendReplies = this.changeSendReplies.bind(this);
    this.handleTitleChange = this.handleChange.bind(this, 'title');
    this.handleBodyChange = this.handleChange.bind(this, 'body');
    this.close = this.close.bind(this);
    this.closeModal = this.closeModal.bind(this);
  }

  componentDidMount () {
    super.componentDidMount();

    if (!this.props.app.config.localStorageAvailable) {
      return;
    }

    const saved = global.localStorage.getItem('savedLinkContent');

    if (saved) {
      const parsed = JSON.parse(saved);
      this.setState(parsed);
      global.localStorage.clear();
    }
  }

  submit (e) {
    e.preventDefault();
    this.setState({
      error: {
        type: '',
        message: '',
        fields: [],
      },
    });

    let title = this.refs.title.value.trim();
    const sub = this.state.subreddit;
    const body = this.refs.body.value.trim();

    const kind = _determineKind(body);
    const errors = this._validateContent(sub, title);

    if (errors.length) {
      this.setState({
        error: {
          type: 'Incomplete:',
          message: 'all required fields must be filled out.',
          fields: errors,
        },
      });

      this.props.app.emit('post:error');
      return;
    }

    title = _removeNewLines(title);

    this._submitPost(this.props.thingId, body, title, kind);
  }

  _validateContent (sub, title) {
    const errors = [];
    if (sub === '' || sub === 'choose a subreddit') {
      errors.push('subreddit');
    }
    if (title === '') {
      errors.push('title');
    }<commit_msg>Submit: fix subreddit being sent in submit<commit_after>import React from 'react';
import { models } from 'snoode';
import throttle from 'lodash/function/throttle';

import BasePage from './BasePage';
import CaptchaBox from '../components/CaptchaBox';
import Modal from '../components/Modal';
import SeashellsDropdown from '../components/SeashellsDropdown';
import SubredditSelectionButton from '../components/SubredditSelectionButton';

function _removeNewLines(title) {
  return title.replace(/(\r\n|\n|\r)/gm,' ');
}

function _determineKind(body) {
  const words = body.trim(' ').split(' ');
  const hasProtocol = /^(http|https):\/\/[^ "]+$/.test(words[0]);

  if (words.length === 1 && hasProtocol) {
    return 'link';
  }

  return 'self';
}

const debouncedDetermineKind = throttle(_determineKind, 1000);

class SubmitPage extends BasePage {
  static propTypes = {
    // apiOptions: React.PropTypes.object,
    kind: React.PropTypes.string,
    resubmit: React.PropTypes.bool,
    subredditName: React.PropTypes.string,
    thingId: React.PropTypes.string,
    type: React.PropTypes.string,
  };

  constructor(props) {
    super(props);

    Object.assign(this.state, {
      subredditName: props.subredditName || 'choose a subreddit',
      subredditSelectionOpen: false,
      title: props.postTitle || '',
      body: props.body || '',
      kind: props.type || 'self',
      sendReplies: true,
      captchaIden: '',
      captchaAnswer: '',
      requiresCaptcha: false,
      captchaCount: 0,
      error: {
        type: '',
        message: '',
        fields: [],
      },
    });

    this.updateCaptchaInfo = this.updateCaptchaInfo.bind(this);
    this.submit = this.submit.bind(this);
    this.goToAboutPage = this.goToAboutPage.bind(this);
    this.toggleSubSelect = this.toggleSubSelect.bind(this);
    this.changeSubreddit = this.changeSubreddit.bind(this);
    this.changeSendReplies = this.changeSendReplies.bind(this);
    this.handleTitleChange = this.handleChange.bind(this, 'title');
    this.handleBodyChange = this.handleChange.bind(this, 'body');
    this.close = this.close.bind(this);
    this.closeModal = this.closeModal.bind(this);
  }

  componentDidMount () {
    super.componentDidMount();

    if (!this.props.app.config.localStorageAvailable) {
      return;
    }

    const saved = global.localStorage.getItem('savedLinkContent');

    if (saved) {
      const parsed = JSON.parse(saved);
      this.setState(parsed);
      global.localStorage.clear();
    }
  }

  submit (e) {
    e.preventDefault();
    this.setState({
      error: {
        type: '',
        message: '',
        fields: [],
      },
    });

    let title = this.refs.title.value.trim();
    const sub = this.state.subredditName;
    const body = this.refs.body.value.trim();

    const kind = _determineKind(body);
    const errors = this._validateContent(sub, title);

    if (errors.length) {
      this.setState({
        error: {
          type: 'Incomplete:',
          message: 'all required fields must be filled out.',
          fields: errors,
        },
      });

      this.props.app.emit('post:error');
      return;
    }

    title = _removeNewLines(title);

    this._submitPost(this.props.thingId, body, title, kind);
  }

  _validateContent (sub, title) {
    const errors = [];
    if (sub === '' || sub === 'choose a subreddit') {
      errors.push('subreddit');
    }
    if (title === '') {
      errors.push('title');
    }
{'fraction_non_alphanumeric': 0.14857225684881867, 'fraction_numerical': 0.0020292796057399622, 'mean_word_length': 2.922683342808414, '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': '5870458', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2137, 'n_words': 416}