<commit_before>import React from 'react';
import { injectIntl } from 'react-intl';
import { connect } from 'react-redux';
import { uploadSourceListFromTemplate } from '../../../actions/sourceActions';
import { updateFeedback } from '../../../actions/appActions';
import composeHelpfulContainer from '../../common/HelpfulContainer';
import LoadingSpinner from '../../common/LoadingSpinner';
import CollectionUploadConfirmer from './form/CollectionUploadConfirmer';
import { DownloadButton } from '../../common/IconButton';

const localMessages = {
  uploadSourcesFromTemplateTitle: { id: 'collections.upload.title', defaultMessage: 'Sources From Template' },
  downloadEmpty: { id: 'collections.download.emptycsv', defaultMessage: 'Download a template in CSV format' },
  downloadFull: { id: 'collections.download.fullcsv', defaultMessage: 'Download current sources in CSV format' },
  helpTitle: { id: 'collection.upload.help.text.title',
    defaultMessage: 'Upload a Template of Sources',
  },
  helpText: { id: 'collection.upload.help.text',
    defaultMessage: 'Uploaded sources with a CSV template. You can download a template by clicking the Download button.',
  },
  feedback: { id: 'collection.upload.feedback', defaultMessage: 'This upload was successful' },
};
class CollectionUploadSourceContainer extends React.Component {

  downloadCsv = () => {
    const { myCollectionId } = this.props;
    let url = null;
    if (myCollectionId) {
      url = `/api/collections/${myCollectionId}/sources.csv?dType=1`;
    } else {
      url = '/api/template/sources.csv?dType=1';
    }
    window.location = url;
  }
  selectedCSV = () => {
    this.setState({ confirmTemplate: true });
  }

  confirmLoadCSV = () => {
    this.setState({ confirmTemplate: false });
  }

  uploadCSV = () => {
    const { uploadCSVFile } = this.props;
    const fd = this.textInput.files[0];
    uploadCSVFile(fd);
    this.selectedCSV();
  }
  render() {
    const { onConfirm, mysources, myCollectionId } = this.props;
    const { formatMessage } = this.props.intl;
    let confirmContent = null;
    if (mysources && mysources.length > 0 && this.state && this.state.confirmTemplate) {
      confirmContent = (
        <CollectionUploadConfirmer onConfirm={onConfirm} onCancel={this.confirmLoadCSV} onClickButton={this.confirmLoadCSV} />
      );
    } else if (this.state && this.state.confirmTemplate) {
      confirmContent = <LoadingSpinner />;
    }
    return (
      <div>
        <input type="file" onChange={this.uploadCSV} ref={(input) => { this.textInput = input; }} />
        <DownloadButton tooltip={mysources && mysources.length > 0 && myCollectionId != null ? formatMessage(localMessages.downloadFull) : formatMessage(localMessages.downloadEmpty)} onClick={this.downloadCsv} />
        { confirmContent }
      </div>
    );
  }
}

CollectionUploadSourceContainer.propTypes = {
  // from state<commit_msg>Disable choose button until Ok/Cancel is pressed<commit_after>import React from 'react';
import { injectIntl } from 'react-intl';
import { connect } from 'react-redux';
import { uploadSourceListFromTemplate } from '../../../actions/sourceActions';
import { updateFeedback } from '../../../actions/appActions';
import composeHelpfulContainer from '../../common/HelpfulContainer';
import LoadingSpinner from '../../common/LoadingSpinner';
import CollectionUploadConfirmer from './form/CollectionUploadConfirmer';
import { DownloadButton } from '../../common/IconButton';

const localMessages = {
  uploadSourcesFromTemplateTitle: { id: 'collections.upload.title', defaultMessage: 'Sources From Template' },
  downloadEmpty: { id: 'collections.download.emptycsv', defaultMessage: 'Download a template in CSV format' },
  downloadFull: { id: 'collections.download.fullcsv', defaultMessage: 'Download current sources in CSV format' },
  helpTitle: { id: 'collection.upload.help.text.title',
    defaultMessage: 'Upload a Template of Sources',
  },
  helpText: { id: 'collection.upload.help.text',
    defaultMessage: 'Uploaded sources with a CSV template. You can download a template by clicking the Download button.',
  },
  feedback: { id: 'collection.upload.feedback', defaultMessage: 'This upload was successful' },
};
class CollectionUploadSourceContainer extends React.Component {

  downloadCsv = () => {
    const { myCollectionId } = this.props;
    let url = null;
    if (myCollectionId) {
      url = `/api/collections/${myCollectionId}/sources.csv?dType=1`;
    } else {
      url = '/api/template/sources.csv?dType=1';
    }
    window.location = url;
  }
  selectedCSV = () => {
    this.setState({ confirmTemplate: true });
  }

  confirmLoadCSV = () => {
    this.setState({ confirmTemplate: false });
  }

  uploadCSV = () => {
    const { uploadCSVFile } = this.props;
    const fd = this.textInput.files[0];
    uploadCSVFile(fd);
    this.selectedCSV();
  }
  render() {
    const { onConfirm, mysources, myCollectionId } = this.props;
    const { formatMessage } = this.props.intl;
    let confirmContent = null;
    if (mysources && mysources.length > 0 && this.state && this.state.confirmTemplate) {
      confirmContent = (
        <CollectionUploadConfirmer onConfirm={onConfirm} onCancel={this.confirmLoadCSV} onClickButton={this.confirmLoadCSV} />
      );
    } else if (this.state && this.state.confirmTemplate) {
      confirmContent = <LoadingSpinner />;
    }
    return (
      <div>
        <input type="file" onChange={this.uploadCSV} ref={(input) => { this.textInput = input; }} disabled={this.state && this.state.confirmTemplate} />
        <DownloadButton tooltip={mysources && mysources.length > 0 && myCollectionId != null ? formatMessage(localMessages.downloadFull) : formatMessage(localMessages.downloadEmpty)} onClick={this.downloadCsv} />
        { confirmContent }
      </div>
    );
  }
}

CollectionUploadSourceContainer.propTypes = {
  // from state
{'fraction_non_alphanumeric': 0.13946788679884764, 'fraction_numerical': 0.0016946280291476022, 'mean_word_length': 3.7983739837398374, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5527876', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1677, 'n_words': 387}