Skip to content

Commit

Permalink
fix(BB-537): Fix issue with EditionGroup matching mechanism
Browse files Browse the repository at this point in the history
Made checkIfNameExists an async function instead of promise-based for clarity.
  • Loading branch information
MonkeyDo committed Oct 8, 2020
1 parent 0f30b5c commit 1831eeb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
44 changes: 24 additions & 20 deletions src/client/entity-editor/name-section/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// @flow

import {snakeCase as _snakeCase, isString, remove, uniqBy} from 'lodash';
import log from 'log';
import request from 'superagent';


Expand Down Expand Up @@ -123,7 +124,7 @@ export function checkIfNameExists(
* @function dispatch
* @param {function} dispatch - The redux dispatch function.
*/
return (dispatch) => {
return async (dispatch) => {
if (!name ||
_snakeCase(entityType) === 'edition' ||
(_snakeCase(entityType) === 'edition_group' && action === UPDATE_WARN_IF_EXISTS)
Expand All @@ -134,26 +135,29 @@ export function checkIfNameExists(
});
return;
}
request.get('/search/exists')
.query({
q: name,
type: _snakeCase(entityType)
})
.then(res => {
let payload = JSON.parse(res.text) || null;
if (Array.isArray(payload)) {
payload = uniqBy(payload, 'bbid');
// Filter out the current entity (if any)
if (isString(entityBBID)) {
remove(payload, ({bbid}) => entityBBID === bbid);
}
}
return dispatch({
payload,
type: action || UPDATE_WARN_IF_EXISTS
try {
const res = await request.get('/search/exists')
.query({
q: name,
type: _snakeCase(entityType)
});
})
.catch((error: {message: string}) => error);

let payload = JSON.parse(res.text) || null;
if (Array.isArray(payload)) {
payload = uniqBy(payload, 'bbid');
// Filter out the current entity (if any)
if (isString(entityBBID)) {
remove(payload, ({bbid}) => entityBBID === bbid);
}
}
dispatch({
payload,
type: action || UPDATE_WARN_IF_EXISTS
});
}
catch (error) {
log.error(error);
}
};
}

Expand Down
31 changes: 24 additions & 7 deletions src/client/entity-editor/name-section/name-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class NameSection extends React.Component {
super(props);
this.updateNameFieldInputRef = this.updateNameFieldInputRef.bind(this);
this.handleNameChange = this.handleNameChange.bind(this);
this.searchForMatchindEditionGroups = this.searchForMatchindEditionGroups.bind(this);
}

/*
Expand All @@ -93,10 +94,33 @@ class NameSection extends React.Component {
}
}

componentDidUpdate(prevProps) {
const {nameValue, searchForExistingEditionGroup} = this.props;
if (prevProps.searchForExistingEditionGroup === searchForExistingEditionGroup ||
searchForExistingEditionGroup === false) {
return;
}
this.searchForMatchindEditionGroups(nameValue);
}

handleNameChange(event) {
this.props.onNameChange(event.target.value);
this.props.onNameChangeCheckIfExists(event.target.value);
this.props.onNameChangeSearchName(event.target.value);
this.searchForMatchindEditionGroups(event.target.value);
}

searchForMatchindEditionGroups(nameValue) {
const {
entityType,
onNameChangeCheckIfEditionGroupExists,
searchForExistingEditionGroup
} = this.props;
// Search for Edition Groups that match the name, if the entity is an Edition
// Will react to name changes and searchForExistingEditionGroup (which reacts to editionGroupBBID field)
if (_.toLower(entityType) === 'edition' && searchForExistingEditionGroup && !_.isNil(nameValue)) {
onNameChangeCheckIfEditionGroupExists(nameValue);
}
}

updateNameFieldInputRef(inputRef) {
Expand All @@ -113,10 +137,8 @@ class NameSection extends React.Component {
nameValue,
sortNameValue,
onLanguageChange,
onNameChangeCheckIfEditionGroupExists,
onSortNameChange,
onDisambiguationChange,
searchForExistingEditionGroup,
searchResults
} = this.props;

Expand All @@ -127,11 +149,6 @@ class NameSection extends React.Component {

const warnIfExists = !_.isEmpty(exactMatches);

// Search for Edition Groups that match the name, if the entity is an Edition
// Will react to name changes and searchForExistingEditionGroup (which reacts to editionGroupBBID field)
if (_.toLower(entityType) === 'edition' && searchForExistingEditionGroup) {
onNameChangeCheckIfEditionGroupExists(nameValue);
}

return (
<div>
Expand Down

0 comments on commit 1831eeb

Please sign in to comment.