client/entity-editor/series-section/series-section.js

"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.find.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.object.values.js");
require("core-js/modules/es.array.sort.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.filter.js");
var _set2 = _interopRequireDefault(require("lodash/set"));
var _forEach2 = _interopRequireDefault(require("lodash/forEach"));
var _upperFirst2 = _interopRequireDefault(require("lodash/upperFirst"));
var _get2 = _interopRequireDefault(require("lodash/get"));
var React = _interopRequireWildcard(require("react"));
var _actions = require("./actions");
var _reactBootstrap = require("react-bootstrap");
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _reactSelect = _interopRequireDefault(require("react-select"));
var _seriesEditor = _interopRequireDefault(require("./series-editor"));
var _helpers = require("../helpers");
var _reactRedux = require("react-redux");
var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");
var _utils = require("../../../common/helpers/utils");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
/*
 * Copyright (C) 2021  Akash Gupta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * Container component. The SeriesSection component contains input fields
 * specific to the series entity. The intention is that this component is
 * rendered as a modular section within the entity editor.
 *
 * @param {Object} props - The properties passed to the component.
 * @param {Object} props.entity - The entity being edited.
 * @param {string} props.entityName - The name of the entity being edited.
 * @param {string} props.entityType - The type of the entity being edited.
 * @param {Function} props.onEdit - The function to call when the user clicks
 * 		  on the edit button.
 * @param {Function} props.onRemove - The function to call when the user clicks
 * 		  on the remove button.
 * @param {Function} props.onSeriesItemAdd - The function to call when the user clicks
 * 		  on the add button.
 * @param {number} props.orderTypeValue - The ID of the ordering type currently selected for
 *        the series.
 * @param {Array} props.seriesOrderingTypes - The list of possible ordering
 * 		  types for a series.
 * @param {Object} props.seriesItems - The list of series items currently in the series.
 * @param {Object} props.relationshipTypes - The list of possible relationship types.
 * @param {string} props.seriesTypeValue - The value of the entity type currently selected for
 *        the series.
 * @param {Function} props.onOrderTypeChange - A function to be called when
 *        a different ordering type is selected.
 * @param {Function} props.onSeriesTypeChange - A function to be called when
 *        a different series type is selected.
 * @returns {ReactElement} React element containing the rendered
 *        SeriesSection.
 */
function SeriesSection(_ref) {
  var entity = _ref.entity,
    entityName = _ref.entityName,
    entityType = _ref.entityType,
    hideItemSelect = _ref.hideItemSelect,
    onEdit = _ref.onEdit,
    onOrderTypeChange = _ref.onOrderTypeChange,
    onRemove = _ref.onRemove,
    onSeriesItemAdd = _ref.onSeriesItemAdd,
    onSeriesTypeChange = _ref.onSeriesTypeChange,
    onSortSeriesItems = _ref.onSortSeriesItems,
    orderTypeValue = _ref.orderTypeValue,
    relationshipTypes = _ref.relationshipTypes,
    seriesItems = _ref.seriesItems,
    seriesOrderingTypes = _ref.seriesOrderingTypes,
    isUnifiedForm = _ref.isUnifiedForm,
    seriesTypeValue = _ref.seriesTypeValue;
  var baseEntity = {
    bbid: (0, _get2.default)(entity, 'bbid'),
    defaultAlias: {
      name: entityName
    },
    disambiguation: (0, _get2.default)(entity, ['disambiguation', 'comment']),
    type: (0, _upperFirst2.default)(entityType)
  };
  var seriesItemsObject = seriesItems.toJS();

  /* If one of the relationships is to a new entity (in creation),
  update that new entity's name to replace "New Entity" */
  if (typeof baseEntity.bbid === 'undefined') {
    (0, _forEach2.default)(seriesItemsObject, function (relationship) {
      var sourceEntity = relationship.sourceEntity,
        targetEntity = relationship.targetEntity;
      var defaultAliasPath = ['defaultAlias', 'name'];
      var newEntity = [sourceEntity, targetEntity].find(function (_ref2) {
        var bbid = _ref2.bbid;
        return bbid === baseEntity.bbid;
      });
      var newRelationshipName = newEntity && (0, _get2.default)(newEntity, defaultAliasPath);
      var baseEntityName = (0, _get2.default)(baseEntity, defaultAliasPath);
      if (newRelationshipName !== baseEntityName) {
        (0, _set2.default)(newEntity, defaultAliasPath, baseEntityName);
      }
    });
  }
  var seriesItemsArray = Object.values(seriesItemsObject);
  (0, _helpers.attachAttribToRelForDisplay)(seriesItemsArray);
  // Sort the series items according to the ordering type before displaying
  if (orderTypeValue === 2) {
    seriesItemsArray.sort((0, _utils.sortRelationshipOrdinal)('position'));
  } else {
    seriesItemsArray.sort((0, _utils.sortRelationshipOrdinal)('number'));
  }
  var seriesOrderingTypesForDisplay = seriesOrderingTypes.map(function (type) {
    return {
      label: type.label,
      value: type.id
    };
  });
  var orderTypeOption = seriesOrderingTypesForDisplay.filter(function (el) {
    return el.value === orderTypeValue;
  });
  var seriesTypesForDisplay = ['Author', 'Work', 'Edition', 'EditionGroup', 'Publisher'].map(function (type) {
    return {
      label: type,
      value: type
    };
  });
  var seriesTypeOption = seriesTypesForDisplay.filter(function (el) {
    return el.value === seriesTypeValue;
  });
  var orderingTooltip = /*#__PURE__*/React.createElement(_reactBootstrap.Tooltip, null, "Ordering Type of the Series Entity");
  var seriesTypeTooltip = /*#__PURE__*/React.createElement(_reactBootstrap.Tooltip, null, "Entity Type of the Series");
  var heading = /*#__PURE__*/React.createElement("h2", null, "What else do you know about the Series?");
  var lgCol = {
    offset: 3,
    span: 6
  };
  if (isUnifiedForm) {
    lgCol.offset = 0;
  }
  return /*#__PURE__*/React.createElement("div", null, !isUnifiedForm && heading, !hideItemSelect && /*#__PURE__*/React.createElement("p", {
    className: "text-muted"
  }, "All fields are mandatory \u2014 select the option from dropdown"), /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: lgCol
  }, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, "Ordering Type", /*#__PURE__*/React.createElement(_reactBootstrap.OverlayTrigger, {
    delay: 50,
    overlay: orderingTooltip
  }, /*#__PURE__*/React.createElement(_reactFontawesome.FontAwesomeIcon, {
    className: "margin-left-0-5",
    icon: _freeSolidSvgIcons.faQuestionCircle
  }))), /*#__PURE__*/React.createElement(_reactSelect.default, {
    backspaceRemovesValue: false,
    classNamePrefix: "react-select",
    instanceId: "seriesOrderingType",
    isClearable: false,
    options: seriesOrderingTypesForDisplay,
    value: orderTypeOption,
    onChange: onOrderTypeChange
  })), !isUnifiedForm && /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, "Series Type", /*#__PURE__*/React.createElement(_reactBootstrap.OverlayTrigger, {
    delay: 50,
    overlay: seriesTypeTooltip
  }, /*#__PURE__*/React.createElement(_reactFontawesome.FontAwesomeIcon, {
    className: "margin-left-0-5",
    icon: _freeSolidSvgIcons.faQuestionCircle
  }))), /*#__PURE__*/React.createElement(_reactSelect.default, {
    backspaceRemovesValue: false,
    classNamePrefix: "react-select",
    instanceId: "SeriesType",
    isClearable: false,
    isDisabled: Boolean(seriesItemsArray.length),
    options: seriesTypesForDisplay,
    value: seriesTypeOption,
    onChange: onSeriesTypeChange
  })))), /*#__PURE__*/React.createElement(_seriesEditor.default, {
    baseEntity: baseEntity,
    hideItemSelect: hideItemSelect,
    isUnifiedForm: isUnifiedForm,
    orderType: orderTypeValue,
    relationshipTypes: relationshipTypes,
    seriesItemsArray: seriesItemsArray,
    seriesType: seriesTypeValue,
    onAdd: onSeriesItemAdd,
    onEdit: onEdit,
    onRemove: onRemove,
    onSort: onSortSeriesItems
  }));
}
SeriesSection.displayName = 'SeriesSection';
SeriesSection.defaultProps = {
  hideItemSelect: false,
  isUnifiedForm: false
};
function mapStateToProps(rootState, _ref3) {
  var isUnifiedForm = _ref3.isUnifiedForm;
  var state = rootState.get('seriesSection');
  var seriesTypeValue = state.get('seriesType');
  var entityPath = isUnifiedForm ? ['Series', 's0', 'text'] : ['nameSection', 'name'];
  return {
    entityName: rootState.getIn(entityPath),
    orderTypeValue: state.get('orderType'),
    seriesItems: state.get('seriesItems'),
    seriesTypeValue: seriesTypeValue
  };
}
function mapDispatchToProps(dispatch) {
  return {
    onEdit: function onEdit(data, rowID) {
      return dispatch((0, _actions.editSeriesItem)(data, rowID));
    },
    onOrderTypeChange: function onOrderTypeChange(value) {
      dispatch((0, _actions.updateOrderType)(value && value.value));
      if (value && value.value === 1) {
        dispatch((0, _actions.sortSeriesItems)(null, null));
      }
    },
    onRemove: function onRemove(rowID) {
      return dispatch((0, _actions.removeSeriesItem)(rowID));
    },
    onSeriesItemAdd: function onSeriesItemAdd(data) {
      return dispatch((0, _actions.addSeriesItem)(data));
    },
    onSeriesTypeChange: function onSeriesTypeChange(value) {
      return dispatch((0, _actions.updateSeriesType)(value && value.value));
    },
    onSortSeriesItems: function onSortSeriesItems(_ref4) {
      var oldIndex = _ref4.oldIndex,
        newIndex = _ref4.newIndex;
      return dispatch((0, _actions.sortSeriesItems)(oldIndex, newIndex));
    }
  };
}
var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(SeriesSection);
exports.default = _default;
//# sourceMappingURL=series-section.js.map