client/entity-editor/entity-editor.js

"use strict";

require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.object.get-own-property-descriptors.js");
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");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _isEqual2 = _interopRequireDefault(require("lodash/isEqual"));
var React = _interopRequireWildcard(require("react"));
var _reactRedux = require("react-redux");
var _aliasEditor = _interopRequireDefault(require("./alias-editor/alias-editor"));
var _annotationSection = _interopRequireDefault(require("./annotation-section/annotation-section"));
var _buttonBar = _interopRequireDefault(require("./button-bar/button-bar"));
var _reactBootstrap = require("react-bootstrap");
var _identifierEditor = _interopRequireDefault(require("./identifier-editor/identifier-editor"));
var _nameSection = _interopRequireDefault(require("./name-section/name-section"));
var _relationshipSection = _interopRequireDefault(require("./relationship-editor/relationship-section"));
var _submissionSection = _interopRequireDefault(require("./submission-section/submission-section"));
var _entity = require("../helpers/entity");
var _actions = require("./submission-section/actions");
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; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } /*
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          * 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. Renders all of the sections of the entity editing form.
 *
 * @param {Object} props - The properties passed to the component.
 * @param {boolean} props.aliasEditorVisible - Whether the alias editor modal
 *        should be made visible.
 * @param {boolean} props.identifierEditorVisible - Whether the identifier
 *        editor modal should be made visible.
 * @param {React.Node} props.children - The child content to wrap with this
 *        entity editor form.
 * @param {Function} props.onSubmit - A function to be called when the
 *        submit button is clicked.
 * @returns {ReactElement} React element containing the rendered EntityEditor.
 */
var EntityEditor = function EntityEditor(props) {
  var aliasEditorVisible = props.aliasEditorVisible,
    children = props.children,
    heading = props.heading,
    identifierEditorVisible = props.identifierEditorVisible,
    onSubmit = props.onSubmit,
    entity = props.entity;
  var currentState = (0, _reactRedux.useSelector)(function (state) {
    return state;
  }).toJS();
  var entityURL;
  // eslint-disable-next-line consistent-return
  var handleUrlChange = React.useCallback(function () {
    if (!(0, _isEqual2.default)(currentState, props.intitialState) && !currentState.submissionSection.submitted) {
      return 'You have some unsaved changes!';
    }
  }, [currentState]);
  React.useEffect(function () {
    window.onbeforeunload = handleUrlChange;
  }, [handleUrlChange]);
  if (entity) {
    entityURL = (0, _entity.getEntityUrl)(entity);
  }
  return /*#__PURE__*/React.createElement("form", {
    onSubmit: onSubmit
  }, /*#__PURE__*/React.createElement(_reactBootstrap.Card, null, /*#__PURE__*/React.createElement(_reactBootstrap.Card.Header, {
    as: "h4"
  }, /*#__PURE__*/React.createElement("div", null, entityURL ? /*#__PURE__*/React.createElement("a", {
    href: entityURL
  }, heading) : heading)), /*#__PURE__*/React.createElement(_reactBootstrap.Card.Body, null, /*#__PURE__*/React.createElement(_aliasEditor.default, (0, _extends2.default)({
    show: aliasEditorVisible
  }, props)), /*#__PURE__*/React.createElement(_nameSection.default, props), /*#__PURE__*/React.createElement(_buttonBar.default, props), /*#__PURE__*/React.cloneElement(React.Children.only(children), _objectSpread({}, props)), /*#__PURE__*/React.createElement(_relationshipSection.default, props), /*#__PURE__*/React.createElement(_identifierEditor.default, (0, _extends2.default)({
    show: identifierEditorVisible
  }, props)), /*#__PURE__*/React.createElement(_annotationSection.default, props)), /*#__PURE__*/React.createElement(_reactBootstrap.Card.Footer, null, /*#__PURE__*/React.createElement(_submissionSection.default, props))));
};
EntityEditor.displayName = 'EntityEditor';
function mapStateToProps(rootState) {
  var state = rootState.get('buttonBar');
  return {
    aliasEditorVisible: state.get('aliasEditorVisible'),
    identifierEditorVisible: state.get('identifierEditorVisible')
  };
}
function mapDispatchToProps(dispatch, _ref) {
  var submissionUrl = _ref.submissionUrl;
  return {
    onSubmit: function onSubmit(event) {
      event.preventDefault();
      dispatch((0, _actions.submit)(submissionUrl));
    }
  };
}
var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(EntityEditor);
exports.default = _default;
//# sourceMappingURL=entity-editor.js.map