client/entity-editor/name-section/actions.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.UPDATE_WARN_IF_EXISTS = exports.UPDATE_SORT_NAME_FIELD = exports.UPDATE_SEARCH_RESULTS = exports.UPDATE_NAME_FIELD = exports.UPDATE_LANGUAGE_FIELD = exports.UPDATE_DISAMBIGUATION_FIELD = void 0;
exports.checkIfNameExists = checkIfNameExists;
exports.debouncedUpdateDisambiguationField = debouncedUpdateDisambiguationField;
exports.debouncedUpdateNameField = debouncedUpdateNameField;
exports.debouncedUpdateSortNameField = debouncedUpdateSortNameField;
exports.searchName = searchName;
exports.updateLanguageField = updateLanguageField;
exports.updateNameField = updateNameField;
exports.updateSortNameField = updateSortNameField;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _uniqBy2 = _interopRequireDefault(require("lodash/uniqBy"));
var _remove2 = _interopRequireDefault(require("lodash/remove"));
var _isString2 = _interopRequireDefault(require("lodash/isString"));
var _snakeCase3 = _interopRequireDefault(require("lodash/snakeCase"));
var _superagent = _interopRequireDefault(require("superagent"));
/*
 * 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.
 */

var UPDATE_DISAMBIGUATION_FIELD = 'UPDATE_DISAMBIGUATION_FIELD';
exports.UPDATE_DISAMBIGUATION_FIELD = UPDATE_DISAMBIGUATION_FIELD;
var UPDATE_LANGUAGE_FIELD = 'UPDATE_LANGUAGE_FIELD';
exports.UPDATE_LANGUAGE_FIELD = UPDATE_LANGUAGE_FIELD;
var UPDATE_NAME_FIELD = 'UPDATE_NAME_FIELD';
exports.UPDATE_NAME_FIELD = UPDATE_NAME_FIELD;
var UPDATE_SORT_NAME_FIELD = 'UPDATE_SORT_NAME_FIELD';
exports.UPDATE_SORT_NAME_FIELD = UPDATE_SORT_NAME_FIELD;
var UPDATE_WARN_IF_EXISTS = 'UPDATE_WARN_IF_EXISTS';
exports.UPDATE_WARN_IF_EXISTS = UPDATE_WARN_IF_EXISTS;
var UPDATE_SEARCH_RESULTS = 'UPDATE_SEARCH_RESULTS';
exports.UPDATE_SEARCH_RESULTS = UPDATE_SEARCH_RESULTS;
/**
 * Produces an action indicating that the name for the entity being edited
 * should be updated with the provided value.
 *
 * @param {string} newName - The new value to be used for the name.
 * @returns {Action} The resulting UPDATE_NAME_FIELD action.
 */
function updateNameField(newName) {
  return {
    payload: newName,
    type: UPDATE_NAME_FIELD
  };
}

/**
 * Produces an action indicating that the sort name for the entity being edited
 * should be updated with the provided value.
 *
 * @param {string} newSortName - The new value to be used for the sort name.
 * @returns {Action} The resulting UPDATE_SORT_NAME_FIELD action.
 */
function updateSortNameField(newSortName) {
  return {
    payload: newSortName,
    type: UPDATE_SORT_NAME_FIELD
  };
}

/**
 * Produces an action indicating that the name for the entity being edited
 * should be updated with the provided value. The action is marked to be
 * debounced by the keystroke debouncer defined for redux-debounce.
 *
 * @param {string} newName - The new value to be used for the name.
 * @returns {Action} The resulting UPDATE_NAME_FIELD action.
 */
function debouncedUpdateNameField(newName) {
  return {
    meta: {
      debounce: 'keystroke'
    },
    payload: newName,
    type: UPDATE_NAME_FIELD
  };
}

/**
 * Produces an action indicating that the sort name for the entity being edited
 * should be updated with the provided value. The action is marked to be
 * debounced by the keystroke debouncer defined for redux-debounce.
 *
 * @param {string} newSortName - The new value to be used for the sort name.
 * @returns {Action} The resulting UPDATE_SORT_NAME_FIELD action.
 */
function debouncedUpdateSortNameField(newSortName) {
  return {
    meta: {
      debounce: 'keystroke'
    },
    payload: newSortName,
    type: UPDATE_SORT_NAME_FIELD
  };
}

/**
 * Produces an action indicating that the language of the name for the entity
 * being edited should be updated with the provided value.
 *
 * @param {string} newLanguageId - The new value to be used for the language ID.
 * @returns {Action} The resulting UPDATE_LANGUAGE_FIELD action.
 */
function updateLanguageField(newLanguageId) {
  return {
    payload: newLanguageId,
    type: UPDATE_LANGUAGE_FIELD
  };
}

/**
 * Produces an action indicating that the disambiguation for the entity being
 * edited should be updated with the provided value. The action is marked to be
 * debounced by the keystroke debouncer defined for redux-debounce.
 *
 * @param {string} newDisambiguation - The new value to be used for the
 *        disambiguation.
 * @returns {Action} The resulting UPDATE_SORT_NAME_FIELD action.
 */
function debouncedUpdateDisambiguationField(newDisambiguation) {
  return {
    meta: {
      debounce: 'keystroke'
    },
    payload: newDisambiguation,
    type: UPDATE_DISAMBIGUATION_FIELD
  };
}

/**
 * Produces an action containing boolean value indicating if the name of the
 * entity already exists. This is done by asynchronously checking if the name
 * of the entity already exists in the database.
 *
 * @param  {string} name - The value to be checked if it already exists.
 * @param  {string} entityBBID - The BBID of the current entity, if it already exists
 * @param  {string} entityType - The entity type of the value to be checked.
 * @param  {string} action - An optional redux action to dispatch. Defaults to UPDATE_WARN_IF_EXISTS
 * @returns {checkIfNameExists~dispatch} The returned function.
 */
function checkIfNameExists(name, entityBBID, entityType, action) {
  /**
   * @function dispatch
   * @param  {function} dispatch - The redux dispatch function.
   */
  return /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(dispatch) {
      var res, payload;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!(!name || (0, _snakeCase3.default)(entityType) === 'edition' || (0, _snakeCase3.default)(entityType) === 'edition_group' && action === UPDATE_WARN_IF_EXISTS)) {
                _context.next = 3;
                break;
              }
              dispatch({
                payload: null,
                type: action || UPDATE_WARN_IF_EXISTS
              });
              return _context.abrupt("return");
            case 3:
              _context.prev = 3;
              _context.next = 6;
              return _superagent.default.get('/search/exists').query({
                q: name,
                type: (0, _snakeCase3.default)(entityType)
              });
            case 6:
              res = _context.sent;
              payload = JSON.parse(res.text) || null;
              if (Array.isArray(payload)) {
                payload = (0, _uniqBy2.default)(payload, 'bbid');
                // Filter out the current entity (if any)
                if ((0, _isString2.default)(entityBBID)) {
                  (0, _remove2.default)(payload, function (_ref2) {
                    var bbid = _ref2.bbid;
                    return entityBBID === bbid;
                  });
                }
              }
              dispatch({
                payload: payload,
                type: action || UPDATE_WARN_IF_EXISTS
              });
              _context.next = 15;
              break;
            case 12:
              _context.prev = 12;
              _context.t0 = _context["catch"](3);
              // eslint-disable-next-line no-console
              console.error(_context.t0);
            case 15:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[3, 12]]);
    }));
    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }();
}

/**
 * Produces an action containing search results of the name entered by the user.
 *  This is done by asynchronously calling the route /search/autocomplete.
 *
 * @param  {string} name - The value to be checked if it already exists.
 * @param  {string} entityBBID - The BBID of the current entity, if it already exists
 * @param  {string} type - Entity type of the name.
 * @returns {searchName~dispatch} The returned function.
 */
function searchName(name, entityBBID, type) {
  /**
   * @function dispatch
   * @param  {function} dispatch - The redux dispatch function.
   */
  return function (dispatch) {
    if (!name) {
      dispatch({
        payload: null,
        type: UPDATE_SEARCH_RESULTS
      });
      return;
    }
    _superagent.default.get('/search/autocomplete').query({
      q: name,
      size: 3,
      type: type
    }).then(function (res) {
      var searchResults = JSON.parse(res.text);
      // Filter out the current entity (if any)
      if ((0, _isString2.default)(entityBBID)) {
        (0, _remove2.default)(searchResults, function (_ref3) {
          var bbid = _ref3.bbid;
          return entityBBID === bbid;
        });
      }
      dispatch({
        payload: searchResults,
        type: UPDATE_SEARCH_RESULTS
      });
    });
  };
}
//# sourceMappingURL=actions.js.map