Skip to content

Commit 33736b3

Browse files
committed
add DOI search
1 parent a24b756 commit 33736b3

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

  • app/javascript/src/researchOutputs

app/javascript/src/researchOutputs/form.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import getConstant from '../utils/constants';
22
import { isUndefined, isObject } from '../utils/isType';
33
import { Tinymce } from '../utils/tinymce.js';
4+
import { renderNotice, renderAlert } from '../utils/notificationHelper';
5+
import { scrollTo } from '../utils/scrollTo';
6+
47

58
$(() => {
69
const form = $('.research_output_form');
@@ -42,4 +45,86 @@ $(() => {
4245
}
4346
}
4447
});
48+
49+
// GET request using DOI
50+
$('#fetch-info-button').on('click', (e) => {
51+
e.preventDefault();
52+
var doi_url = $('#research_output_doi_url').val();
53+
doi_url = doi_url.trim();
54+
55+
// Method to extract DOI from full URL
56+
function extractDOI(doi_url) {
57+
// Regular expression to match DOI in URL
58+
const doiRegex = /(10\.\d{4,}\/[\S]+)\b/i;
59+
// Extract DOI using regex
60+
const doiMatch = doi_url.match(doiRegex);
61+
62+
// Check if there's a match and return the DOI
63+
if (doiMatch) {
64+
return doiMatch[1]; // Return the first capturing group
65+
} else {
66+
return null; // Return null if no DOI found
67+
}
68+
}
69+
// Format DOI before AJAx call
70+
var doi = extractDOI(doi_url);
71+
72+
// AJAX call
73+
$.ajax({
74+
url: '/open_aire_service/search_proxy/' + doi,
75+
type: 'GET',
76+
headers: { 'Accept': 'application/*' }
77+
}).done((xmlString) => {
78+
79+
if (xmlString == null) {
80+
// Display fail notice
81+
renderAlert('DOI entered returned an empty response. Please check if you have entered the DOI in the correct format i.e. 10.1234/ijdc.123456.', { className: 'alert-warning' });
82+
scrollTo('#notification-area');
83+
} else { // Parsing XML
84+
// Setting Type
85+
var type = $(xmlString).find('resulttype > classname').first().text();
86+
if (type) {
87+
$('#research_output_output_type').val(type);
88+
} else {
89+
//else block
90+
}
91+
92+
// Setting Title as main title from response
93+
var title = $(xmlString).find('title > classid').filter(function () {
94+
return $(this).text() === 'main title';
95+
}).siblings('__content__').first().text();
96+
if (title) {
97+
$('#research_output_title').val(title);
98+
} else {
99+
// else block
100+
}
101+
102+
// Setting Description
103+
var description = $(xmlString).find('description').first().text();
104+
if (description) {
105+
var description_with_html = '<p>' + description + '</p>';
106+
var iframe_content = $('#research_output_description_ifr').contents().find('body#tinymce');
107+
iframe_content.html(description_with_html);
108+
} else {
109+
// else block
110+
}
111+
112+
113+
// Setting Release date
114+
var date = $(xmlString).find('dateofacceptance:first').first().text();
115+
if (date) {
116+
$('#research_output_release_date').val(date);
117+
} else {
118+
// else block
119+
}
120+
121+
$('#research_output_doi_url').val(doi);
122+
// Display success notice
123+
renderNotice('Successfully retrieved the research output\'s metadata.', { className: 'alert-info' });
124+
}
125+
}).fail((xhr, status, error) => {
126+
// Handle errors
127+
alert('An error occurred while processing your request. Please try again later.');
128+
});
129+
});
45130
});

0 commit comments

Comments
 (0)