"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.function.name.js");
require("core-js/modules/es.array.includes.js");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _actions = require("./actions");
var _reactBootstrap = require("react-bootstrap");
var _entitySearchFieldOption = _interopRequireDefault(require("../common/entity-search-field-option"));
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _react = _interopRequireDefault(require("react"));
var _searchEntityCreateSelect = _interopRequireDefault(require("../../unified-form/common/search-entity-create-select"));
var _validationLabel = _interopRequireDefault(require("../common/validation-label"));
var _action = require("../../unified-form/cover-tab/action");
var _reactRedux = require("react-redux");
var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");
var _excluded = ["index", "author", "joinPhrase", "name", "isUnifiedForm", "onAuthorChange", "onJoinPhraseChange", "onClearHandler", "onNameChange", "onRemoveButtonClick"];
/*
* Copyright (C) 2020 Sean Burke
*
* 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 {string} props.index - The index of the row in the parent editor (i.e. 'n0')
* @param {string} props.author - The ID of the type currently selected.
* @param {string} props.joinPhrase - The ID of the type currently selected.
* @param {string} props.name - The value currently set for this
* identifier.
* @param {Function} props.onAuthorChange - A function to be called when a new
* identifier type is selected.
* @param {Function} props.onJoinPhraseChange - A function to be called when the
* value for the identifier is changed.
* @param {Function} props.onNameChange - A function to be called when the
* value for the identifier is changed.
* @param {Function} props.onRemoveButtonClick - A function to be called when
* the button to remove the identifier is clicked.
* @returns {ReactElement} React element containing the rendered IdentifierRow.
*/
function AuthorCreditRow(_ref) {
var index = _ref.index,
author = _ref.author,
joinPhrase = _ref.joinPhrase,
name = _ref.name,
isUnifiedForm = _ref.isUnifiedForm,
onAuthorChange = _ref.onAuthorChange,
onJoinPhraseChange = _ref.onJoinPhraseChange,
onClearHandler = _ref.onClearHandler,
onNameChange = _ref.onNameChange,
onRemoveButtonClick = _ref.onRemoveButtonClick,
rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
var SelectWrapper = !isUnifiedForm ? _entitySearchFieldOption.default : _searchEntityCreateSelect.default;
var onChangeHandler = _react.default.useCallback(function (value, action) {
if (['clear', 'pop-value', 'select-option'].includes(action === null || action === void 0 ? void 0 : action.action) && author !== null && author !== void 0 && author.get('__isNew__', false)) {
onClearHandler(author.get('id'));
}
onAuthorChange(value);
}, [author, onAuthorChange, onClearHandler]);
var handleButtonClick = _react.default.useCallback(function () {
if (author !== null && author !== void 0 && author.get('__isNew__', false)) {
onClearHandler(author.get('id'));
}
onRemoveButtonClick();
}, [author, index, onClearHandler, onRemoveButtonClick]);
return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Row, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
md: {
span: 3
}
}, /*#__PURE__*/_react.default.createElement(SelectWrapper, (0, _extends2.default)({
instanceId: "author".concat(index),
isUnifiedForm: isUnifiedForm,
label: "Author",
rowId: index,
validationState: !author ? 'error' : null,
value: author,
onChange: onChangeHandler
}, rest, {
type: "author"
}))), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
md: {
span: 3
}
}, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Form.Label, null, /*#__PURE__*/_react.default.createElement(_validationLabel.default, {
empty: name.length === 0,
error: !name.length
}, "Author as credited")), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Form.Control, {
type: "text",
value: name,
onChange: onNameChange
}))), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
md: {
span: 3
}
}, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Form.Label, null, "Join Phrase"), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Form.Control, {
type: "text",
value: joinPhrase,
onChange: onJoinPhraseChange
}))), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, {
md: {
span: 3
}
}, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Button, {
block: true,
className: "margin-top-d18",
variant: "danger",
onClick: handleButtonClick
}, /*#__PURE__*/_react.default.createElement(_reactFontawesome.FontAwesomeIcon, {
icon: _freeSolidSvgIcons.faTimes
}), "\xA0Remove"))), /*#__PURE__*/_react.default.createElement("hr", null));
}
AuthorCreditRow.displayName = 'AuthorCreditEditor.CreditRow';
AuthorCreditRow.defaultProps = {
isUnifiedForm: false
};
function mapStateToProps(rootState, _ref2) {
var index = _ref2.index;
var state = rootState.get('authorCreditEditor');
return {
author: state.getIn([index, 'author']),
joinPhrase: state.getIn([index, 'joinPhrase']),
name: state.getIn([index, 'name'])
};
}
function mapDispatchToProps(dispatch, _ref3) {
var index = _ref3.index;
return {
onAuthorChange: function onAuthorChange(value) {
return dispatch((0, _actions.updateCreditAuthorValue)(index, value));
},
onClearHandler: function onClearHandler(aid) {
return dispatch((0, _action.clearAuthor)(aid));
},
onJoinPhraseChange: function onJoinPhraseChange(event) {
return dispatch((0, _actions.updateCreditJoinPhraseValue)(index, event.target.value));
},
onNameChange: function onNameChange(event) {
return dispatch((0, _actions.updateCreditDisplayValue)(index, event.target.value));
},
onRemoveButtonClick: function onRemoveButtonClick() {
return dispatch((0, _actions.removeAuthorCreditRow)(index));
}
};
}
var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(AuthorCreditRow);
exports.default = _default;
//# sourceMappingURL=author-credit-row.js.map