client/entity-editor/author-section/author-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.map.js");
require("core-js/modules/es.function.name.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.array.find.js");
var React = _interopRequireWildcard(require("react"));
var _actions = require("./actions");
var _reactBootstrap = require("react-bootstrap");
var _utils = require("../../helpers/utils");
var _author = require("../validators/author");
var _newDateField = _interopRequireDefault(require("../common/new-date-field"));
var _entitySearchFieldOption = _interopRequireDefault(require("../common/entity-search-field-option"));
var _reactSelect = _interopRequireDefault(require("react-select"));
var _reactRedux = require("react-redux");
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) 2016  Ben Ockmore
 *
 * 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 AuthorSection component contains input fields
 * specific to the author 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 {string} props.beginDateLabel - The label to be used for the begin
 *        date input.
 * @param {string} props.beginDateValue - The begin date currently set for
 *        this author.
 * @param {Array} props.authorTypes - The list of possible types for a author.
 * @param {string} props.endDateLabel - The label to be used for the end date
 *        input.
 * @param {string} props.endDateValue - The end date currently set for this
 *        author.
 * @param {boolean} props.endedChecked - Whether or not the ended checkbox
 *        is checked.
 * @param {string} props.endedLabel - The label to be used for the ended
 *        checkbox input.
 * @param {Array} props.genderOptions - The list of possible genders.
 * @param {boolean} props.genderShow - Whether or not the gender field should
 *        be shown (only for authors which represent people).
 * @param {number} props.genderValue - The ID of the gender currently selected.
 * @param {number} props.typeValue - The ID of the type currently selected for
 *        the author.
 * @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.onGenderChange - A function to be called when
 *        a different gender is selected.
 * @param {Function} props.onTypeChange - A function to be called when
 *        a different author type is selected.
 * @returns {ReactElement} React element containing the rendered AuthorSection.
 */
function AuthorSection(_ref) {
  var beginAreaLabel = _ref.beginAreaLabel,
    beginDateLabel = _ref.beginDateLabel,
    beginDateValue = _ref.beginDateValue,
    beginAreaValue = _ref.beginAreaValue,
    authorTypes = _ref.authorTypes,
    endAreaLabel = _ref.endAreaLabel,
    endAreaValue = _ref.endAreaValue,
    endDateLabel = _ref.endDateLabel,
    endDateValue = _ref.endDateValue,
    endedChecked = _ref.endedChecked,
    endedLabel = _ref.endedLabel,
    genderOptions = _ref.genderOptions,
    genderShow = _ref.genderShow,
    genderValue = _ref.genderValue,
    typeValue = _ref.typeValue,
    isUnifiedForm = _ref.isUnifiedForm,
    onBeginAreaChange = _ref.onBeginAreaChange,
    onBeginDateChange = _ref.onBeginDateChange,
    onEndAreaChange = _ref.onEndAreaChange,
    onEndDateChange = _ref.onEndDateChange,
    onEndedChange = _ref.onEndedChange,
    onGenderChange = _ref.onGenderChange,
    onTypeChange = _ref.onTypeChange;
  var genderOptionsForDisplay = genderOptions.map(function (gender) {
    return {
      label: gender.name,
      value: gender.id
    };
  });
  var genderOption = genderOptionsForDisplay.filter(function (el) {
    return el.value === genderValue;
  });
  var authorTypesForDisplay = authorTypes.map(function (type) {
    return {
      label: type.label,
      value: type.id
    };
  });
  var typeOption = authorTypesForDisplay.filter(function (el) {
    return el.value === typeValue;
  });
  var currentAuthorType = typeValue ? authorTypes.find(function (type) {
    return type.id === typeValue;
  }) : {
    id: 1,
    label: 'Person'
  };
  var _validateAuthorSectio = (0, _author.validateAuthorSectionBeginDate)(beginDateValue),
    isValidDob = _validateAuthorSectio.isValid,
    dobError = _validateAuthorSectio.errorMessage;
  var _validateAuthorSectio2 = (0, _author.validateAuthorSectionEndDate)(beginDateValue, endDateValue, currentAuthorType.label),
    isValidDod = _validateAuthorSectio2.isValid,
    dodError = _validateAuthorSectio2.errorMessage;
  var heading = /*#__PURE__*/React.createElement("h2", null, "What else do you know about the Author?");
  var lgCol = {
    offset: 3,
    span: 6
  };
  if (isUnifiedForm) {
    lgCol.offset = 0;
  }
  return /*#__PURE__*/React.createElement("div", null, !isUnifiedForm && heading, /*#__PURE__*/React.createElement("p", {
    className: "text-muted"
  }, "All fields optional \u2014 leave something blank if you don\u2019t know it"), /*#__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, "Type"), /*#__PURE__*/React.createElement(_reactSelect.default, {
    isClearable: true,
    classNamePrefix: "react-select",
    instanceId: "authorType",
    options: authorTypesForDisplay,
    value: typeOption,
    onChange: onTypeChange
  })))), /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: lgCol
  }, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, {
    className: genderShow ? null : 'd-none'
  }, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, "Gender"), /*#__PURE__*/React.createElement(_reactSelect.default, {
    classNamePrefix: "react-select",
    instanceId: "gender",
    isClearable: "true",
    options: genderOptionsForDisplay,
    value: genderOption,
    onChange: onGenderChange
  })))), /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: lgCol
  }, /*#__PURE__*/React.createElement(_newDateField.default, {
    show: true,
    defaultValue: beginDateValue,
    empty: (0, _utils.isNullDate)(beginDateValue),
    error: !isValidDob,
    errorMessage: dobError,
    label: beginDateLabel,
    onChangeDate: onBeginDateChange
  }))), /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: lgCol
  }, /*#__PURE__*/React.createElement(_entitySearchFieldOption.default, {
    instanceId: "beginArea",
    label: beginAreaLabel,
    type: "area",
    value: beginAreaValue,
    onChange: onBeginAreaChange
  }))), /*#__PURE__*/React.createElement("div", {
    className: !isUnifiedForm && 'text-center'
  }, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Check, {
    defaultChecked: endedChecked,
    label: endedLabel,
    type: "checkbox",
    onChange: onEndedChange
  })), endedChecked && /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: lgCol
  }, /*#__PURE__*/React.createElement(_newDateField.default, {
    show: true,
    defaultValue: endDateValue,
    empty: (0, _utils.isNullDate)(endDateValue),
    error: !isValidDod,
    errorMessage: dodError,
    label: endDateLabel,
    onChangeDate: onEndDateChange
  }))), /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: lgCol
  }, /*#__PURE__*/React.createElement(_entitySearchFieldOption.default, {
    instanceId: "endArea",
    label: endAreaLabel,
    type: "area",
    value: endAreaValue,
    onChange: onEndAreaChange
  })))));
}
AuthorSection.displayName = 'AuthorSection';
function mapStateToProps(rootState, _ref2) {
  var authorTypes = _ref2.authorTypes;
  var state = rootState.get('authorSection');
  var typeValue = state.get('type');
  var personType = authorTypes.find(function (type) {
    return type.label === 'Person';
  });
  var groupType = authorTypes.find(function (type) {
    return type.label === 'Group';
  });
  if (!personType) {
    throw new Error('there should be an author type with label "Person"');
  }
  if (!groupType) {
    throw new Error('there should be an author type with label "Group"');
  }
  var isGroup = typeValue === groupType.id;
  var _labelsForAuthor = (0, _utils.labelsForAuthor)(isGroup),
    beginDateLabel = _labelsForAuthor.beginDateLabel,
    beginAreaLabel = _labelsForAuthor.beginAreaLabel,
    endedLabel = _labelsForAuthor.endedLabel,
    endDateLabel = _labelsForAuthor.endDateLabel,
    endAreaLabel = _labelsForAuthor.endAreaLabel;
  return {
    beginAreaLabel: beginAreaLabel,
    beginAreaValue: state.get('beginArea'),
    beginDateLabel: beginDateLabel,
    beginDateValue: state.get('beginDate'),
    endAreaLabel: endAreaLabel,
    endAreaValue: state.get('endArea'),
    endDateLabel: endDateLabel,
    endDateValue: state.get('endDate'),
    endedChecked: state.get('ended'),
    endedLabel: endedLabel,
    genderShow: !isGroup,
    genderValue: state.get('gender'),
    typeValue: typeValue
  };
}
function mapDispatchToProps(dispatch) {
  return {
    onBeginAreaChange: function onBeginAreaChange(value) {
      return dispatch((0, _actions.updateBeginArea)(value));
    },
    onBeginDateChange: function onBeginDateChange(beginDate) {
      return dispatch((0, _actions.debouncedUpdateBeginDate)(beginDate));
    },
    onEndAreaChange: function onEndAreaChange(value) {
      return dispatch((0, _actions.updateEndArea)(value));
    },
    onEndDateChange: function onEndDateChange(endDate) {
      return dispatch((0, _actions.debouncedUpdateEndDate)(endDate));
    },
    onEndedChange: function onEndedChange(event) {
      return dispatch((0, _actions.updateEnded)(event.target.checked));
    },
    onGenderChange: function onGenderChange(value) {
      return dispatch((0, _actions.updateGender)(value && value.value));
    },
    onTypeChange: function onTypeChange(value) {
      return dispatch((0, _actions.updateType)(value && value.value));
    }
  };
}
var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(AuthorSection);
exports.default = _default;
//# sourceMappingURL=author-section.js.map