"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactIntl = require("react-intl");

var _immutable = _interopRequireDefault(require("immutable"));

var _FooterContainer = _interopRequireDefault(require("../../containers/sections/FooterContainer"));

var _Header = _interopRequireDefault(require("../sections/Header"));

var _LoginModal = _interopRequireDefault(require("../login/LoginModal"));

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

/* global window */
const propTypes = {
  decorated: _propTypes.default.bool,
  history: _propTypes.default.object,
  openModalName: _propTypes.default.string,
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  screenName: _propTypes.default.string,
  username: _propTypes.default.string.isRequired,
  children: _propTypes.default.node,
  closeModal: _propTypes.default.func,
  resetLogin: _propTypes.default.func
};
const defaultProps = {
  decorated: true
};
const contextTypes = {
  config: _propTypes.default.object,
  intl: _reactIntl.intlShape
};

class ProtectedPage extends _react.Component {
  constructor() {
    super();
    this.handleLoginSuccess = this.handleLoginSuccess.bind(this);
  }

  handleLoginSuccess() {
    const {
      closeModal,
      resetLogin
    } = this.props;
    window.setTimeout(() => {
      if (resetLogin) {
        resetLogin();
      }

      if (closeModal) {
        closeModal();
      }
    }, 0);
  }

  render() {
    const {
      decorated,
      history,
      openModalName,
      perms,
      screenName,
      username,
      children,
      closeModal
    } = this.props;
    const {
      config,
      intl
    } = this.context;
    const header = decorated ? _react.default.createElement(_Header.default, {
      history: history,
      perms: perms,
      screenName: screenName || username
    }) : null;
    const footer = decorated ? _react.default.createElement(_FooterContainer.default, {
      config: config,
      intl: intl
    }) : null;
    return _react.default.createElement("div", null, header, children, footer, _react.default.createElement(_LoginModal.default, {
      isOpen: openModalName === _LoginModal.default.modalName,
      onCloseButtonClick: closeModal,
      onSuccess: this.handleLoginSuccess
    }));
  }

}

exports.default = ProtectedPage;
ProtectedPage.propTypes = propTypes;
ProtectedPage.defaultProps = defaultProps;
ProtectedPage.contextTypes = contextTypes;