'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Uploader = undefined;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDropzone = require('react-dropzone');

var _reactDropzone2 = _interopRequireDefault(_reactDropzone);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Uploader = exports.Uploader = function (_React$Component) {
  _inherits(Uploader, _React$Component);

  function Uploader(props) {
    _classCallCheck(this, Uploader);

    var _this = _possibleConstructorReturn(this, (Uploader.__proto__ || Object.getPrototypeOf(Uploader)).call(this, props));

    _this.handleBase64Return = function (file) {
      var reader = new FileReader();
      reader.onload = function (event) {
        _this.props.onSuccess({
          file: file,
          preview: event.target.result,
          name: file.name
        });
      };
      reader.readAsDataURL(file);
    };

    _this.state = {
      fileName: _this.props.fileName
    };

    _this.handleChangeAcceptedFiles = _this.handleChangeAcceptedFiles.bind(_this);
    _this.handleChangeRejectedFiles = _this.handleChangeRejectedFiles.bind(_this);
    _this.handleClick = _this.handleClick.bind(_this);
    _this.handleCancel = _this.handleCancel.bind(_this);
    return _this;
  }

  _createClass(Uploader, [{
    key: 'handleChangeAcceptedFiles',
    value: function handleChangeAcceptedFiles(acceptedFiles) {
      var file = acceptedFiles[0];

      this.setState({
        fileName: file.name
      });

      if (this.props.base64) {
        this.handleBase64Return(file);
      } else {
        this.props.onSuccess({
          file: file,
          preview: file.preview,
          name: file.name
        });
      }

      this.props.onError(false);
    }
  }, {
    key: 'handleChangeRejectedFiles',
    value: function handleChangeRejectedFiles() {
      this.props.onError(true);

      this.handleCancel();
    }
  }, {
    key: 'handleClick',
    value: function handleClick() {
      this.dropzoneRef.open();
    }
  }, {
    key: 'handleCancel',
    value: function handleCancel() {
      this.setState({
        fileName: null
      });

      this.props.onReset();
    }
  }, {
    key: 'renderTheme',
    value: function renderTheme() {
      if (!this.props.theme) return;

      var Theme = this.props.theme;

      return _react2.default.createElement(Theme, {
        buttonLabel: this.props.buttonLabel,
        fileName: this.state.fileName,
        onClick: this.handleClick,
        onCancel: this.handleCancel,
        disabled: this.props.disabled,
        deletable: this.props.deletable
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      return _react2.default.createElement(
        _reactDropzone2.default,
        {
          ref: function ref(node) {
            _this2.dropzoneRef = node;
          },
          name: this.props.name,
          accept: this.props.acceptedFiles,
          maxSize: this.props.maxSize,
          onDropAccepted: this.handleChangeAcceptedFiles,
          onDropRejected: this.handleChangeRejectedFiles,
          disableClick: true,
          multiple: false,
          style: {}
        },
        this.renderTheme()
      );
    }
  }]);

  return Uploader;
}(_react2.default.Component);

Uploader.defaultProps = {
  name: null,
  acceptedFiles: 'image/*',
  maxSize: null,
  fileName: null,
  theme: null,
  buttonLabel: 'Choose a file',
  onSuccess: function onSuccess() {},
  onError: function onError() {},
  onReset: function onReset() {},
  disabled: false,
  base64: false
};