Skip to content

Commit b18dc52

Browse files
committed
fix feed and add voting functionality
1 parent e70989a commit b18dc52

File tree

7 files changed

+87
-16
lines changed

7 files changed

+87
-16
lines changed

public/js/main.js

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
11

2+
// EXAMPLE - FEED PAGE
23
if ($('main').hasClass('feed') ) {
34
steem.api.setOptions({ url: 'wss://rpc.buildteam.io' });
45
steem.api.getState('/trending/', (err, result) => {
5-
console.log(err, result);
6+
7+
let resultsArray = [];
68

79
for ( post in result.content ){
8-
console.log(post);
10+
11+
var converter = new showdown.Converter()
12+
var html = converter.makeHtml(result.content[post].body)
13+
14+
resultsArray.push({
15+
id: result.content[post].id,
16+
title: result.content[post].title,
17+
author: result.content[post].author,
18+
body: html,
19+
permlink: result.content[post].permlink
20+
})
21+
}
22+
23+
resultsArray = resultsArray.sort((a,b) => {
24+
return b.id - a.id
25+
});
26+
27+
resultsArray.forEach( (post, i, arr) => {
28+
929
let template = `
10-
<div>
11-
<h3><a href="">${result.content[post].title}</a> - ${result.content[post].author}</h3>
12-
<p></p>
13-
<p>${ (result.content[post].body).substring(0, 250) }...</p>
14-
</div>
15-
`
30+
<div data-post-id="${post.id}">
31+
<h3><a href="">${post.title}</a> - ${post.author}</h3>
32+
<p>${ (post.body).substring(0, 250) }...</p>
33+
<form method="post">
34+
<input type="hidden" name="postId" value="${post.id}">
35+
<input type="hidden" name="author" value="${post.author}">
36+
<input type="hidden" name="permlink" value="${post.permlink}">
37+
<input type="submit" class="vote" value="Vote">
38+
</form>
39+
</div>`
1640
$('main').append(template)
41+
})
42+
1743

18-
}
1944
});
45+
46+
$('main').on('click', '.vote',(e) => {
47+
e.preventDefault()
48+
$.post({
49+
url: '/post/vote',
50+
dataType: 'json',
51+
data: $(e.currentTarget).parent().serialize()
52+
}, (response) => {
53+
$(`*[data-post-id="${response.id}"]`).children().last().after()
54+
.append('<span> Voted!</span>')
55+
})
56+
57+
})
2058
}

public/js/showdown.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/post.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ router.post('/create-post', (req, res) => {
2020
res.redirect('/auth')
2121
} else {
2222
console.log(req.body)
23-
console.log(req.session.steemconnect.name)
24-
23+
api.vote(voter, author, permlink, weight, function (err, res) {
24+
console.log(err, res)
25+
});
2526
let author = req.session.steemconnect.name
2627
let permlink = util.urlString()
2728
var tags = req.body.tags.split(',').map(item => item.trim());
@@ -34,9 +35,9 @@ router.post('/create-post', (req, res) => {
3435
console.log(err, steemResponse)
3536
let msg;
3637
if (err) {
37-
let msg = `Post Failed ${err}`
38+
let msg = `Post Failed ${err}`
3839
} else {
39-
let msg = 'Posted To the Steem Network'
40+
let msg = 'Posted To the Steem Network'
4041
}
4142

4243
res.render('post', {
@@ -48,4 +49,32 @@ router.post('/create-post', (req, res) => {
4849
}
4950
});
5051

52+
router.post('/vote', (req, res) => {
53+
if (!req.session.steemconnect) {
54+
res.redirect('/auth')
55+
} else {
56+
let postId = req.body.postId
57+
let voter = req.session.steemconnect.name
58+
let author = req.body.author
59+
let permlink = req.body.permlink
60+
let weight = 10000
61+
62+
steem.vote(voter, author, permlink, weight, function (err, steemResponse) {
63+
if (err) {
64+
res.json({
65+
error: error.error_description
66+
})
67+
} else {
68+
res.json({
69+
name: req.session.steemconnect.name,
70+
msg: 'voted!',
71+
id: postId
72+
})
73+
}
74+
75+
});
76+
77+
}
78+
})
79+
5180
module.exports = router;

views/create-post.pug

Whitespace-only changes.

views/feed.pug

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ block content
55

66
main.feed
77
h1 Feed - /Trending
8+
9+
script(src='/js/showdown.min.js')

views/layout.pug

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ doctype html
22
html
33
head
44
title= title
5-
link(rel='stylesheet', href='/stylesheets/style.css')
5+
link(rel='stylesheet', href='/css/style.css')
66
body
77
block content
88
script(src="https://code.jquery.com/jquery-3.2.1.min.js"
99
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
1010
crossorigin="anonymous")
1111
script(src="https://cdn.steemjs.com/lib/latest/steem.min.js")
12-
script(src="/js/sc2-sdk.min.js")
1312
script(src="/js/main.js")

views/post.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ block content
1212
textarea(name="post")
1313
label Tags
1414
input(type="text" name="tags")
15-
button(type="submit" ) Submit
15+
button(type="submit") Submit
1616
p Posting as #{name}
1717
p #{msg}

0 commit comments

Comments
 (0)