Skip to content

Commit

Permalink
ORCID for collaborators/contributors (#588)
Browse files Browse the repository at this point in the history
* Fixes bug with ORCID fetching for contributors

* Added a couple of tests

* Linting errors

* Minor comment tweak
  • Loading branch information
hectorcorrea committed Nov 2, 2022
1 parent 1eaa0be commit d47e631
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 27 deletions.
54 changes: 31 additions & 23 deletions app/assets/javascripts/edit_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ Vite wraps it as an ESM, and there doesn't seem to be a way to specify
an export that we can call as-needed.
*/

console.log('edit_utils.js loaded...');
$(() => {
console.log('edit_utils.js hooks loading...');
function incrementCounter(elementId) {
let counter = parseInt($(elementId)[0].value, 10);
counter++;
Expand All @@ -34,7 +32,7 @@ $(() => {
const sequenceId = `sequence_${num}`;
const rowHtml = `<tr id="${rowId}" class="creators-table-row">
<td>
<input class="orcid-entry" type="text" id="${orcidId}" name="${orcidId}" value="${orcid}" data-num="${num}" placeholder="0000-0000-0000-0000" />
<input class="orcid-entry-creator" type="text" id="${orcidId}" name="${orcidId}" value="${orcid}" data-num="${num}" placeholder="0000-0000-0000-0000" />
</td>
<td>
<input type="text" id="${givenNameId}" name="${givenNameId}" value="${givenName}" />
Expand Down Expand Up @@ -113,7 +111,7 @@ $(() => {

const rowHtml = `<tr id="${rowId}" class="contributors-table-row">
<td>
<input class="orcid-entry" type="text" id="${orcidId}" name="${orcidId}" value="${orcid}" data-num="${num}" placeholder="0000-0000-0000-0000" />
<input class="orcid-entry-collaborator" type="text" id="${orcidId}" name="${orcidId}" value="${orcid}" data-num="${num}" placeholder="0000-0000-0000-0000" />
</td>
<td>
<input type="text" id="${givenNameId}" name="${givenNameId}" value="${givenName}" />
Expand Down Expand Up @@ -279,6 +277,25 @@ $(() => {
$(`#family_name_${suffix}`).val(familyName);
}

// Fetch information via ORCID's public API and dumps the data into the elements indicated.
function fetchOrcid(orcidValue, givenNameId, familyNameId) {
if (isOrcid(orcidValue)) {
$.ajax({
url: `${pdc.orcid_url}/${orcidValue}`,
dataType: 'jsonp',
})
.done((data) => {
const givenName = data.person.name['given-names'].value;
const familyName = data.person.name['family-name'].value;
$(givenNameId).val(givenName);
$(familyNameId).val(familyName);
})
.fail((XMLHttpRequest, textStatus, errorThrown) => {
console.log(`Error fetching ORCID for ${errorThrown}`);
});
}
}

$('#btn-add-creator').on('click', (el) => {
const num = incrementCounter('#creator_count');
addCreatorHtml(num, '', '', '');
Expand Down Expand Up @@ -424,27 +441,18 @@ $(() => {
}
}

// Fetch name information for a given ORCID via ORCID's public API
$(document).on('input', '.orcid-entry', (el) => {
// Fetch information for a creator via ORCID's public API
$(document).on('input', '.orcid-entry-creator', (el) => {
const num = el.target.attributes['data-num'].value;
const orcid = $(el.target).val().trim();
if (isOrcid(orcid)) {
$.ajax({
url: `${pdc.orcid_url}/${orcid}`,
dataType: 'jsonp',
})
.done((data) => {
const givenName = data.person.name['given-names'].value;
const familyName = data.person.name['family-name'].value;
const givenNameId = `#given_name_${num}`;
const familyNameId = `#family_name_${num}`;
$(givenNameId).val(givenName);
$(familyNameId).val(familyName);
})
.fail((XMLHttpRequest, textStatus, errorThrown) => {
console.log(`Error fetching ORCID for ${errorThrown}`);
});
}
fetchOrcid(orcid, `#given_name_${num}`, `#family_name_${num}`);
});

// Fetch information for a collaborator/contributor via ORCID's public API
$(document).on('input', '.orcid-entry-collaborator', (el) => {
const num = el.target.attributes['data-num'].value;
const orcid = $(el.target).val().trim();
fetchOrcid(orcid, `#contributor_given_name_${num}`, `#contributor_family_name_${num}`);
});

// Drop the "http..."" portion of the URL if the user enters the full URL of a DataSpace ARK
Expand Down
29 changes: 25 additions & 4 deletions spec/support/orcid_specs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This sets up a little local server to respond to ORCID ajax requests
# This is utilized to test the javascript that parses the ajax response and puts the ORCID family name and given name into the form
# The data returned is a simplified version of the data returned from ORCID. If more data than just the name is needed the data here will need to be updated
# Given any ORCID the response value is hard coded to Sally Smith
# The response is hard-coded for a few known values and defaults to "Sally Smith" for all others.
#
# This Fake was modeled on the information in this article: https://thoughtbot.com/blog/using-capybara-to-test-javascript-that-makes-http
class FakeOrcidIntegration < Sinatra::Base
Expand All @@ -16,9 +16,30 @@ def self.boot
get "/:orcid" do |orcid|
content_type(:js)
callback = params[:callback]
data = { "orcid-identifier" => { "uri" => "http://orcid.org/#{orcid}", "path" => orcid, "host" => "orcid.org" },
"person" => { "name" => { "given-names" => { "value" => "Sally" }, "family-name" => { "value" => "Smith" } } } }.to_json
"#{callback}(#{data});"
data = {
"orcid-identifier" => {
"uri" => "http://orcid.org/#{orcid}",
"path" => orcid,
"host" => "orcid.org"
},
"person" => {
"name" => {
"given-names" => { "value" => "Sally" },
"family-name" => { "value" => "Smith" }
}
}
}

case orcid
when "0000-0001-8965-6820"
data["person"]["name"]["given-names"]["value"] = "Carmen"
data["person"]["name"]["family-name"]["value"] = "Valdez"
when "0000-0001-5443-5964"
data["person"]["name"]["given-names"]["value"] = "Melody"
data["person"]["name"]["family-name"]["value"] = "Loya"
end

"#{callback}(#{data.to_json});"
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/system/work_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,26 @@
expect(page.find(".activity-history-log-title", visible: false).tag_name).to eq "div"
end
end

context "ORCID information" do
let(:work) { FactoryBot.create(:draft_work) }
let(:user) { work.created_by_user }

it "fetches information for creators" do
sign_in user
visit edit_work_path(work)
fill_in "orcid_1", with: "0000-0001-8965-6820"
expect(page.find("#given_name_1").value).to eq "Carmen"
expect(page.find("#family_name_1").value).to eq "Valdez"
end

it "fetches information for contributors" do
sign_in user
visit edit_work_path(work)
click_on "Additional Metadata"
fill_in "contributor_orcid_1", with: "0000-0001-5443-5964"
expect(page.find("#contributor_given_name_1").value).to eq "Melody"
expect(page.find("#contributor_family_name_1").value).to eq "Loya"
end
end
end

0 comments on commit d47e631

Please sign in to comment.