From 5a5b666794b688881eb1e1635bf9f2ceb3cc4346 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Sat, 19 Dec 2020 22:35:50 -0600 Subject: Add title to posts --- src/templates/feed.html | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/templates/feed.html') diff --git a/src/templates/feed.html b/src/templates/feed.html index 2158f3e..d7494a2 100644 --- a/src/templates/feed.html +++ b/src/templates/feed.html @@ -1,6 +1,9 @@
{{#each posts}}
+ {{#if this.showTitle}} +

{{this.title}}

+ {{/if}}

{{{this.description}}}

{{#each this.images}} -- cgit v1.2.3 From 5137bb9b277fcd45327680166c9e39f26ec48c45 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Wed, 30 Dec 2020 22:28:39 -0600 Subject: Add like button to posts --- src/css/styles.css | 17 +++++++++++++++-- src/index.js | 6 +++++- src/server.js | 10 ++++++++++ src/templates/feed.html | 5 ++++- 4 files changed, 34 insertions(+), 4 deletions(-) (limited to 'src/templates/feed.html') diff --git a/src/css/styles.css b/src/css/styles.css index 3a555cb..88d8e68 100644 --- a/src/css/styles.css +++ b/src/css/styles.css @@ -8,7 +8,6 @@ --btn-hover: #0a0; --light-text: #aaa; } - body { width: 60%; @@ -142,7 +141,6 @@ p { padding: 1px 3px; border: 1px solid var(--btn-text); border-radius: 3px; - width: 100%; max-width: 100px; line-height: 2em; text-align: center; @@ -152,6 +150,7 @@ p { } .btn-primary { + width: 100%; height: 2em; font-size: 20px; background-color: var(--btn-color); @@ -162,6 +161,15 @@ p { background-color: var(--btn-hover); } +.btn-secondary { + background-color: var(--btn-color); + color: var(--btn-text); +} + +.btn-secondary:hover{ + background-color: var(--btn-hover); +} + .date { font-style: italic; margin: 0em; @@ -223,3 +231,8 @@ a.navigation:visited, a.navigation:link { .email::after { content: "@marks.kitchen"; } + +a.cool { + float: right; +} + diff --git a/src/index.js b/src/index.js index a94f8d0..7ceecf0 100644 --- a/src/index.js +++ b/src/index.js @@ -56,7 +56,11 @@ function setUpModels(){ }, title: { type: Sequelize.STRING, - },}), + }, + likes: { + type: Sequelize.INTEGER, + } + }), "pictures": database.define('pictures', { source: { type: Sequelize.TEXT, allowNull: false}, }), diff --git a/src/server.js b/src/server.js index d820cd7..cd76b5d 100644 --- a/src/server.js +++ b/src/server.js @@ -146,6 +146,15 @@ function setUpRoutes(models, jwtFunctions, database, templates) { let body = templates["blog-single"]({posts, date}); res.status(200).send(body) }) + server.get('/post/like/:type/:id', async (req, res) => { + let type = req.params.type + let id = req.params.id + var post = await models.posts.findOne({ + where: { type, id }, + }); + post.update({likes: post.likes+1}) + res.redirect(`/post/${type}/${id}`); + }) server.get('/tags/:name', async (req, res) => { const { name } = req.params; const postsWithTag = await models.tags.findAll({ attributes: ["postId"], where: { text: name } }) @@ -258,6 +267,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { const type = req.body.type req.body.description = marked(req.body.description) const newPost = await models.posts.create(req.body); + newPost.likes = 0 req.files.forEach(async (file) => { await models.pictures.create({ "source": "uploads/" + file.filename, "postId": newPost.id }); console.log("uploaded ", file.path); diff --git a/src/templates/feed.html b/src/templates/feed.html index d7494a2..ae3494c 100644 --- a/src/templates/feed.html +++ b/src/templates/feed.html @@ -21,7 +21,10 @@ {{this}} {{/each}} + + 👍 ({{this.likes}}) +

{{/each}} -
\ No newline at end of file +
-- cgit v1.2.3 From 5b5dc610bc0da17fd006982de50b4dc74b134a54 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Tue, 5 Jan 2021 20:25:10 -0600 Subject: Refactor likes to use JS to avoid redirect --- src/css/styles.css | 3 ++- src/js/like.js | 11 +++++++++++ src/server.js | 2 +- src/templates/feed.html | 4 ++-- src/templates/header.html | 2 +- 5 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 src/js/like.js (limited to 'src/templates/feed.html') diff --git a/src/css/styles.css b/src/css/styles.css index 88d8e68..4572569 100644 --- a/src/css/styles.css +++ b/src/css/styles.css @@ -147,6 +147,7 @@ p { vertical-align: middle; white-space: nowrap; text-decoration: none; + cursor: pointer; } .btn-primary { @@ -232,7 +233,7 @@ a.navigation:visited, a.navigation:link { content: "@marks.kitchen"; } -a.cool { +.cool { float: right; } diff --git a/src/js/like.js b/src/js/like.js new file mode 100644 index 0000000..cd1e015 --- /dev/null +++ b/src/js/like.js @@ -0,0 +1,11 @@ +function likePost(type, id){ + fetch(`/post/like/${type}/${id}`) + .then(response => response.json()) + .then(response => { + console.log(response) + let btn_id=`btn_${type}_${id}` + let el = document.getElementById(btn_id) + el.innerText = `👍 (${response.likes})` + }) +} + diff --git a/src/server.js b/src/server.js index 07da08c..773feb0 100644 --- a/src/server.js +++ b/src/server.js @@ -153,7 +153,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { where: { type, id }, }); post.update({likes: post.likes+1}) - res.redirect(`/post/${type}/${id}`); + res.status(200).send({likes: post.likes}); }) server.get('/tags/:name', async (req, res) => { const { name } = req.params; diff --git a/src/templates/feed.html b/src/templates/feed.html index ae3494c..3ea4f16 100644 --- a/src/templates/feed.html +++ b/src/templates/feed.html @@ -21,9 +21,9 @@ {{this}} {{/each}} - + 👍 ({{this.likes}}) - +

{{/each}} diff --git a/src/templates/header.html b/src/templates/header.html index 2ceebfd..1989b04 100644 --- a/src/templates/header.html +++ b/src/templates/header.html @@ -2,4 +2,4 @@ - + -- cgit v1.2.3