"use strict";

require("core-js/modules/web.dom.iterable");

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

var _auth0React = require("@auth0/auth0-react");

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

var _reactRedux = require("react-redux");

var userActions = _interopRequireWildcard(require("../../actions/user"));

var _constants = require("../../util/constants");

var _awaitingScreen = _interopRequireDefault(require("./awaiting-screen"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * This higher-order component ensures that state.user is loaded
 * in the redux store for any wrapped component that may need it.
 * The requireLoggedInUser argument handles the two use cases for this component:
 * - Some components (e.g. those processing a user account) require a logged in user to be available,
 *   and without it they cannot function.
     For such components, set requireLoggedInUser to true.
 *   An awaiting screen will be displayed while state.user data are being fetched,
 *   and the wrapped component will be shown upon availability of state.user.
 * - Other components (e.g. landing pages) don't require a logged in user to be available to function
 *   but will display extra functionality if so.
 *   For such components, omit requireLoggedInUser parameter (or set to false).
 *   The wrapped component is shown immediately, and no awaiting screen is displayed while state.user is being retrieved.
 * @param {React.Component} WrappedComponent The component to be wrapped to that uses state.user from the redux store.
 * @param {boolean} requireLoggedInUser Whether the wrapped component requires state.user to properly function.
 */
function withLoggedInUserSupport(WrappedComponent, requireLoggedInUser) {
  return props => /*#__PURE__*/_react.default.createElement(UserLoaderScreenWithAuth, {
    passedProps: props,
    requireLoggedInUser: requireLoggedInUser,
    WrappedComponent: WrappedComponent
  });
}
/**
 * This component ensures that values under state.user are set when a user is logged in.
 * If needed by the child WrappedComponent, this component displays a wait screen while state.user values are being fetched.
 * Upon completion (or if no user is logged in or if auth is disabled), it renders the
 * specified WrappedComponent with the passed props and the auth0 props if available.
 */


class UserLoaderScreen extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "loggedInUserIsUnfetched", () => {
      const {
        auth0,
        loggedInUser
      } = this.props;
      return auth0 && auth0.isAuthenticated && !loggedInUser;
    });

    _defineProperty(this, "acccessTokenIsUnfetched", () => {
      const {
        accessToken,
        auth0
      } = this.props;
      return auth0 && auth0.isAuthenticated && !accessToken;
    });
  }

  componentDidUpdate() {
    const {
      auth0,
      fetchAuth0Token,
      fetchOrInitializeUser
    } = this.props;

    if (this.acccessTokenIsUnfetched()) {
      fetchAuth0Token(auth0);
    } else if (this.loggedInUserIsUnfetched()) {
      fetchOrInitializeUser(auth0.user);
    }
  }

  render() {
    const {
      auth0,
      passedProps,
      requireLoggedInUser,
      WrappedComponent
    } = this.props;

    if (requireLoggedInUser && this.loggedInUserIsUnfetched()) {
      // If a logged-in user is required, then
      // display a hint while the logged-in user data is being fetched (from componentDidMount).
      // Don't display this if loggedInUser is not required or is already available.
      // TODO: Improve this screen.
      return /*#__PURE__*/_react.default.createElement(_awaitingScreen.default, null);
    } // Forward the auth0 object to the wrapped component.
    // (if there is no user, auth0 will be null and not be forwarded.)


    return /*#__PURE__*/_react.default.createElement(WrappedComponent, _extends({
      auth0: auth0
    }, passedProps));
  }

} // connect to the redux store


const mapStateToProps = (state, ownProps) => {
  const {
    accessToken,
    loggedInUser
  } = state.user;
  return {
    accessToken,
    loggedInUser
  };
};

const mapDispatchToProps = {
  fetchAuth0Token: userActions.fetchAuth0Token,
  fetchOrInitializeUser: userActions.fetchOrInitializeUser
};
const UserLoaderScreenWithAuth = (0, _auth0React.withAuth0)((0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(UserLoaderScreen), {
  audience: _constants.AUTH0_AUDIENCE,
  scope: _constants.AUTH0_SCOPE
});
module.exports = exports.default;

//# sourceMappingURL=with-logged-in-user-support.js