Skip to content

Commit

Permalink
Use the Fetch API instead of getJSON part of #3913
Browse files Browse the repository at this point in the history
  • Loading branch information
christinach committed Dec 28, 2023
1 parent 77833fc commit 88f0912
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions app/javascript/orangelight/availability.es6
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ export default class AvailabilityUpdater {
for(let i= 0; i < batches.length; i++) {
const batch_url = `${this.bibdata_base_url}/bibliographic/availability.json?bib_ids=${batches[i].join()}`;
console.log(`batch: ${i}, url: ${batch_url}`);
$.getJSON(batch_url, this.process_results_list)
.fail((jqXHR, _textStatus, errorThrown) => {
// Log that there were problems fetching a batch. Unfortunately we don't know exactly
// which batch so we cannot include that information.
console.error(`Failed to retrieve availability data for batch. HTTP status: ${jqXHR.status}: ${errorThrown}`);
});
fetch(batch_url)
.then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}).then((response) => response.json())
.then((data) => this.process_results_list(data))
.catch(error => {
console.log(`Failed to retrieve availability data for batch. Error: ${error}`);
})
}

// a show page
Expand All @@ -56,29 +61,45 @@ export default class AvailabilityUpdater {
this.host_id = $("#main-content").data("host-id") || "";
if (this.id.match(/^SCSB-\d+/)) {
url = `${this.availability_url}?scsb_id=${this.id.replace(/^SCSB-/, '')}`;
return $.getJSON(url, this.process_scsb_single)
.fail((jqXHR, textStatus, errorThrown) => {
return console.error(`Failed to retrieve availability data for the SCSB record ${this.id}: ${errorThrown}`);
});

return fetch(url)
.then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}).then((response) => response.json())
.catch(error => {
console.log(`Failed to retrieve availability data for the SCSB record ${this.id}: ${error}`);
this.update_availability_undetermined();
})
} else {
return $.getJSON(this.availability_url_show(), this.process_single)
.fail((jqXHR, textStatus, errorThrown) => {
if (jqXHR.status == 429) {
fetch(this.availability_url_show())
.then(function(response) {
if (response.status === 404) {
console.log(allowRetry)
console.log(this)
if (allowRetry) {
console.log(allowRetry)
console.log(this)
console.log(`Retrying availability for record ${this.id}`);
window.setTimeout(() => {
this.update_availability_retrying();
this.request_availability(false);
}, 1500);
} else {
console.error(`Failed to retrieve availability data for the bib (retry). Record ${this.id}: ${errorThrown}`);
this.update_availability_undetermined();
}
return;
}, 1500); }
// } else {
// throw Error(response.statusText);
// }
}
else if (!response.ok && response.status !== 429 ) {
throw Error(response.statusText);
}
return console.error(`Failed to retrieve availability data for the bib. record ${this.id}: ${errorThrown}`);
});
return response;
}).then((response) => response.json())
.then((data) => this.process_single(data))
.catch(error => {
console.log(`Failed to retrieve availability data for record ID ${this.id}: ${error}`);
this.update_availability_undetermined();
})
}
}
}
Expand All @@ -98,10 +119,17 @@ export default class AvailabilityUpdater {
if (barcodes.length < 1) { return; }
const params = $.param({barcodes});
const url = `${this.availability_url}?${params}`;
return $.getJSON(url, this.process_barcodes)
.fail((jqXHR, textStatus, errorThrown) => {
return console.error(`Failed to retrieve availability data for the SCSB barcodes ${barcodes.join(", ")}: ${errorThrown}`);
});
return fetch(url)
.then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}).then((response) => response.json())
.then((data) => this.process_barcodes(data))
.catch(error => {
console.log(`Failed to retrieve availability data for the SCSB barcodes ${barcodes.join(", ")}: ${error}`);
})
}
}

Expand Down

0 comments on commit 88f0912

Please sign in to comment.