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.

54 lines
1.9 KiB

  1. module.exports=
  2. {
  3. /**
  4. * Renders a bunch of blog post previews to the user
  5. *
  6. * @param baseURL-- url of the page
  7. * @param posts -- sql data about the posts to render
  8. * @param currentPage -- the current page to render
  9. * @param numOfPosts -- number of posts to render
  10. * @returns {Promise} renders the html of the posts
  11. */
  12. main: function(baseURL, posts, currentPage, numOfPosts)
  13. {
  14. if(typeof currentPage == "undefined")
  15. {
  16. currentPage = 1;
  17. }
  18. else
  19. {
  20. currentPage = Number(currentPage);
  21. }
  22. return new Promise(function(resolve, reject)
  23. {
  24. const promises = [];
  25. for(var i = (currentPage-1) * numOfPosts; i < (currentPage-1) * numOfPosts + numOfPosts; i++)
  26. {
  27. if(i < posts.length)
  28. {
  29. promises.push(new Promise(function(res, rej)
  30. {
  31. require("../posts/singlePost.js")
  32. .renderPreview(posts[i]).then(function(html)
  33. {
  34. res(html);
  35. }).catch(function(error)
  36. {
  37. reject(error)
  38. })
  39. }));
  40. }
  41. }
  42. promises.push(require('../posts/renderNextBar').main(baseURL, currentPage, numOfPosts, posts.length));
  43. Promise.all(promises).then(function(content)
  44. {
  45. resolve("<div class='col-md-8'>" + content.join('') + "</div>");
  46. }).catch(function(error)
  47. {
  48. reject(error);
  49. });
  50. });
  51. }
  52. };