diff --git a/app/controllers/contributors_controller.rb b/app/controllers/contributors_controller.rb index 410b95fd37..96a39f4664 100644 --- a/app/controllers/contributors_controller.rb +++ b/app/controllers/contributors_controller.rb @@ -114,13 +114,16 @@ def translate_roles(hash:) def process_org(hash:) return hash unless hash.present? && hash[:org_id].present? - allow = !Rails.configuration.x.application.restrict_orgs - org = org_from_params(params_in: hash, - allow_create: allow) + org = org_from_params(params_in: hash, allow_create: true) + + if org.nil? + flash[:alert] = + _('Contributor saved without affiliation. If you intended to add an affiliation, please check ' \ + 'if the organisation appears in the list in a different form.') + end hash = remove_org_selection_params(params_in: hash) - return hash if org.blank? && !allow return hash unless org.present? hash[:org_id] = org.id diff --git a/app/javascript/src/utils/autoComplete.js b/app/javascript/src/utils/autoComplete.js index 49f8b37d50..ecdb106fc6 100644 --- a/app/javascript/src/utils/autoComplete.js +++ b/app/javascript/src/utils/autoComplete.js @@ -1,6 +1,7 @@ import 'jquery-ui/ui/widgets/autocomplete'; import getConstant from './constants'; import { isObject, isString, isArray } from './isType'; +import debounce from '../utils/debounce'; // Updates the ARIA help text that lets the user know how many suggestions const updateAriaHelper = (autocomplete, suggestionCount) => { @@ -94,6 +95,12 @@ const toggleWarning = (autocomplete, displayIt) => { } }; +// Delayed warning display (fires only after typing pauses) +const debouncedToggleWarning = debounce((autocomplete, displayIt) => { + toggleWarning(autocomplete, displayIt); +}, 1000); + + // Looks up the value in the crosswalk const findInCrosswalk = (selection, crosswalk) => { // Default to the name only @@ -123,7 +130,11 @@ const warnableSelection = (selection) => { const handleSelection = (autocomplete, hidden, crosswalk, selection) => { const out = findInCrosswalk(selection, crosswalk); - toggleWarning(autocomplete, warnableSelection(out)); + // When user types, 'displayIt = false' hides warning initially + toggleWarning(autocomplete, false); + + // After user pauses for 1 second, show warning + debouncedToggleWarning(autocomplete, warnableSelection(out)); // Set the ID and trigger the onChange event for any view specific // JS to trigger events diff --git a/app/services/org_selection/hash_to_org_service.rb b/app/services/org_selection/hash_to_org_service.rb index a301d5faaa..6769363307 100644 --- a/app/services/org_selection/hash_to_org_service.rb +++ b/app/services/org_selection/hash_to_org_service.rb @@ -63,6 +63,13 @@ def to_identifiers(hash:) private + def match_hash_to_ror_org(hash:) + return nil unless hash[:ror].present? + + ror_results = OrgSelection::SearchService.search_externally(search_term: hash[:name]) + ror_results&.find { |r| r[:ror] == hash[:ror] } + end + # Lookup the Org by it's :id and return if the name matches the search def lookup_org_by_id(hash:) org = Org.where(id: hash[:id]).first if hash[:id].present? @@ -92,14 +99,18 @@ def lookup_org_by_name(hash:) def initialize_org(hash:) return nil unless hash.present? && hash[:name].present? + # Attempt to find an ROR match to the hash + ror_hash = match_hash_to_ror_org(hash: hash) + return nil unless ror_hash + Org.new( - name: hash[:name], - links: links_from_hash(name: hash[:name], website: hash[:url]), - language: language_from_hash(hash: hash), - target_url: hash[:url], + name: ror_hash[:name], + links: links_from_hash(name: ror_hash[:name], website: ror_hash[:url]), + language: language_from_hash(hash: ror_hash), + target_url: ror_hash[:url], institution: true, is_other: false, - abbreviation: abbreviation_from_hash(hash: hash) + abbreviation: abbreviation_from_hash(hash: ror_hash) ) end diff --git a/app/views/contributors/_form.html.erb b/app/views/contributors/_form.html.erb index 4c9ef30562..b70de31191 100644 --- a/app/views/contributors/_form.html.erb +++ b/app/views/contributors/_form.html.erb @@ -56,9 +56,8 @@ roles_tooltip = _("Select each role that applies to the contributor.")