@panneau/layout
Version:
Layout core for Panneau
110 lines (102 loc) • 4.64 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["urlGenerator", "resource", "link", "label", "items"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
/* eslint-disable react/no-array-index-key */
import React from 'react';
import PropTypes from 'prop-types'; // import classNames from 'classnames';
import get from 'lodash/get';
import isString from 'lodash/isString';
import { PropTypes as PanneauPropTypes, withDefinition, withUrlGenerator } from '@panneau/core';
import { defineMessages } from 'react-intl';
import NavbarItem from './NavbarItem';
var messages = defineMessages({
index: {
"id": "layouts.navbar.resource.index",
"defaultMessage": "View all {resource}"
},
create: {
"id": "layouts.navbar.resource.create",
"defaultMessage": "Create {resource}"
}
});
var propTypes = {
urlGenerator: PanneauPropTypes.urlGenerator,
resource: PanneauPropTypes.resource,
link: PropTypes.string,
label: PanneauPropTypes.label,
items: PropTypes.arrayOf(PropTypes.shape({
type: PropTypes.string,
label: PanneauPropTypes.label
}))
};
var defaultProps = {
urlGenerator: null,
resource: null,
link: null,
label: null,
items: [{
type: 'action',
action: 'index',
label: messages.viewAll
}, {
type: 'divider'
}, {
type: 'action',
action: 'create',
label: messages.addNew
}]
};
export var NavbarResource = function NavbarResource(_ref) {
var urlGenerator = _ref.urlGenerator,
resource = _ref.resource,
link = _ref.link,
label = _ref.label,
items = _ref.items,
props = _objectWithoutProperties(_ref, _excluded);
if (resource === null) {
return null;
}
var routeKeyPrefix = get(resource, 'routes', null) ? "resource.".concat(resource.id) : 'resource';
var finalLabel = label || get(resource, 'messages.names.plural', get(resource, 'name', null)) || resource.id;
var finalLink = link || (urlGenerator !== null ? urlGenerator.route("".concat(routeKeyPrefix, ".index"), {
resource: resource.id
}) : null);
var actions = ['index', 'create'];
var actionsLabels = actions.reduce(function (labels, action) {
var message = get(resource, "messages.navbar.".concat(action), messages[action]);
return _objectSpread(_objectSpread({}, labels), {}, _defineProperty({}, action, isString(message) ? message : _objectSpread(_objectSpread({}, message), {}, {
values: {
resource: get(resource, action === 'index' ? 'messages.names.a_plural' : 'messages.names.a', get(resource, 'name', resource.id))
}
})));
}, {});
var finalItems = (items || []).map(function (it) {
return get(it, 'type', 'link') === 'action' ? _objectSpread(_objectSpread({}, it), {}, {
label: it.label || get(actionsLabels, it.action, null),
link: it.link || (urlGenerator !== null ? urlGenerator.route("".concat(routeKeyPrefix, ".").concat(it.action), {
resource: resource.id
}) : null)
}) : it;
});
return /*#__PURE__*/React.createElement(NavbarItem, _extends({}, props, {
link: finalLink,
label: finalLabel,
items: finalItems
}));
};
NavbarResource.propTypes = propTypes;
NavbarResource.defaultProps = defaultProps;
var mapResourceFromDefinition = function mapResourceFromDefinition(definition, _ref2) {
var resource = _ref2.resource;
return {
resource: get(definition, 'resources', []).find(function (it) {
return it.id === resource;
}) || null
};
};
var WithDefinitionContainer = withDefinition(mapResourceFromDefinition)(NavbarResource);
var WithUrlGeneratorContainer = withUrlGenerator()(WithDefinitionContainer);
export default WithUrlGeneratorContainer;