client/entity-editor/series-section/series-editor.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.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.array.map.js");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _get2 = _interopRequireDefault(require("lodash/get"));
var _find3 = _interopRequireDefault(require("lodash/find"));
var _reactBootstrap = require("react-bootstrap");
var _react = _interopRequireWildcard(require("react"));
var _reactSortableHoc = require("react-sortable-hoc");
var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");
var _entitySearchFieldOption = _interopRequireDefault(require("../common/entity-search-field-option"));
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _relationship = _interopRequireDefault(require("../relationship-editor/relationship"));
var _relationshipEditor = require("../relationship-editor/relationship-editor");
var _this = void 0;
/*
 * 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.
 */
/* eslint-disable react/jsx-no-bind, @typescript-eslint/no-invalid-this */
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; }
/**
 * This is a simple React component. No Redux connection. Renders the
 * Entity Search Field and displays the list of series items. The Entity Search
 * Field is a simple search box that allows the user to search for entities, and the Add
 * Button allows the user to add the entity. The remove button allows to
 * remove a entity from the list.
 *
 * @param {Object} props - The properties passed to the component.
 * @param {Entity} props.baseEntity - The entity currently being edited in the
 *        entity editor
 * @param {Array<RelationshipType>} props.relationshipTypes - All the possible
 *        relationship types for any pair of entities.
 * @param {string} props.seriesType - Enity type of the series.
 * @param {Function} props.onRemove - A function to be called when the Remove
 *        button is clicked.
 * @param {Function} props.onEdit - A function to be called when attribute values is
 *        modified.
 * @param {Function} props.onAdd - A function to be called when the add button
 *        is clicked.
 * @param {Array} props.seriesItemsArray - The list of entities.
 * @param {boolean} props.isUnifiedForm - Whether the series section is render inside UF.
 *
 * @returns {ReactElement} React Component as a modular section within the entity editor.
 *
 */

var SeriesListItem = function SeriesListItem(_ref) {
  var _find2;
  var value = _ref.value,
    baseEntity = _ref.baseEntity,
    handleNumberAttributeChange = _ref.handleNumberAttributeChange,
    onRemove = _ref.onRemove,
    dragHandler = _ref.dragHandler,
    isUnifiedForm = _ref.isUnifiedForm;
  return /*#__PURE__*/_react.default.createElement(_reactBootstrap.Row, {
    className: "margin-top-d5 ".concat(isUnifiedForm ? 'w-100 align-items-center' : ''),
    key: value.rowID
  }, (!isUnifiedForm || dragHandler) && /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
    className: "text-right form-control-static padding-left-0",
    lg: 1
  }, dragHandler ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactFontawesome.FontAwesomeIcon, {
    icon: _freeSolidSvgIcons.faBars
  }), " \xA0\xA0") : null), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
    lg: 2
  }, /*#__PURE__*/_react.default.createElement("input", {
    className: "form-control",
    placeholder: "Enter value...",
    type: "text",
    value: ((_find2 = (0, _find3.default)(value.attributes, {
      attributeType: 2
    })) === null || _find2 === void 0 ? void 0 : _find2.value.textValue) || '',
    onChange: handleNumberAttributeChange.bind(_this, value.rowID)
  })), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
    className: "form-control-static",
    lg: 7
  }, /*#__PURE__*/_react.default.createElement(_relationship.default, {
    link: true,
    contextEntity: baseEntity,
    relationshipType: value.relationshipType,
    sourceEntity: value.sourceEntity,
    targetEntity: value.targetEntity
  })), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
    lg: 2
  }, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Button, {
    role: "button",
    variant: "danger",
    onClick: onRemove.bind(_this, value.rowID)
  }, /*#__PURE__*/_react.default.createElement(_reactFontawesome.FontAwesomeIcon, {
    icon: _freeSolidSvgIcons.faTimes
  }), /*#__PURE__*/_react.default.createElement("span", null, "\xA0Remove"))));
};
SeriesListItem.displayName = 'SeriesListItem';
SeriesListItem.propTypes = {
  baseEntity: _propTypes.default.object.isRequired,
  dragHandler: _propTypes.default.bool.isRequired,
  handleNumberAttributeChange: _propTypes.default.func.isRequired,
  isUnifiedForm: _propTypes.default.bool.isRequired,
  onRemove: _propTypes.default.func.isRequired,
  value: _propTypes.default.any.isRequired
};
var SortableItem = (0, _reactSortableHoc.SortableElement)(function (_ref2) {
  var value = _ref2.value,
    onRemove = _ref2.onRemove,
    baseEntity = _ref2.baseEntity,
    handleNumberAttributeChange = _ref2.handleNumberAttributeChange,
    isUnifiedForm = _ref2.isUnifiedForm;
  return /*#__PURE__*/_react.default.createElement(SeriesListItem, {
    dragHandler: true,
    baseEntity: baseEntity,
    handleNumberAttributeChange: handleNumberAttributeChange,
    isUnifiedForm: isUnifiedForm,
    value: value,
    onRemove: onRemove
  });
});
var SortableList = (0, _reactSortableHoc.SortableContainer)(function (_ref3) {
  var children = _ref3.children;
  return /*#__PURE__*/_react.default.createElement("div", null, children);
});
function SeriesEditor(_ref4) {
  var baseEntity = _ref4.baseEntity,
    relationshipTypes = _ref4.relationshipTypes,
    seriesType = _ref4.seriesType,
    orderType = _ref4.orderType,
    onRemove = _ref4.onRemove,
    hideItemSelect = _ref4.hideItemSelect,
    onAdd = _ref4.onAdd,
    onEdit = _ref4.onEdit,
    onSort = _ref4.onSort,
    seriesItemsArray = _ref4.seriesItemsArray,
    isUnifiedForm = _ref4.isUnifiedForm;
  var _useState = (0, _react.useState)(null),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    seriesItem = _useState2[0],
    setSeriesItem = _useState2[1];
  var _useState3 = (0, _react.useState)(null),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    targetEntity = _useState4[0],
    setTargetEntity = _useState4[1];
  var handleEntityChange = function handleEntityChange(value) {
    setTargetEntity(value);
    // Convert "value" of type EntitySearchResult to type Entity
    var otherEntity = {
      bbid: (0, _get2.default)(value, 'id'),
      defaultAlias: {
        name: (0, _get2.default)(value, 'text')
      },
      disambiguation: (0, _get2.default)(value, 'disambiguation'),
      type: (0, _get2.default)(value, 'type')
    };
    var relationships = (0, _relationshipEditor.generateRelationshipSelection)(relationshipTypes, baseEntity, otherEntity);
    if (relationships.length) {
      // Filter out relationship type 70 - 74
      var filterRelationships = relationships.filter(function (relationship) {
        return relationship.relationshipType.id > 69 && relationship.relationshipType.id < 75;
      });
      setSeriesItem(filterRelationships[0]);
    }
  };
  var handleAdd = function handleAdd() {
    if (onAdd && targetEntity) {
      // Set attributes value before adding the entity
      seriesItem.attributes = [{
        attributeType: 1,
        value: {
          textValue: null
        }
      }, {
        attributeType: 2,
        value: {
          textValue: null
        }
      }];
      if (seriesItem) {
        onAdd(seriesItem);
        onSort({
          newIndex: null,
          oldIndex: null
        });
        // After adding the entity, clear the Entity Search Field by setting targetEntity to null.
        setTargetEntity(null);
      }
    }
  };
  var handleNumberAttributeChange = function handleNumberAttributeChange(rowID, _ref5) {
    var target = _ref5.target;
    var value = target.value === '' ? null : target.value;
    var attributeNumber = {
      attributeType: 2,
      value: {
        textValue: value
      }
    };
    onEdit(attributeNumber, rowID);
    onSort({
      newIndex: null,
      oldIndex: null
    });
  };
  var heading = /*#__PURE__*/_react.default.createElement("h2", null, "What ", seriesType, "s are part of this Series?");
  var alignText = isUnifiedForm ? 'text-left' : 'text-right';
  return /*#__PURE__*/_react.default.createElement("div", null, !isUnifiedForm && heading, seriesItemsArray.length ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, " ", orderType === 1 ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, seriesItemsArray.map(function (value) {
    return !value.isRemoved && /*#__PURE__*/_react.default.createElement(SeriesListItem, {
      baseEntity: baseEntity,
      dragHandler: false,
      handleNumberAttributeChange: handleNumberAttributeChange,
      isUnifiedForm: isUnifiedForm,
      key: value.rowID,
      value: value,
      onRemove: onRemove
    });
  })) : /*#__PURE__*/_react.default.createElement(SortableList, {
    distance: 1,
    onSortEnd: onSort
  }, seriesItemsArray.map(function (value, index) {
    return !value.isRemoved && /*#__PURE__*/_react.default.createElement(SortableItem, {
      baseEntity: baseEntity,
      handleNumberAttributeChange: handleNumberAttributeChange,
      index: index,
      isUnifiedForm: isUnifiedForm,
      key: value.rowID,
      value: value,
      onRemove: onRemove
    });
  }))) : null, !hideItemSelect && /*#__PURE__*/_react.default.createElement(_reactBootstrap.Row, {
    className: "margin-top-d8"
  }, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
    className: alignText,
    lg: isUnifiedForm ? 7 : 3
  }, /*#__PURE__*/_react.default.createElement("p", {
    className: "margin-top-d5"
  }, "Add an entity to the series:")), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
    lg: isUnifiedForm ? 6 : 7,
    style: {
      marginTop: -22
    }
  }, /*#__PURE__*/_react.default.createElement(_entitySearchFieldOption.default, {
    className: "series-editor-select",
    instanceId: "entitySearchField",
    name: "entity",
    type: [seriesType],
    value: targetEntity,
    onChange: handleEntityChange
  })), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
    lg: isUnifiedForm ? 7 : 2
  }, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Button, {
    variant: "success",
    onClick: handleAdd
  }, /*#__PURE__*/_react.default.createElement(_reactFontawesome.FontAwesomeIcon, {
    icon: _freeSolidSvgIcons.faPlus
  }), /*#__PURE__*/_react.default.createElement("span", null, "\xA0Add ", seriesType)))));
}
SeriesEditor.displayName = 'SeriesEditor';
SeriesEditor.defaultProps = {
  isUnifiedForm: false
};
var _default = SeriesEditor;
exports.default = _default;
//# sourceMappingURL=series-editor.js.map