Browse Source

Fixed recent posts with async promises

pull/4/head
jrtechs 6 years ago
parent
commit
10df8bc26f
8 changed files with 70 additions and 55 deletions
  1. +1
    -0
      includes/footer.html
  2. +32
    -0
      posts/pages.js
  3. +1
    -26
      posts/singlePost.js
  4. +1
    -1
      sidebar/categoriesSideBar.js
  5. +4
    -15
      sidebar/popularPosts.js
  6. +1
    -1
      sidebar/recentPosts.js
  7. +1
    -1
      sidebar/sidebar.js
  8. +29
    -11
      utils/sql.js

+ 1
- 0
includes/footer.html View File

@ -1,4 +1,5 @@
<!-- END w3-content --> <!-- END w3-content -->
</div>
</div> </div>
</div> </div>

+ 32
- 0
posts/pages.js View File

@ -0,0 +1,32 @@
var renderPage = function(result, requestURL)
{
return new Promise(function(resolve, reject)
{
result.write("<div class=\"w3-col l8 s12\">");
});
};
module.exports=
{
/**
* Displays a page and sidebar to users
*
* @param res
* @param fileName request url
*/
main: function(res, requestURL, request)
{
console.log("posts page");
return new Promise(function(resolve, reject)
{
renderPage(res, requestURL).then(function()
{
return require("../sidebar/sidebar.js").main(res)
}).then(function ()
{
resolve();
});
});
}
};

+ 1
- 26
posts/singlePost.js View File

@ -51,29 +51,4 @@ module.exports=
resolve() resolve()
}); });
} }
};
/*
<div class="w3-card-4 w3-margin w3-white">
<img src="/w3images/woods.jpg" alt="Nature" style="width:100%">
<div class="w3-container">
<h3><b>TITLE HEADING</b></h3>
<h5>Title description, <span class="w3-opacity">Date</span></h5>
</div>
<div class="w3-container">
content
</div>
</div>
*/
};

+ 1
- 1
sidebar/categoriesSideBar.js View File

@ -30,7 +30,7 @@ module.exports=
console.log(cat); console.log(cat);
res.write("<li class=\"w3-padding-16\">"); res.write("<li class=\"w3-padding-16\">");
res.write("<a href='\\category\\" + cat.url + "'><span class=\"w3-large\">" + cat.name + "</span><br></a>");
res.write("<a href='/category/" + cat.url + "'><span class=\"w3-large\">" + cat.name + "</span><br></a>");
res.write("</li>"); res.write("</li>");
}); });

+ 4
- 15
sidebar/popularPosts.js View File

@ -11,33 +11,22 @@ module.exports=
res.write("<div class=\"w3-card w3-margin\">"); res.write("<div class=\"w3-card w3-margin\">");
res.write(" <div class=\"w3-container w3-padding\">\n" +
"\n" +
" <h4>Popular Posts</h4>\n" +
"\n" +
" </div>");
res.write("<div class=\"w3-container w3-padding\"><h4>Popular Posts</h4></div>");
res.write("<ul class=\"w3-ul w3-hoverable w3-white\">"); res.write("<ul class=\"w3-ul w3-hoverable w3-white\">");
sql.getCategories().then(function(categories)
sql.getPopularPosts().then(function(posts)
{ {
console.log(categories[0].name);
categories.forEach(function(cat)
posts.forEach(function(cat)
{ {
//res.write(cat.name);
console.log(cat); console.log(cat);
res.write("<li class=\"w3-padding-16\">"); res.write("<li class=\"w3-padding-16\">");
//res.write("<a href=\"#\#\" class=\"w3-bar-item w3-button\" style=\"width=100%\">" + cat.name + "</a>");
res.write("<a href='jrtechs.net'><span class=\"w3-large\">" + cat.name + "</span><br></a>");
res.write("<a href='" + url + "'><span class=\"w3-large\">" + p.name + "</span><br></a>");
res.write("</li>"); res.write("</li>");
}); });
res.write("</ul></div>"); res.write("</ul></div>");
resolve(); resolve();
}) })
}); });
} }
}; };

+ 1
- 1
sidebar/recentPosts.js View File

@ -19,7 +19,7 @@ module.exports=
{ {
posts.forEach(function(p) posts.forEach(function(p)
{ {
var url = '\\' + p.category + '\\' + p.name;
var url = '/' + p.category + '/' + p.url;
res.write("<li class=\"w3-padding-16\">"); res.write("<li class=\"w3-padding-16\">");
res.write("<a href='" + url + "'><span class=\"w3-large\">" + p.name + "</span><br></a>"); res.write("<a href='" + url + "'><span class=\"w3-large\">" + p.name + "</span><br></a>");
res.write("</li>"); res.write("</li>");

+ 1
- 1
sidebar/sidebar.js View File

@ -11,7 +11,7 @@ module.exports=
utils.include(res,"sidebar/sidebar.html").then(function() utils.include(res,"sidebar/sidebar.html").then(function()
{ {
return require("../sidebar/popularPosts.js").main(res);
return require("../sidebar/recentPosts.js").main(res);
}).then(function() }).then(function()
{ {
return require("../sidebar/categoriesSideBar.js").main(res); return require("../sidebar/categoriesSideBar.js").main(res);

+ 29
- 11
utils/sql.js View File

@ -153,22 +153,40 @@ module.exports=
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
var q = "select name, category_id from posts order by post_id desc limit 10";
var q = "select name,url, category_id from posts order by post_id desc limit 10";
fetch(q).then(function(sqlPosts) fetch(q).then(function(sqlPosts)
{ {
var posts = [];
sqlPosts.forEach(function(p)
var promises = [];
sqlPosts.forEach(function(post)
{ {
var getCategory = "select url from categories where category_id='" + p.category_id + "'";
fetch(getCategory).then(function(url)
promises.push(new Promise(function(res, rej)
{ {
var obj = new Object();
obj.name = p.name;
obj.category = url.url;
posts.push(obj);
})
var getCategory = "select url from categories where category_id='" + post.category_id + "'";
fetch(getCategory).then(function(urls)
{
var obj = new Object();
obj.name = post.name;
obj.url = post.url;
obj.category = urls[0].url;
res(obj);
});
}));
});
Promise.all(promises).then(function(goodies)
{
resolve(goodies);
}); });
resolve(posts);
});
});
},
getPopularPosts: function()
{
return new Promise(function(resolve, reject)
{
var q = "select * from popular_posts";
fetch(q).then(function(sqlPosts)
{
}); });
}); });
} }

Loading…
Cancel
Save