client/entity-editor/name-section/name-section-merge.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.function.name.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.index-of.js");
var _capitalize2 = _interopRequireDefault(require("lodash/capitalize"));
var _isNil2 = _interopRequireDefault(require("lodash/isNil"));
var _findIndex2 = _interopRequireDefault(require("lodash/findIndex"));
var _actions = require("./actions");
var _mergeField = _interopRequireDefault(require("../common/merge-field"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireDefault(require("react"));
var _reactRedux = require("react-redux");
var _reactValidators = require("../../helpers/react-validators");
/*
 * Copyright (C) 2019  Nicolas Pelletier
 *
 * 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 NameSectionMerge component contains input fields for
 * setting the name of an entity. It also allows setting of the entity's
 * disambiguation. 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 {string} props.disambiguationDefaultValue - The default value for the
 *        disambiguation field.
 * @param {Array} props.languageOptions - The list of possible languages for the
 *        entity name.
 * @param {string} props.languageValue - The ID of the language currently
 *        selected for the entity name.
 * @param {string} props.nameValue - The name currently set for this entity.
 * @param {string} props.sortNameValue - The sort name currently set for this
 *        entity.
 * @param {Function} props.onLanguageChange - A function to be called when a
 *        different language type is selected.
 * @param {Function} props.onNameChange - A function to be called when the name
 *        is changed.
 * @param {Function} props.onSortNameChange - A function to be called when the
 *        sort name is changed.
 * @param {Function} props.onDisambiguationChange - A function to be called when
 *        the disambiguation is changed.
 * @returns {ReactElement} React element containing the rendered NameSectionMerge.
 */

function NameSectionMerge(_ref) {
  var disambiguationDefaultValue = _ref.disambiguationDefaultValue,
    mergingEntities = _ref.mergingEntities,
    entityType = _ref.entityType,
    languageOptions = _ref.languageOptions,
    languageValue = _ref.languageValue,
    nameValue = _ref.nameValue,
    sortNameValue = _ref.sortNameValue,
    onLanguageChange = _ref.onLanguageChange,
    onNameChange = _ref.onNameChange,
    onSortNameChange = _ref.onSortNameChange,
    onDisambiguationChange = _ref.onDisambiguationChange;
  var nameOptions = [];
  var sortNameOptions = [];
  var languageSelectOptions = [];
  var disambiguationOptions = [];
  mergingEntities.forEach(function (entity) {
    if ((0, _findIndex2.default)(nameOptions, ['label', entity.defaultAlias.name]) === -1) {
      nameOptions.push({
        label: entity.defaultAlias.name,
        value: entity.defaultAlias.name
      });
    }
    if ((0, _findIndex2.default)(sortNameOptions, ['label', entity.defaultAlias.sortName]) === -1) {
      sortNameOptions.push({
        label: entity.defaultAlias.sortName,
        value: entity.defaultAlias.sortName
      });
    }
    var matchingLanguage = languageOptions.filter(function (language) {
      return language.id === entity.defaultAlias.languageId;
    }).map(function (language) {
      return {
        label: language.name,
        value: language.id
      };
    });
    if ((0, _findIndex2.default)(languageSelectOptions, ['value', matchingLanguage[0].value]) === -1) {
      languageSelectOptions.push(matchingLanguage[0]);
    }
    if (!(0, _isNil2.default)(entity.disambiguation) && disambiguationOptions.indexOf(entity.disambiguation) === -1) {
      disambiguationOptions.push({
        label: entity.disambiguation.comment,
        value: entity.disambiguation.comment
      });
    }
  });
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_mergeField.default, {
    currentValue: nameValue,
    label: "Name",
    options: nameOptions,
    tooltipText: "Prefered name of the ".concat((0, _capitalize2.default)(entityType), " in their original language.\n\t\t\t\tOther names (full name, name in another language) are added as 'aliases'."),
    valueProperty: "value",
    onChange: onNameChange
  }), /*#__PURE__*/_react.default.createElement(_mergeField.default, {
    currentValue: sortNameValue,
    label: "Sort Name",
    options: sortNameOptions,
    tooltipText: "Alphabetical sort name for the above name.",
    valueProperty: "value",
    onChange: onSortNameChange
  }), /*#__PURE__*/_react.default.createElement(_mergeField.default, {
    currentValue: languageValue,
    label: "Language",
    options: languageSelectOptions,
    tooltipText: "Language the above name is in",
    onChange: onLanguageChange
  }), /*#__PURE__*/_react.default.createElement(_mergeField.default, {
    currentValue: disambiguationDefaultValue,
    label: "Disambiguation",
    options: disambiguationOptions,
    tooltipText: "In case there is another distinct entity with the same name",
    onChange: onDisambiguationChange
  }));
}
NameSectionMerge.displayName = 'NameSectionMerge';
NameSectionMerge.propTypes = {
  disambiguationDefaultValue: _propTypes.default.string,
  entityType: _reactValidators.entityTypeProperty.isRequired,
  languageOptions: _propTypes.default.array.isRequired,
  languageValue: _propTypes.default.number,
  mergingEntities: _propTypes.default.array.isRequired,
  nameValue: _propTypes.default.string.isRequired,
  onDisambiguationChange: _propTypes.default.func.isRequired,
  onLanguageChange: _propTypes.default.func.isRequired,
  onNameChange: _propTypes.default.func.isRequired,
  onSortNameChange: _propTypes.default.func.isRequired,
  sortNameValue: _propTypes.default.string.isRequired
};
NameSectionMerge.defaultProps = {
  disambiguationDefaultValue: null,
  languageValue: null
};
function mapStateToProps(rootState) {
  var state = rootState.get('nameSection');
  return {
    disambiguationDefaultValue: state.get('disambiguation'),
    languageValue: state.get('language'),
    nameValue: state.get('name'),
    sortNameValue: state.get('sortName')
  };
}
function mapDispatchToProps(dispatch) {
  return {
    onDisambiguationChange: function onDisambiguationChange(option) {
      dispatch((0, _actions.debouncedUpdateDisambiguationField)(option));
    },
    onLanguageChange: function onLanguageChange(option) {
      return dispatch((0, _actions.updateLanguageField)(option));
    },
    onNameChange: function onNameChange(option) {
      return dispatch((0, _actions.debouncedUpdateNameField)(option));
    },
    onSortNameChange: function onSortNameChange(option) {
      return dispatch((0, _actions.debouncedUpdateSortNameField)(option));
    }
  };
}
var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(NameSectionMerge);
exports.default = _default;
//# sourceMappingURL=name-section-merge.js.map