<commit_before>/*
  Dumb component for add-on uploads to share between add-on submission and
  add-on version submission.
*/
import classnames from 'classnames';
import React from 'react';
import FileReaderInput from 'react-file-reader-input';

import ProgressBar from './progressBar';
import ValidationError from './validationError';


export default class AddonUpload extends React.Component {
  /*
    slug (string) -- only used in new version submissions.
  */
  static contextTypes = {
    router: React.PropTypes.object
  };

  static propTypes = {
    isSubmitting: React.PropTypes.bool,
    messageChange: React.PropTypes.func.isRequired,
    slug: React.PropTypes.string,
    submit: React.PropTypes.func.isRequired,
    uploadLoaded: React.PropTypes.number,
    uploadTotal: React.PropTypes.number,
    validationError: React.PropTypes.object.isRequired
  };

  constructor(props) {
    super(props);

    this.accept = [
      'application/octet-stream',
      'application/zip',
      'application/x-zip',
      'application/x-zip-compressed'
    ].join(',');

    this.state = {
      fileData: null,
      fileSize: null,
      fileName: null,
      message: null,
    };
  }

  handleChange = (e, results) => {
    const [result, file] = results[0];
    this.setState({
      fileData: result.target.result,
      fileSize: result.loaded,
      fileName: file.name,
    });
  }

  handleMessageChange = e => {
    this.props.messageChange(e.target.value);
  }

  handleSubmit = e => {
    e.preventDefault();
    this.props.submit(this.state.fileData, this.props.slug);
  }

  render() {
    const KB_DIVISOR = 1024;
    const MB_DIVISOR = 1048576;

    let divisor = MB_DIVISOR;
    let unit = 'MB'
    if (this.state.fileSize < 1000 * KB_DIVISOR) {
      // Show KBs if less than 1MB.
      divisor = KB_DIVISOR;
      unit = 'KB';
    }
    const fileSize = toFixedDown(this.state.fileSize / divisor, 2);

    const hasValidationError = !!this.props.validationError;
    const showProgressBar = (this.props.uploadLoaded &&
                             this.props.uploadLoaded < this.props.uploadTotal);

    return (
      <div className="addon-upload">
        <form className="form-inline" onSubmit={this.handleSubmit}>

          <div className={classnames({
            'field': true,
            'field--error': hasValidationError,
          })}>
            <label htmlFor="submission-addon--zip">Add-on:</label>
            <FileReaderInput as="buffer" accept=".zip"
                             id="submission-addon--zip"
                             onChange={this.handleChange}>
              <div className="form-inline--file-input"
                   data-file-input--has-data={!!this.state.fileName}>
                {this.state.fileName ?
                 `${this.state.fileName} (${fileSize}${unit})` :
                 'Select file...'}
              </div>
            </FileReaderInput>
            <p className="form-msg form-msg--help">
              Must be packaged as a .zip file. All metadata&mdash;such as
              add-on name and description are configured via the add-on&apos;s
              manifest file. Learn more about <a
              href="https://developer.mozilla.org/docs/Mozilla/Firefox_OS/Add-ons#The_anatomy_of_a_Firefox_OS_add-on">
                packaging Firefox OS add-ons
              </a>.
            </p>
            {hasValidationError &&<commit_msg>{} is not falsy.<commit_after>/*
  Dumb component for add-on uploads to share between add-on submission and
  add-on version submission.
*/
import classnames from 'classnames';
import React from 'react';
import FileReaderInput from 'react-file-reader-input';

import ProgressBar from './progressBar';
import ValidationError from './validationError';


export default class AddonUpload extends React.Component {
  /*
    slug (string) -- only used in new version submissions.
  */
  static contextTypes = {
    router: React.PropTypes.object
  };

  static propTypes = {
    isSubmitting: React.PropTypes.bool,
    messageChange: React.PropTypes.func.isRequired,
    slug: React.PropTypes.string,
    submit: React.PropTypes.func.isRequired,
    uploadLoaded: React.PropTypes.number,
    uploadTotal: React.PropTypes.number,
    validationError: React.PropTypes.object.isRequired
  };

  constructor(props) {
    super(props);

    this.accept = [
      'application/octet-stream',
      'application/zip',
      'application/x-zip',
      'application/x-zip-compressed'
    ].join(',');

    this.state = {
      fileData: null,
      fileSize: null,
      fileName: null,
      message: null,
    };
  }

  handleChange = (e, results) => {
    const [result, file] = results[0];
    this.setState({
      fileData: result.target.result,
      fileSize: result.loaded,
      fileName: file.name,
    });
  }

  handleMessageChange = e => {
    this.props.messageChange(e.target.value);
  }

  handleSubmit = e => {
    e.preventDefault();
    this.props.submit(this.state.fileData, this.props.slug);
  }

  render() {
    const KB_DIVISOR = 1024;
    const MB_DIVISOR = 1048576;

    let divisor = MB_DIVISOR;
    let unit = 'MB'
    if (this.state.fileSize < 1000 * KB_DIVISOR) {
      // Show KBs if less than 1MB.
      divisor = KB_DIVISOR;
      unit = 'KB';
    }
    const fileSize = toFixedDown(this.state.fileSize / divisor, 2);

    const hasValidationError = !!this.props.validationError.key;
    const showProgressBar = (this.props.uploadLoaded &&
                             this.props.uploadLoaded < this.props.uploadTotal);

    return (
      <div className="addon-upload">
        <form className="form-inline" onSubmit={this.handleSubmit}>

          <div className={classnames({
            'field': true,
            'field--error': hasValidationError,
          })}>
            <label htmlFor="submission-addon--zip">Add-on:</label>
            <FileReaderInput as="buffer" accept=".zip"
                             id="submission-addon--zip"
                             onChange={this.handleChange}>
              <div className="form-inline--file-input"
                   data-file-input--has-data={!!this.state.fileName}>
                {this.state.fileName ?
                 `${this.state.fileName} (${fileSize}${unit})` :
                 'Select file...'}
              </div>
            </FileReaderInput>
            <p className="form-msg form-msg--help">
              Must be packaged as a .zip file. All metadata&mdash;such as
              add-on name and description are configured via the add-on&apos;s
              manifest file. Learn more about <a
              href="https://developer.mozilla.org/docs/Mozilla/Firefox_OS/Add-ons#The_anatomy_of_a_Firefox_OS_add-on">
                packaging Firefox OS add-ons
              </a>.
            </p>
            {hasValidationError &&
{'fraction_non_alphanumeric': 0.12312180889861415, 'fraction_numerical': 0.00525164113785558, 'mean_word_length': 2.2278719397363465, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 2, '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': '6425367', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1994, 'n_words': 429}