Browse Source

Implemented functionality allowing people to view older posts on the home page.

pull/34/head
jrtechs 6 years ago
parent
commit
639cd1867e
7 changed files with 115 additions and 59 deletions
  1. +2
    -2
      includes/html/header.html
  2. +8
    -27
      posts/category.js
  3. +5
    -27
      posts/homePage.js
  4. +53
    -0
      posts/renderBatchOfPreviewes.js
  5. +38
    -0
      posts/renderNextBar.js
  6. +8
    -2
      sites/blog.js
  7. +1
    -1
      utils/sql.js

+ 2
- 2
includes/html/header.html View File

@ -8,8 +8,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/includes/css/purge.css" media="screen">
<!--<link rel="stylesheet" href="/css/bootstrap.css" media="screen">-->
<!--<link rel="stylesheet" href="/includes/css/purge.css" media="screen">-->
<link rel="stylesheet" href="/includes/css/bootstrap.css" media="screen">
<link rel="stylesheet" href="/includes/css/code.css" media="screen"> <link rel="stylesheet" href="/includes/css/code.css" media="screen">

+ 8
- 27
posts/category.js View File

@ -4,6 +4,8 @@ const sql = require('../utils/sql');
//file IO //file IO
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
const batchPreview = require('../posts/renderBatchOfPreviewes');
/** /**
* Renders all posts in a single category * Renders all posts in a single category
@ -11,7 +13,7 @@ const utils = require('../utils/utils.js');
* @param resultURL * @param resultURL
* @returns {*} * @returns {*}
*/ */
const renderPosts = function(resultURL)
const renderPosts = function(resultURL, page)
{ {
const splitURL = resultURL.split("/"); const splitURL = resultURL.split("/");
if(splitURL.length >= 3) if(splitURL.length >= 3)
@ -20,32 +22,10 @@ const renderPosts = function(resultURL)
{ {
sql.getPostsFromCategory(splitURL[2]).then(function(posts) sql.getPostsFromCategory(splitURL[2]).then(function(posts)
{ {
var promises = [];
posts.forEach(function(p)
{
promises.push(new Promise(function(res, rej)
{
require("../posts/singlePost.js")
.renderPreview(p).then(function(html)
{
res(html);
}).catch(function(error)
{
rej(error);
})
}));
});
Promise.all(promises).then(function(content)
{
resolve("<div class='col-md-8'>" + content.join('') + "</div>");
}).catch(function(error)
{
reject(error);
});
}).catch(function(err)
resolve(batchPreview.main(resultURL, posts, page, 5));
}).catch(function(error)
{ {
reject(err);
reject(error);
}) })
}); });
} }
@ -67,7 +47,8 @@ module.exports=
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
Promise.all([renderPosts(requestURL),
var page = request.query.page;
Promise.all([renderPosts(requestURL, page),
require("../sidebar/sidebar.js").main()]).then(function(content) require("../sidebar/sidebar.js").main()]).then(function(content)
{ {
resolve(content.join('')); resolve(content.join(''));

+ 5
- 27
posts/homePage.js View File

@ -1,42 +1,19 @@
const sql = require('../utils/sql'); const sql = require('../utils/sql');
const postRenderer = require('../posts/singlePost.js');
const batchPreview = require('../posts/renderBatchOfPreviewes');
/**Renders each recent post for the homepage of the website /**Renders each recent post for the homepage of the website
* *
* @param result * @param result
* @returns {*|Promise} * @returns {*|Promise}
*/ */
var renderRecentPosts = function()
var renderRecentPosts = function(baseURL, page)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
sql.getRecentPostSQL().then(function(posts) sql.getRecentPostSQL().then(function(posts)
{ {
var postPromises = [];
var content = "<div class='col-md-8'>";
posts.forEach(function(post)
{
postPromises.push(new Promise(function(res, rej)
{
postRenderer.renderPreview(post).then(function(cont)
{
res(cont);
}).catch(function(error)
{
rej(error);
})
}));
});
Promise.all(postPromises).then(function(cont)
{
content = content + cont.join('') + "</div>";
resolve(content);
}).catch(function(error)
{
reject(error);
})
resolve(batchPreview.main(baseURL, posts, page, 5));
}).catch(function(error) }).catch(function(error)
{ {
reject(error); reject(error);
@ -54,9 +31,10 @@ module.exports=
*/ */
main: function(requestURL, request) main: function(requestURL, request)
{ {
var page = request.query.page;
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
Promise.all([renderRecentPosts(), require("../sidebar/sidebar.js").main()]).then(function(content)
Promise.all([renderRecentPosts(requestURL, page), require("../sidebar/sidebar.js").main()]).then(function(content)
{ {
resolve(content.join('')); resolve(content.join(''));
}).catch(function(error) }).catch(function(error)

+ 53
- 0
posts/renderBatchOfPreviewes.js View File

@ -0,0 +1,53 @@
module.exports=
{
/**
* Calls posts and sidebar modules to render blog contents in order
*
* @param res
* @param fileName request url
*/
main: function(baseURL, posts, currentPage, numOfPosts)
{
if(typeof currentPage == "undefined")
{
currentPage = 1;
}
else
{
currentPage = Number(currentPage);
}
return new Promise(function(resolve, reject)
{
const promises = [];
for(var i = (currentPage-1) * numOfPosts; i < (currentPage-1) * numOfPosts + numOfPosts; i++)
{
if(i < posts.length)
{
promises.push(new Promise(function(res, rej)
{
require("../posts/singlePost.js")
.renderPreview(posts[i]).then(function(html)
{
res(html);
}).catch(function(error)
{
rej(error);
})
}));
}
}
promises.push(require('../posts/renderNextBar').main(baseURL, currentPage, numOfPosts, posts.length));
Promise.all(promises).then(function(content)
{
resolve("<div class='col-md-8'>" + content.join('') + "</div>");
}).catch(function(error)
{
reject(error);
});
});
}
};

+ 38
- 0
posts/renderNextBar.js View File

@ -0,0 +1,38 @@
const isValidPage = function(page, postsPerPage, totalPosts)
{
return (!(page === 0 || page -1 >= totalPosts/postsPerPage));
};
module.exports=
{
main: function(baseURL, currentPage, postsPerPage, totalPosts)
{
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 &raquo;</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 &raquo;</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>";
}
};

+ 8
- 2
sites/blog.js View File

@ -38,7 +38,12 @@ module.exports=
} }
else else
{ {
const html = cache.get(filename);
var page = request.query.page;
if(typeof page == "undefined")
page = 1;
page = Number(page);
const html = cache.get(filename + "?page=" + page);
result.writeHead(200, {'Content-Type': 'text/html'}); result.writeHead(200, {'Content-Type': 'text/html'});
if (html == null) { if (html == null) {
@ -59,13 +64,14 @@ module.exports=
file = "../posts/posts.js"; file = "../posts/posts.js";
} }
Promise.all([includes.printHeader(), Promise.all([includes.printHeader(),
require(file).main(filename, request), require(file).main(filename, request),
includes.printFooter()]).then(function (content) includes.printFooter()]).then(function (content)
{ {
result.write(content.join('')); result.write(content.join(''));
result.end(); result.end();
cache.put(filename, content.join(''));
cache.put(filename + "?page=" + page, content.join(''));
}).catch(function (err) }).catch(function (err)
{ {

+ 1
- 1
utils/sql.js View File

@ -196,7 +196,7 @@ module.exports=
*/ */
getRecentPostSQL: function() getRecentPostSQL: function()
{ {
return fetch("select * from posts order by post_id desc limit 10");
return fetch("select * from posts order by post_id desc");
}, },

Loading…
Cancel
Save