Skip to content
Snippets Groups Projects
Commit d8886468 authored by David Planella's avatar David Planella
Browse files

Implemented API pagination with fetch

parent c37fb8da
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -21,31 +21,34 @@ var hackathonIssueID = '14';
var apiHost = 'https://gitlab.com/api/v4';
var gitlabProjectID = '9821951';
var apiEndpoint = '/projects/' + gitlabProjectID + '/issues/' + hackathonIssueID + '/related_merge_requests';
// TODO: needs pagination
var apiPagination = '?per_page=100';
var apiURL = apiHost + apiEndpoint + apiPagination;
var apiURL = apiHost + apiEndpoint;
 
var request = new XMLHttpRequest();
const limitPerPage = 100;
 
request.open('GET', apiURL, true);
request.onload = function () {
const getRelatedMRs = async function(pageNo = 1) {
 
var data = JSON.parse(this.response);
var mrCount = 0;
var boxMessage = '';
if (request.status >= 200 && request.status < 400) {
data.forEach(mr => {
mrCount += 1;
});
boxMessage = mrCount + ' MRs submitted '
+ ' at the last Hackathon ';
let apiPaginatedURL = apiURL + `?page=${pageNo}&per_page=${limitPerPage}`;
var apiResults = await fetch(apiPaginatedURL)
.then(resp=>{
return resp.json();
});
return apiResults;
}
 
const getAllRelatedMRs = async function(pageNo = 1) {
const results = await getRelatedMRs(pageNo);
if (results.length > 0) {
return results.concat(await getAllRelatedMRs(pageNo + 1));
} else {
boxMessage = 'Error fetching merge requests';
return results;
}
};
 
document.getElementById('hackathonMRCount').innerHTML = boxMessage;
}
(async ()=>{
const entireList = await getAllRelatedMRs();
var boxMessage = entireList.length + ' MRs submitted '
+ ' at the last Hackathon!';
document.getElementById('hackathonMRCount').innerHTML = boxMessage;
 
request.send();
\ No newline at end of file
})();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment