<commit_before>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withStyles, createStyleSheet } from 'material-ui/styles';
import Typography from 'material-ui/Typography';
import Button from 'material-ui/Button';
import IconButton from 'material-ui/IconButton';
import Close from 'material-ui-icons/Close';
import FileUpload from 'material-ui-icons/FileUpload';
import Attachment from 'material-ui-icons/Attachment';

const messages = {
  upload: 'upload file',
};

const styleSheet = createStyleSheet('FileUploadButton', theme => ({
  root: {
    width: 0.1,
    height: 0.1,
    opacity: 0,
    overflow: 'hidden',
    position: 'absolute',
    zIndex: -1,
  },
  iconLabel: {
    display: 'flex',
    alignItems: 'center',
    minWidth: 72,
  },
  wrapContent: {
    display: 'inline-block',
  },
  icon: {
    marginRight: theme.spacing.unit,
  },
  removeIcon: {
    fill: theme.palette.accent[300],
  },
}));

class FileUploadButton extends Component {
  constructor(props) {
    super(props);

    this.handleChange = this.handleChange.bind(this);
    this.handleRemove = this.handleRemove.bind(this);
  }

  handleChange() {
    const { fileSelected, fileAdded } = this.props;
    const [file] = this.refInput.files;

    if (file) {
      fileSelected(file);
    } else if (!fileAdded) {
      fileSelected(null);
    }
  }

  handleRemove() {
    const { fileSelected } = this.props;

    this.refInput.value = null;
    fileSelected(null);
  }

  render() {
    const { classes, fieldName, deleteDisabled, fileAdded } = this.props;

    return (
      <div>
        <input
          onChange={this.handleChange}
          className={classes.root}
          name={`${fieldName}-input`}
          id={`${fieldName}-input`}
          ref={(field) => { this.refInput = field; }}
          type="file"
        />

        {!fileAdded ?
          <Button dense color="accent" >
            <label className={classes.iconLabel} htmlFor={`${fieldName}-input`}>
              <FileUpload className={classes.icon} />
              {messages.upload}
            </label>
          </Button>
          :
          <div className={classes.wrapContent}>
            <Typography type="subheading" className={classes.iconLabel} gutterBottom >
              <Attachment className={classes.icon} />
              {fileAdded}
              <IconButton disabled={deleteDisabled} onClick={() => this.handleRemove()}>
                <Close className={classes.removeIcon} />
              </IconButton>
            </Typography>
          </div>}
      </div>
    );
  }
}

FileUploadButton.propTypes = {
  classes: PropTypes.object.isRequired,
  fieldName: PropTypes.string.isRequired,
  fileSelected: PropTypes.func.isRequired,
  fileAdded: PropTypes.object,
  deleteDisabled: PropTypes.bool,
};

FileUploadButton.defaultProps = {
  deleteDisabled: false,
};

export default withStyles(styleSheet)(FileUploadButton);

<commit_msg>Upload button logic fix<commit_after>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withStyles, createStyleSheet } from 'material-ui/styles';
import Typography from 'material-ui/Typography';
import Button from 'material-ui/Button';
import IconButton from 'material-ui/IconButton';
import Close from 'material-ui-icons/Close';
import FileUpload from 'material-ui-icons/FileUpload';
import Attachment from 'material-ui-icons/Attachment';

const messages = {
  upload: 'upload file',
};

const styleSheet = createStyleSheet('FileUploadButton', theme => ({
  root: {
    width: 0.1,
    height: 0.1,
    opacity: 0,
    overflow: 'hidden',
    position: 'absolute',
    zIndex: -1,
  },
  iconLabel: {
    display: 'flex',
    alignItems: 'center',
    minWidth: 72,
  },
  wrapContent: {
    display: 'inline-block',
  },
  icon: {
    marginRight: theme.spacing.unit,
  },
  removeIcon: {
    fill: theme.palette.accent[300],
  },
}));

class FileUploadButton extends Component {
  constructor(props) {
    super(props);

    this.handleChange = this.handleChange.bind(this);
    this.handleRemove = this.handleRemove.bind(this);
  }

  handleChange() {
    const { fileSelected, fileAdded } = this.props;
    const [file] = this.refInput.files;

    if (file) {
      fileSelected(file);
    } else if (!fileAdded) {
      fileSelected(null);
    }

    this.refInput.value = null;
  }

  handleRemove() {
    const { fileSelected } = this.props;

    fileSelected(null);
  }

  render() {
    const { classes, fieldName, deleteDisabled, fileAdded } = this.props;

    return (
      <div>
        <input
          onChange={this.handleChange}
          className={classes.root}
          name={`${fieldName}-input`}
          id={`${fieldName}-input`}
          ref={(field) => { this.refInput = field; }}
          type="file"
        />

        {!fileAdded ?
          <Button dense color="accent" >
            <label className={classes.iconLabel} htmlFor={`${fieldName}-input`}>
              <FileUpload className={classes.icon} />
              {messages.upload}
            </label>
          </Button>
          :
          <div className={classes.wrapContent}>
            <Typography type="subheading" className={classes.iconLabel} gutterBottom >
              <Attachment className={classes.icon} />
              {fileAdded}
              <IconButton disabled={deleteDisabled} onClick={() => this.handleRemove()}>
                <Close className={classes.removeIcon} />
              </IconButton>
            </Typography>
          </div>}
      </div>
    );
  }
}

FileUploadButton.propTypes = {
  classes: PropTypes.object.isRequired,
  fieldName: PropTypes.string.isRequired,
  fileSelected: PropTypes.func.isRequired,
  fileAdded: PropTypes.object,
  deleteDisabled: PropTypes.bool,
};

FileUploadButton.defaultProps = {
  deleteDisabled: false,
};

export default withStyles(styleSheet)(FileUploadButton);


{'fraction_non_alphanumeric': 0.13632529104099883, 'fraction_numerical': 0.0037118272313143245, 'mean_word_length': 2.735349716446125, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, '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': '5393409', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1744, 'n_words': 351}