2018-02-01 01:00:11 +01:00
|
|
|
const CLAIMS_PER_PAGE = 12;
|
2017-12-07 21:23:21 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2018-02-01 01:00:11 +01:00
|
|
|
returnPaginatedChannelClaims (channelName, longChannelClaimId, claims, page) {
|
2017-12-07 21:23:21 +01:00
|
|
|
const totalPages = module.exports.determineTotalPages(claims);
|
2018-01-30 20:46:22 +01:00
|
|
|
const paginationPage = module.exports.getPageFromQuery(page);
|
2018-04-27 08:42:22 +02:00
|
|
|
return {
|
2018-02-01 01:00:11 +01:00
|
|
|
channelName : channelName,
|
|
|
|
longChannelClaimId: longChannelClaimId,
|
|
|
|
claims : module.exports.extractPageFromClaims(claims, paginationPage),
|
|
|
|
previousPage : module.exports.determinePreviousPage(paginationPage),
|
|
|
|
currentPage : paginationPage,
|
|
|
|
nextPage : module.exports.determineNextPage(totalPages, paginationPage),
|
|
|
|
totalPages : totalPages,
|
|
|
|
totalResults : module.exports.determineTotalClaims(claims),
|
2017-12-07 21:23:21 +01:00
|
|
|
};
|
|
|
|
},
|
2018-01-30 20:46:22 +01:00
|
|
|
getPageFromQuery (page) {
|
|
|
|
if (page) {
|
|
|
|
return parseInt(page);
|
2017-12-07 21:23:21 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
extractPageFromClaims (claims, pageNumber) {
|
|
|
|
if (!claims) {
|
|
|
|
return []; // if no claims, return this default
|
|
|
|
}
|
|
|
|
// logger.debug('claims is array?', Array.isArray(claims));
|
|
|
|
// logger.debug(`pageNumber ${pageNumber} is number?`, Number.isInteger(pageNumber));
|
|
|
|
const claimStartIndex = (pageNumber - 1) * CLAIMS_PER_PAGE;
|
2018-02-01 01:00:11 +01:00
|
|
|
const claimEndIndex = claimStartIndex + CLAIMS_PER_PAGE;
|
2018-04-27 08:42:22 +02:00
|
|
|
return claims.slice(claimStartIndex, claimEndIndex);
|
2017-12-07 21:23:21 +01:00
|
|
|
},
|
|
|
|
determineTotalPages (claims) {
|
|
|
|
if (!claims) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
const totalClaims = claims.length;
|
|
|
|
if (totalClaims < CLAIMS_PER_PAGE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const fullPages = Math.floor(totalClaims / CLAIMS_PER_PAGE);
|
|
|
|
const remainder = totalClaims % CLAIMS_PER_PAGE;
|
|
|
|
if (remainder === 0) {
|
|
|
|
return fullPages;
|
|
|
|
}
|
|
|
|
return fullPages + 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
determinePreviousPage (currentPage) {
|
|
|
|
if (currentPage === 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return currentPage - 1;
|
|
|
|
},
|
|
|
|
determineNextPage (totalPages, currentPage) {
|
|
|
|
if (currentPage === totalPages) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return currentPage + 1;
|
|
|
|
},
|
|
|
|
determineTotalClaims (claims) {
|
|
|
|
if (!claims) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return claims.length;
|
|
|
|
},
|
|
|
|
};
|