client/entity-editor/identifier-editor/identifier-row.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.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.regexp.constructor.js");
require("core-js/modules/es.regexp.to-string.js");
var React = _interopRequireWildcard(require("react"));
var data = _interopRequireWildcard(require("../../helpers/data"));
var _actions = require("./actions");
var _reactBootstrap = require("react-bootstrap");
var _common = require("../validators/common");
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _reactSelect = _interopRequireDefault(require("react-select"));
var _valueField = _interopRequireDefault(require("./value-field"));
var _utils = require("../../../common/helpers/utils");
var _reactRedux = require("react-redux");
var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");
var _identifiersLinks = _interopRequireDefault(require("../../components/pages/entities/identifiers-links.js"));
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 IdentifierRow component renders a single Row
 * containing several input fields, allowing the user to set the value and type
 * for an identifier in the IdentifierEditor. A button is also included to
 * remove the identifier from the editor.
 *
 * @param {Object} props - The properties passed to the component.
 * @param {number} props.index - The index of the row in the parent editor.
 * @param {Array} props.typeOptions - The list of possible types for an
 *        identifier.
 * @param {number} props.typeValue - The ID of the type currently selected.
 * @param {string} props.valueValue - The value currently set for this
 *        identifier.
 * @param {Function} props.onTypeChange - A function to be called when a new
 *        identifier type is selected.
 * @param {Function} props.onRemoveButtonClick - A function to be called when
 *        the button to remove the identifier is clicked.
 * @param {Function} props.onValueChange - A function to be called when the
 *        value for the identifier is changed.
 * @returns {ReactElement} React element containing the rendered IdentifierRow.
 */
function IdentifierRow(_ref) {
  var index = _ref.index,
    typeOptions = _ref.typeOptions,
    valueValue = _ref.valueValue,
    typeValue = _ref.typeValue,
    onTypeChange = _ref.onTypeChange,
    onRemoveButtonClick = _ref.onRemoveButtonClick,
    onValueChange = _ref.onValueChange;
  var identifierTypesForDisplay = typeOptions.map(function (type) {
    return {
      label: type.label,
      value: type.id
    };
  });
  var identifierValue = identifierTypesForDisplay.filter(function (el) {
    return el.value === typeValue;
  });
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: 4
  }, /*#__PURE__*/React.createElement(_valueField.default, {
    defaultValue: valueValue,
    empty: !valueValue && typeValue === null,
    error: !(0, _common.validateIdentifierValue)(valueValue, typeValue, typeOptions),
    onChange: onValueChange
  })), /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    lg: 4
  }, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, "Type"), /*#__PURE__*/React.createElement(_reactSelect.default, {
    classNamePrefix: "react-select",
    instanceId: "identifierType".concat(index),
    options: identifierTypesForDisplay,
    value: identifierValue,
    onChange: onTypeChange
  }))), /*#__PURE__*/React.createElement(_reactBootstrap.Col, {
    className: "text-right",
    lg: {
      offset: 1,
      span: 3
    }
  }, /*#__PURE__*/React.createElement(_reactBootstrap.Button, {
    block: true,
    className: "margin-top-d15",
    variant: "danger",
    onClick: onRemoveButtonClick
  }, /*#__PURE__*/React.createElement(_reactFontawesome.FontAwesomeIcon, {
    icon: _freeSolidSvgIcons.faTimes
  }), /*#__PURE__*/React.createElement("span", null, "\xA0Remove")))), typeValue && valueValue && /*#__PURE__*/React.createElement(_reactBootstrap.Row, null, /*#__PURE__*/React.createElement(_reactBootstrap.Col, null, "Preview Link:", /*#__PURE__*/React.createElement(_identifiersLinks.default, {
    typeId: typeValue,
    value: valueValue
  }))), /*#__PURE__*/React.createElement("hr", null));
}
IdentifierRow.displayName = 'IdentifierEditor.Identifier';
function handleValueChange(dispatch, event, index, types) {
  var value = event.target.value;
  value = (0, _utils.collapseWhiteSpaces)(value);
  var guessedType = data.guessIdentifierType(value, types);
  if (guessedType) {
    var result = new RegExp(guessedType.detectionRegex).exec(value);
    value = result[1];
    // 	disabling "add isbn row" feature temporary
    // if (guessedType.id === 9) {
    // 	const isbn10Type:any = types.find((el) => el.id === 10);
    // 	const isbn10 = isbn13To10(value);
    // 	if (isbn10) {
    // 		dispatch(debouncedUpdateIdentifierValue(index + 1, isbn10, isbn10Type, false));
    // 	}
    // }
    // if (guessedType.id === 10) {
    // 	const isbn13Type:any = types.find((el) => el.id === 9);
    // 	const isbn13 = isbn10To13(value);
    // 	if (isbn13) {
    // 		dispatch(debouncedUpdateIdentifierValue(index + 1, isbn10To13(value), isbn13Type, false));
    // 	}
    // }
  }

  return dispatch((0, _actions.debouncedUpdateIdentifierValue)(index, value, guessedType));
}
function mapStateToProps(rootState, _ref2) {
  var index = _ref2.index;
  var state = rootState.get('identifierEditor');
  return {
    typeValue: state.getIn([index, 'type']),
    valueValue: state.getIn([index, 'value'])
  };
}
function mapDispatchToProps(dispatch, _ref3) {
  var index = _ref3.index,
    typeOptions = _ref3.typeOptions;
  return {
    onRemoveButtonClick: function onRemoveButtonClick() {
      return dispatch((0, _actions.removeIdentifierRow)(index));
    },
    onTypeChange: function onTypeChange(value) {
      return dispatch((0, _actions.updateIdentifierType)(index, value && value.value));
    },
    onValueChange: function onValueChange(event) {
      return handleValueChange(dispatch, event, index, typeOptions);
    }
  };
}
var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(IdentifierRow);
exports.default = _default;
//# sourceMappingURL=identifier-row.js.map