| @ -1,38 +1,47 @@ | |||||
| const isValidPage = function(page, postsPerPage, totalPosts) | const isValidPage = function(page, postsPerPage, totalPosts) | ||||
| { | { | ||||
| return (!(page === 0 || page -1 >= totalPosts/postsPerPage)); | |||||
| return !(page === 0 || page -1 >= totalPosts/postsPerPage); | |||||
| }; | }; | ||||
| module.exports= | module.exports= | ||||
| { | { | ||||
| main: function(baseURL, currentPage, postsPerPage, totalPosts) | main: function(baseURL, currentPage, postsPerPage, totalPosts) | ||||
| { | { | ||||
| var nextPage = currentPage + 1; | |||||
| var previousPage = currentPage - 1; | |||||
| return new Promise(function(resolve, reject) | |||||
| { | |||||
| var olderPosts = ""; | |||||
| var newerPosts = ""; | |||||
| if(!isValidPage(currentPage, postsPerPage, totalPosts)) | |||||
| { | |||||
| reject("Invalid Page"); | |||||
| } | |||||
| if (isValidPage(previousPage, postsPerPage, totalPosts)) | |||||
| { | |||||
| newerPosts = "<button class=\"btn btn-secondary btn-lg " + | |||||
| "w3-padding-large w3-white w3-border\" onclick=\"location.href='" + | |||||
| baseURL + "?page=" + previousPage + | |||||
| "'\"><b>Newer Posts »</b></button>"; | |||||
| } | |||||
| var nextPage = currentPage + 1; | |||||
| var previousPage = currentPage - 1; | |||||
| var olderPosts = ""; | |||||
| var newerPosts = ""; | |||||
| if (isValidPage(previousPage, postsPerPage, totalPosts)) | |||||
| { | |||||
| newerPosts = "<button class=\"btn btn-secondary btn-lg " + | |||||
| "w3-padding-large w3-white w3-border\" onclick=\"location.href='" + | |||||
| baseURL + "?page=" + previousPage + | |||||
| "'\"><b>Newer Posts »</b></button>"; | |||||
| } | |||||
| if (isValidPage(nextPage, postsPerPage, totalPosts)) | |||||
| { | |||||
| olderPosts = "<button class=\"btn btn-secondary btn-lg " + | |||||
| "w3-padding-large w3-white w3-border\" onclick=\"location.href='" + | |||||
| baseURL + "?page=" + nextPage + | |||||
| "'\"><b>Older Posts »</b></button>"; | |||||
| } | |||||
| resolve(" <div class=\"row\">\n" + | |||||
| " <div class=\"col-6\">" + newerPosts + "</div>\n" + | |||||
| " <div class=\"col-6\"><span class=\"float-right\">" + olderPosts + "</span></div>\n" + | |||||
| " <br><br></div>"); | |||||
| }) | |||||
| if (isValidPage(nextPage, postsPerPage, totalPosts)) | |||||
| { | |||||
| olderPosts = "<button class=\"btn btn-secondary btn-lg " + | |||||
| "w3-padding-large w3-white w3-border\" onclick=\"location.href='" + | |||||
| baseURL + "?page=" + nextPage + | |||||
| "'\"><b>Older Posts »</b></button>"; | |||||
| } | |||||
| return " <div class=\"row\">\n" + | |||||
| " <div class=\"col-6\">" + newerPosts + "</div>\n" + | |||||
| " <div class=\"col-6\"><span class=\"float-right\">" + olderPosts + "</span></div>\n" + | |||||
| " <br><br></div>"; | |||||
| } | } | ||||
| }; | }; | ||||