From 4832cc9850b587b6bc130a6783ed2d37ef5d573c Mon Sep 17 00:00:00 2001 From: jrtechs Date: Thu, 17 Jan 2019 20:11:14 -0500 Subject: [PATCH] Updated contact page to use templating engine. --- {includes => blog}/contact.js | 29 ++++++++---- {sidebar => blog}/sidebar.js | 0 includes/html/contact.html | 74 ------------------------------- includes/html/invalidCaptcha.html | 4 -- sites/blog.js | 4 +- 5 files changed, 22 insertions(+), 89 deletions(-) rename {includes => blog}/contact.js (87%) rename {sidebar => blog}/sidebar.js (100%) delete mode 100644 includes/html/contact.html delete mode 100644 includes/html/invalidCaptcha.html diff --git a/includes/contact.js b/blog/contact.js similarity index 87% rename from includes/contact.js rename to blog/contact.js index d4cfe75..0caa03d 100644 --- a/includes/contact.js +++ b/blog/contact.js @@ -38,6 +38,10 @@ const CAPTCHA_SECRET = config.CAPTCHA_SECRET; const EMAIL_PASSWORD = config.EMAIL_PASSWORD; +const TEMPLATE_FILE = "blog/contact.html"; +const whiskers = require('whiskers'); + + /** * Verifies if the captcha response recieved from the post data was * valid, or are bots trying to get around the captcha @@ -142,7 +146,7 @@ const sendEmail = function(name, email, message) * @param request -- main express request * @returns {Promise} renders the html of the contact widget */ -const processContactPage = function(request) +const processContactPage = function(request, templateContext) { return new Promise(function(resolve, reject) { @@ -161,21 +165,22 @@ const processContactPage = function(request) { if(valid) { - resolve(utils.include("includes/html/messageSent.html")); - + templateContext.messageSent = true; + resolve(); sendEmail(data.name, data.email, data.message); } else { - resolve(utils.include("includes/html/invalidCaptcha.html")); + resolve(); } }); } else { - resolve(utils.include("includes/html/contact.html")); + resolve(); } + }).catch(function(err) { reject(err); @@ -198,12 +203,18 @@ module.exports = main: function(request, result) { result.writeHead(200, {'Content-Type': 'text/html'}); - Promise.all([includes.printAdminHeader(), + + console.log("eh"); + var templateContext = Object(); + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), processContactPage(request), - require("../sidebar/sidebar.js").main(), - includes.printFooter()]).then(function(content) + includes.printHeader(templateContext), + includes.printFooter(templateContext), + require("./sidebar.js").main(templateContext)]) + .then(function (content) { - result.write(content.join('')); + const html = whiskers.render(content[0], templateContext); + result.write(html); result.end(); }).catch(function(err) diff --git a/sidebar/sidebar.js b/blog/sidebar.js similarity index 100% rename from sidebar/sidebar.js rename to blog/sidebar.js diff --git a/includes/html/contact.html b/includes/html/contact.html deleted file mode 100644 index 3bdd82d..0000000 --- a/includes/html/contact.html +++ /dev/null @@ -1,74 +0,0 @@ -
- - - -
-
-
-
-

Contact Me

-

I would love to hear from you.

-
-
-
- - -
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
- -
-
-
-
- -
-
- -
-
- -
-
-
- -
- -
-
- -
-
-
-
-
- - - - \ No newline at end of file diff --git a/includes/html/invalidCaptcha.html b/includes/html/invalidCaptcha.html deleted file mode 100644 index 7c08c78..0000000 --- a/includes/html/invalidCaptcha.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Invalid Captcha

- -
\ No newline at end of file diff --git a/sites/blog.js b/sites/blog.js index 430dfae..162f26a 100644 --- a/sites/blog.js +++ b/sites/blog.js @@ -41,7 +41,7 @@ module.exports= } else if (filename.includes("/contact")) { - require("../includes/contact.js").main(request, result); + require("../blog/contact.js").main(request, result); } else { @@ -79,7 +79,7 @@ module.exports= includes.printHeader(templateContext), includes.printFooter(templateContext), require(file).main(filename, request, templateContext), - require("../sidebar/sidebar.js").main(templateContext)]) + require("../blog/sidebar.js").main(templateContext)]) .then(function (content) { var html = whiskers.render(content[0], templateContext);