'use strict';

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

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 _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

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; } /* global URL */

var propTypes = {
  location: _propTypes2.default.object,
  match: _propTypes2.default.object,
  readContent: _propTypes2.default.func.isRequired,
  renderError: _propTypes2.default.func,
  renderLoading: _propTypes2.default.func
};

var ContentViewerPage = function (_Component) {
  _inherits(ContentViewerPage, _Component);

  function ContentViewerPage() {
    _classCallCheck(this, ContentViewerPage);

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

    _this.state = {
      blobUrl: null
    };
    return _this;
  }

  _createClass(ContentViewerPage, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.readContent();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var contentPath = (0, _get2.default)(this.props.match, ['params', 'contentPath']);
      var prevContentPath = (0, _get2.default)(prevProps.match, ['params', 'contentPath']);

      if (contentPath !== prevContentPath) {
        this.reset();
        this.readContent();
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.reset();
      this.isUnmounted = true;
    }
  }, {
    key: 'reset',
    value: function reset() {
      var blobUrl = this.state.blobUrl;


      if (blobUrl) {
        URL.revokeObjectURL(blobUrl);

        this.setState({
          blobUrl: null
        });
      }
    }
  }, {
    key: 'readContent',
    value: function readContent() {
      var _this2 = this;

      var _props = this.props,
          location = _props.location,
          match = _props.match,
          readContent = _props.readContent;


      readContent(location, match).then(function (response) {
        if (response && response.status === 200 && response.data && !_this2.isUnmounted) {
          _this2.setState({
            blobUrl: URL.createObjectURL(response.data)
          });
        }
      }).catch(function (error) {
        _this2.setState({
          error: error
        });
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          renderError = _props2.renderError,
          renderLoading = _props2.renderLoading;
      var _state = this.state,
          blobUrl = _state.blobUrl,
          error = _state.error;


      if (error) {
        return renderError ? renderError(error) : null;
      }

      if (!blobUrl) {
        return renderLoading ? renderLoading() : null;
      }

      var style = {
        position: 'fixed',
        left: '0',
        top: '0',
        width: '100%',
        height: '100%',
        border: 'none'
      };

      return _react2.default.createElement('iframe', { src: blobUrl, style: style });
    }
  }]);

  return ContentViewerPage;
}(_react.Component);

exports.default = ContentViewerPage;


ContentViewerPage.propTypes = propTypes;