client/entity-editor/publisher-section/publisher-section-merge.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.object.to-string.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.function.name.js");
var _isNil2 = _interopRequireDefault(require("lodash/isNil"));
var _find2 = _interopRequireDefault(require("lodash/find"));
var React = _interopRequireWildcard(require("react"));
var _actions = require("./actions");
var _entity = require("../../helpers/entity");
var _publisher = require("../validators/publisher");
var _entitySelect = _interopRequireDefault(require("../common/entity-select"));
var _linkedEntitySelect = _interopRequireDefault(require("../common/linked-entity-select"));
var _mergeField = _interopRequireDefault(require("../common/merge-field"));
var _reactRedux = require("react-redux");
var _utils = require("../../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) 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 PublisherSectionMerge component contains input fields
 * specific to the publisher 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 {Map<string, any>} props.areaValue - The area currently set for this
 *        publisher.
 * @param {string} props.beginDateValue - The begin date currently set for
 *        this publisher.
 * @param {string} props.endDateValue - The end date currently set for this
 *        publisher.
 * @param {boolean} props.endedChecked - Whether or not the ended checkbox
 *        is checked.
 * @param {Array} props.mergingEntities - The list of entities being merged
 * @param {number} props.typeValue - The ID of the type currently selected for
 *        the publisher.
 * @param {Function} props.onAreaChange - A function to be called when a
 *        different area is selected.
 * @param {Function} props.onBeginDateChange - A function to be called when
 *        the begin date is changed.
 * @param {Function} props.onEndDateChange - A function to be called when
 *        the end date is changed.
 * @param {Function} props.onEndedChange - A function to be called when
 *        the ended checkbox is toggled.
 * @param {Function} props.onTypeChange - A function to be called when
 *        a different publisher type is selected.
 * @returns {ReactElement} React element containing the rendered
 *          PublisherSectionMerge.
 */
function PublisherSectionMerge(_ref) {
  var areaValue = _ref.areaValue,
    beginDateValue = _ref.beginDateValue,
    endDateValue = _ref.endDateValue,
    endedChecked = _ref.endedChecked,
    mergingEntities = _ref.mergingEntities,
    typeValue = _ref.typeValue,
    onAreaChange = _ref.onAreaChange,
    onBeginDateChange = _ref.onBeginDateChange,
    onEndDateChange = _ref.onEndDateChange,
    onEndedChange = _ref.onEndedChange,
    onTypeChange = _ref.onTypeChange;
  var areaOptions = [];
  var typeOptions = [];
  var beginDateOptions = [];
  var endDateOptions = [];
  var endedOptions = [];
  mergingEntities.forEach(function (entity) {
    var typeOption = entity.publisherType && {
      label: entity.publisherType.label,
      value: entity.publisherType.id
    };
    if (typeOption && !(0, _find2.default)(typeOptions, ['value', typeOption.value])) {
      typeOptions.push(typeOption);
    }
    var area = !(0, _isNil2.default)(entity.area) && {
      label: entity.area.name,
      value: (0, _entity.entityToOption)(entity.area)
    };
    if (area && !(0, _find2.default)(areaOptions, ['value.id', area.value.id])) {
      areaOptions.push(area);
    }
    var beginDate = !(0, _isNil2.default)(entity.beginDate) && (0, _entity.transformISODateForSelect)(entity.beginDate);
    if (beginDate && !(0, _find2.default)(beginDateOptions, ['value', beginDate.value])) {
      beginDateOptions.push(beginDate);
    }
    var ended = !(0, _isNil2.default)(entity.ended) && {
      label: entity.ended ? 'Yes' : 'No',
      value: entity.ended
    };
    if (ended && !(0, _find2.default)(endedOptions, ['value', ended.value])) {
      endedOptions.push(ended);
    }
    var endDate = !(0, _isNil2.default)(entity.endDate) && (0, _entity.transformISODateForSelect)(entity.endDate);
    if (endDate && !(0, _find2.default)(endDateOptions, ['value', endDate.value])) {
      endDateOptions.push(endDate);
    }
  });
  var formattedBeginDateValue = (0, _entity.transformISODateForSelect)(beginDateValue);
  var formattedEndDateValue = endedChecked ? (0, _entity.transformISODateForSelect)(endDateValue) : '';
  var _validatePublisherSec = (0, _publisher.validatePublisherSectionBeginDate)(beginDateValue),
    isValidBeginDate = _validatePublisherSec.isValid,
    errorMessageBeginDate = _validatePublisherSec.errorMessage;
  var _validatePublisherSec2 = (0, _publisher.validatePublisherSectionEndDate)(beginDateValue, endDateValue, endedChecked),
    isValidEndDate = _validatePublisherSec2.isValid,
    errorMessageEndDate = _validatePublisherSec2.errorMessage;
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_mergeField.default, {
    currentValue: typeValue,
    label: "Type",
    options: typeOptions,
    onChange: onTypeChange
  }), /*#__PURE__*/React.createElement(_mergeField.default, {
    components: {
      Option: _linkedEntitySelect.default,
      SingleValue: _entitySelect.default
    },
    currentValue: areaValue,
    label: "Area",
    options: areaOptions,
    onChange: onAreaChange
  }), /*#__PURE__*/React.createElement(_mergeField.default, {
    currentValue: formattedBeginDateValue,
    error: !isValidBeginDate,
    errorMessage: errorMessageBeginDate,
    label: "Date Founded",
    options: beginDateOptions,
    onChange: onBeginDateChange
  }), /*#__PURE__*/React.createElement(_mergeField.default, {
    currentValue: endedChecked,
    label: "Dissolved?",
    options: endedOptions,
    onChange: onEndedChange
  }), endedChecked && /*#__PURE__*/React.createElement(_mergeField.default, {
    currentValue: formattedEndDateValue,
    error: !isValidEndDate,
    errorMessage: errorMessageEndDate,
    label: "Date Dissolved",
    options: endDateOptions,
    onChange: onEndDateChange
  }));
}
PublisherSectionMerge.displayName = 'PublisherSectionMerge';
function mapStateToProps(rootState) {
  var state = rootState.get('publisherSection');
  return {
    areaValue: (0, _utils.convertMapToObject)(state.get('area')),
    beginDateValue: state.get('beginDate'),
    endDateValue: state.get('endDate'),
    endedChecked: state.get('ended'),
    typeValue: (0, _utils.convertMapToObject)(state.get('type'))
  };
}
function mapDispatchToProps(dispatch) {
  return {
    onAreaChange: function onAreaChange(value) {
      return dispatch((0, _actions.updateArea)(value));
    },
    onBeginDateChange: function onBeginDateChange(beginDate) {
      dispatch((0, _actions.debouncedUpdateBeginDate)(beginDate));
    },
    onEndDateChange: function onEndDateChange(endDate) {
      return dispatch((0, _actions.debouncedUpdateEndDate)(endDate));
    },
    onEndedChange: function onEndedChange(value) {
      return dispatch((0, _actions.updateEnded)(value));
    },
    onTypeChange: function onTypeChange(value) {
      return dispatch((0, _actions.updateType)(value));
    }
  };
}
var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(PublisherSectionMerge);
exports.default = _default;
//# sourceMappingURL=publisher-section-merge.js.map