client/unified-form/validators/cover-tab.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isCoverTabEmpty = isCoverTabEmpty;
exports.validateCoverTab = validateCoverTab;
exports.validateISBN = validateISBN;
require("core-js/modules/es.function.name.js");
var _size2 = _interopRequireDefault(require("lodash/size"));
var _get2 = _interopRequireDefault(require("lodash/get"));
var _common = require("../../entity-editor/validators/common");
/**
 * Validates the ISBN Field.
 *
 * @param {object} isbn - ISBN state object containing `type` and `value`
 * @returns {boolean} - true if valid, false if invalid
 */
function validateISBN(isbn) {
  // since type will already be defined for valid ISBNs
  return !(Boolean(isbn) && !(0, _get2.default)(isbn, 'type', null) && (0, _get2.default)(isbn, 'value', '').length > 0);
}

/**
 * Validates the Cover Tab state.
 *
 * @param {object} data - the form state object
 * @param {Array} identifierTypes - the list of identifier types
 * @returns {boolean} - true if form state valid, false if invalid
 */
function validateCoverTab(data, identifierTypes) {
  return (0, _common.validateNameSection)((0, _get2.default)(data, 'nameSection')) && (0, _common.validateIdentifiers)((0, _get2.default)(data, 'identifierEditor', {}), identifierTypes) && (0, _common.validateAuthorCreditSection)((0, _get2.default)(data, 'authorCreditEditor')) && validateISBN((0, _get2.default)(data, 'ISBN'));
}

/**
 *  Check whether Cover Tab is modified or not.
 *
 * @param {object} data - the form state object
 * @returns {boolean} - true if cover tab state empty
 */
function isCoverTabEmpty(data) {
  var _nameSection$name, _nameSection$sortName, _nameSection$disambig, _authorCreditEditor$n;
  var nameSection = (0, _get2.default)(data, 'nameSection', {});
  var authorCreditEditor = (0, _get2.default)(data, 'authorCreditEditor', {});
  var ISBN = (0, _get2.default)(data, 'ISBN', {});
  var identifierEditor = (0, _get2.default)(data, 'identifierEditor', {});
  return ((_nameSection$name = nameSection.name) === null || _nameSection$name === void 0 ? void 0 : _nameSection$name.length) === 0 && ((_nameSection$sortName = nameSection.sortName) === null || _nameSection$sortName === void 0 ? void 0 : _nameSection$sortName.length) === 0 && !nameSection.language && ((_nameSection$disambig = nameSection.disambiguation) === null || _nameSection$disambig === void 0 ? void 0 : _nameSection$disambig.length) === 0 && (0, _size2.default)(authorCreditEditor) === 1 && !((_authorCreditEditor$n = authorCreditEditor.n0) !== null && _authorCreditEditor$n !== void 0 && _authorCreditEditor$n.author) && (0, _size2.default)(identifierEditor) === 0 && !ISBN.type;
}
//# sourceMappingURL=cover-tab.js.map