Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.5 KiB

  1. /**
  2. * Determines if the requested page is out of bounds
  3. *
  4. * @param page current page
  5. * @param postsPerPage - number of blog rendered on each page
  6. * @param totalPosts - total blog in this category/total
  7. * @returns {boolean} if this is a valid page
  8. */
  9. const isValidPage = function(page, postsPerPage, totalPosts)
  10. {
  11. return !(page === 0 || page -1 >= totalPosts/postsPerPage);
  12. };
  13. module.exports=
  14. {
  15. /**
  16. * Renders two buttons on the bottom of the page to
  17. * go to the left or right
  18. *
  19. * Used by the home page and categories pages
  20. * @param baseURL -- base url of page being rendered
  21. * @param currentPage -- current page being rendered
  22. * @param postsPerPage -- number of blog on each page
  23. * @param totalPosts -- total amount of blog in the category
  24. * @returns {Promise} promise which renders the buttons
  25. */
  26. main: function(baseURL, currentPage, postsPerPage, totalPosts)
  27. {
  28. return new Promise(function(resolve, reject)
  29. {
  30. if(!isValidPage(currentPage, postsPerPage, totalPosts))
  31. {
  32. reject("Invalid Page");
  33. }
  34. var nextPage = currentPage + 1;
  35. var previousPage = currentPage - 1;
  36. var olderPosts = "";
  37. var newerPosts = "";
  38. if (isValidPage(previousPage, postsPerPage, totalPosts))
  39. {
  40. newerPosts = "<button class=\"btn btn-secondary btn-lg " +
  41. "w3-padding-large w3-white w3-border\" onclick=\"location.href='" +
  42. baseURL + "?page=" + previousPage +
  43. "'\"><b>Newer Posts &raquo;</b></button>";
  44. }
  45. if (isValidPage(nextPage, postsPerPage, totalPosts))
  46. {
  47. olderPosts = "<button class=\"btn btn-secondary btn-lg " +
  48. "w3-padding-large w3-white w3-border\" onclick=\"location.href='" +
  49. baseURL + "?page=" + nextPage +
  50. "'\"><b>Older Posts &raquo;</b></button>";
  51. }
  52. resolve(" <div class=\"row\">\n" +
  53. " <div class=\"col-6\">" + newerPosts + "</div>\n" +
  54. " <div class=\"col-6\"><span class=\"float-right\">" + olderPosts + "</span></div>\n" +
  55. " <br><br></div>");
  56. })
  57. }
  58. };