-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchAndProcessData.js
48 lines (44 loc) · 1.96 KB
/
fetchAndProcessData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { toQueryParams } from "./uriHelper.js";
import { queryBuilder } from "./queryBuilder.js";
import { makeAjaxRequest } from "./ajaxHelper.js";
import { family } from "./globals.js";
import { processTargets } from "./nodesAndEdges.js";
import { endpointURL } from "./globals.js";
export async function fetchAndProcessTargets(conceptRDFUri, conceptId, conceptLabel, iYear, targetYear) {
$("#spinner").show();
const isBackward = iYear > targetYear;
// Data to be sent as query parameters
const callerId = isBackward ? "pastConcepts" : "futureConcepts";
const q = { query: queryBuilder( callerId, family, conceptRDFUri, '', conceptId) };
const queryParams = toQueryParams(q);
return new Promise((resolve, reject) => {
makeAjaxRequest(
`${endpointURL}?${queryParams}`, // Include query parameters in the URL
"GET", // Change to GET method
{
Accept: "application/sparql-results+json",
"Content-Type": "application/x-www-form-urlencoded",
},
{}, // No data in the body for GET request
function (data) {
try {
const newTargets = processTargets(data, conceptId, conceptLabel, iYear, targetYear);
resolve(newTargets); // Resolve promise with new targets
} catch (e) {
console.error("Failed to process response data:", e, "Response data:", data,
"Parameters:", {conceptId, conceptLabel, iYear, targetYear});
reject(new Error(`Invalid JSON response for conceptId: ${conceptId}, conceptLabel: ${conceptLabel}, iYear: ${iYear}, targetYear: ${targetYear}`));
} finally {
$("#spinner").hide();
}
},
function (fetchError) {
console.error("AJAX request error:", conceptRDFUri);
$("#spinner").hide();
document.getElementById('errorContainer').innerText = fetchError;
reject(new Error(fetchError)); // Reject promise with error
},
`${conceptId}-${iYear}-${targetYear}`
);
});
}