Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/components / user / UserMenu / UserMenu.js
Size: Mime:
"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

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

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

var _classnames2 = _interopRequireDefault(require("classnames"));

var _commonMessages = require("@doodle/common-messages");

var _Icon = _interopRequireDefault(require("../../visuals/Icon"));

var _Menu = _interopRequireDefault(require("../../controls/Menu"));

var _UserAvatar = _interopRequireDefault(require("../UserAvatar"));

var _Button = require("../../controls/Button/Button");

var _Menu2 = require("../../controls/Menu/Menu");

var _translate = require("../../utils/translate");

var _helper = _interopRequireDefault(require("../../utils/helper"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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; }

var ArrowDown = function ArrowDown(props) {
  return /*#__PURE__*/_react["default"].createElement("svg", props, /*#__PURE__*/_react["default"].createElement("path", {
    d: "M7.41 7.84L12 12.42l4.59-4.58L18 9.25l-6 6-6-6z",
    fill: "currentColor"
  }));
};

ArrowDown.defaultProps = {
  xmlns: "http://www.w3.org/2000/svg",
  viewBox: "0 0 24 24"
};
// The menu items will always appear in the order defined here. Any custom items
// will be inserted in the middle, in-between the 2nd and 3rd item here.
var defaultItems = [{
  label: _commonMessages.messages.dashboard,
  to: '/dashboard'
}, {
  label: _commonMessages.messages.account,
  to: '/account'
}, {
  label: _commonMessages.messages.helpAndSupport,
  to: 'https://help.doodle.com'
}, {
  label: _commonMessages.messages.logout // action added in render method
  // url is added in navigateToUrl within handleClickLogout

}];
exports.defaultItems = defaultItems;
var adminMenuItem = [{
  label: _commonMessages.messages.adminSettings,
  to: '/admin'
}];
exports.adminMenuItem = adminMenuItem;

var UserMenu = /*#__PURE__*/function (_Component) {
  _inherits(UserMenu, _Component);

  var _super = _createSuper(UserMenu);

  function UserMenu(props) {
    var _this;

    _classCallCheck(this, UserMenu);

    _this = _super.call(this, props);
    _this.handleClickLogout = _this.handleClickLogout.bind(_assertThisInitialized(_this));
    return _this;
  }
  /**
   * Handles calling the onClickLogout and passes the default behaviour
   * into the onClickLogout callback as an argument.
   * To override or add behaviour, see onClickLogout prop.
   */


  _createClass(UserMenu, [{
    key: "handleClickLogout",
    value: function handleClickLogout() {
      var onClickLogout = this.props.onClickLogout;
      onClickLogout(function () {
        return (0, _helper["default"])('/logout');
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          name = _this$props.name,
          avatar = _this$props.avatar,
          customItems = _this$props.customItems,
          variant = _this$props.variant,
          dimension = _this$props.dimension,
          intl = _this$props.intl,
          isOrgAdmin = _this$props.isOrgAdmin;
      var translatedDefaultItems = (0, _translate.translateMenuItems)(defaultItems, intl);
      var translatedCustomItems = (0, _translate.translateMenuItems)(customItems, intl);
      var menuItems = [translatedDefaultItems[0], translatedDefaultItems[1]].concat(_toConsumableArray(translatedCustomItems), [translatedDefaultItems[2], _objectSpread(_objectSpread({}, translatedDefaultItems[3]), {}, {
        action: this.handleClickLogout
      })]);
      /**
       * If the current user is an organisation admin insert
       * a link to admin settings as the 3rd menu item
       */

      if (isOrgAdmin) {
        menuItems.splice(2, 0, (0, _translate.translateMenuItems)(adminMenuItem, intl)[0]);
      }

      var menuClassNames = (0, _classnames2["default"])('UserMenu', _defineProperty({}, "UserMenu--".concat(dimension), dimension));
      return /*#__PURE__*/_react["default"].createElement(_Menu["default"], {
        className: menuClassNames,
        variant: variant,
        horizontalAlign: "right",
        items: menuItems,
        dimension: dimension
      }, /*#__PURE__*/_react["default"].createElement(_UserAvatar["default"], {
        name: name,
        avatar: avatar
      }), /*#__PURE__*/_react["default"].createElement(_Icon["default"], {
        icon: ArrowDown
      }));
    }
  }]);

  return UserMenu;
}(_react.Component);

_defineProperty(UserMenu, "propTypes", {
  name: _propTypes["default"].string,
  avatar: _propTypes["default"].string,

  /** Is the logged in user an admin of an organisation
   */
  isOrgAdmin: _propTypes["default"].bool,

  /**
   * Callback executed when the user clicks on the "Logout" button.
   * It can be overridden to add custom functionality such as tracking.
   * You can execute the default button behaviour within onClickLogout by running
   * the function provided as first parameter. If your custom code is asynchronous,
   * run the default behaviour as callback of your asynchronous action. This guarantees
   * that your custom action completes before navigating to another page.
   * @param {Function} defaultBehaviour - Function to execute the default click behaviour
   */
  onClickLogout: _propTypes["default"].func,
  customItems: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    label: _propTypes["default"].shape({
      id: _propTypes["default"].string,
      defaultMessage: _propTypes["default"].string
    }).isRequired,
    to: _propTypes["default"].string,
    action: _propTypes["default"].func
  })),
  variant: _propTypes["default"].oneOf(_Button.buttonVariants),
  dimension: _propTypes["default"].oneOf(_Menu2.menuDimensions),
  intl: _propTypes["default"].object
});

_defineProperty(UserMenu, "defaultProps", {
  name: null,
  avatar: null,
  isOrgAdmin: false,
  onClickLogout: function onClickLogout(defaultBehaviour) {
    return defaultBehaviour();
  },
  customItems: [],
  variant: 'linkWhite',
  dimension: _Menu2.defaultMenuDimension,
  intl: null
});

var _default = UserMenu;
exports["default"] = _default;