Skip to content

Commit ccea826

Browse files
committed
add single page content
1 parent ea9d94d commit ccea826

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

public/js/main.js

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ function displayContent(result, initial){
6262
}
6363

6464
let itemTemplate = `
65+
<a href="${post.url}">
6566
<div class="item " data-url="${post.url}" data-permlink="${ post.permlink }">
6667
<img class="item__image " src="https://steemitimages.com/480x768/${image}" onerror="">
6768
<div class="item__author">
6869
<h1>${post.title}</h1>
6970
<span>@${post.author}</span>
7071
</div>
7172
</div>
73+
</a>
7274
`
7375
$('.feed-insert').append(itemTemplate)
7476
}
@@ -91,10 +93,89 @@ function genImageInHTML(markdown){
9193
}
9294
}
9395

94-
function getPost(url) {
95-
console.log(url)
96+
function getPostAndComments(url) {
97+
steem.api.getState(url, (err, result) => {
98+
let users = result.accounts;
99+
let resultsArray = [];
100+
for ( post in result.content ){
101+
102+
var html = result.content[post].body
103+
104+
resultsArray.push({
105+
id: result.content[post].id,
106+
title: result.content[post].root_title,
107+
author: result.content[post].author,
108+
body: html,
109+
json: result.content[post].json_metadata,
110+
permlink: result.content[post].permlink,
111+
depth: result.content[post].depth,
112+
root_comment: result.content[post].root_comment,
113+
parent_permlink: result.content[post].parent_permlink,
114+
created: result.content[post].created,
115+
votes: result.content[post].net_votes,
116+
voters: result.content[post].active_votes.map(vote => vote.voter),
117+
value: Math.round( parseFloat(result.content[post].pending_payout_value.substring(0,5)) * 100) / 100
118+
})
119+
}
120+
121+
// Sort By Date/ID
122+
resultsArray = resultsArray.sort((a,b) => {
123+
return b.id - a.id
124+
});
125+
126+
// Find Deepest Comment
127+
let maxDepthComment = resultsArray.reduce((prev, current) => {
128+
return (prev.depth > current.depth) ? prev : current
129+
})
130+
131+
// Multi demention array by
132+
let resultsByDepth = [];
133+
for (var i = 0; i < maxDepthComment.depth + 1; i++) {
134+
resultsByDepth.push(resultsArray.filter(elem => {
135+
return elem.depth === i
136+
}))
137+
}
138+
appendSinglePost(resultsByDepth[0][0], users)
139+
140+
})
141+
}
142+
143+
function generateProfileImage(author){
144+
let profileImage = 'img/default-user.jpg';
145+
146+
try {
147+
if (author.json_metadata === '' || typeof author.json_metadata === 'undefined' ) {
148+
author.json_metadata = { profile_image : false }
149+
} else {
150+
author.json_metadata = JSON.parse(author.json_metadata).profile
151+
}
152+
153+
profileImage = author.json_metadata.profile_image ? 'https://steemitimages.com/128x128/' + author.json_metadata.profile_image : '';
154+
155+
} catch(err){
156+
console.log(err)
157+
}
158+
return profileImage
96159
}
97160

161+
function appendSinglePost(post, users){
162+
let author = users[post.author]
163+
console.log(author)
164+
let html = converter.makeHtml(post.body)
165+
let profileImage = generateProfileImage(author)
166+
167+
let tags = JSON.parse(post.json).tags.reduce( (all,tag) => all + `<span>${tag}</span>`, '')
168+
let header = `
169+
<img src="${profileImage}" class="author-img" width="35" height="35" src="">
170+
<span class="overlay__author-username">@${post.author}</span>
171+
${tags}
172+
<h1 class="title">${post.title}</h1>
173+
`
174+
$('main').append(header + html)
175+
}
176+
177+
// ----------------------------------------------------
178+
98179
if ($('main').hasClass('feed') ) {
99180

100181
let feedType = $('main.feed').data('feed-type')
@@ -108,7 +189,7 @@ if ($('main').hasClass('feed') ) {
108189

109190
if ($('main').hasClass('single')) {
110191
let data = $('main').data()
111-
getPost(`/@${data.username}/${data.permlink}`)
192+
getPostAndComments(`/${data.category}/@${data.username}/${data.permlink}`)
112193
}
113194

114195

routes/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ router.get('/@:username?', (req, res, next) => {
1818
});
1919
});
2020

21-
router.get('/@:username/:permlink', (req, res, next) => {
21+
router.get('/:category/@:username/:permlink', (req, res, next) => {
22+
let category = req.params.category
2223
let username = req.params.username
2324
let permlink = req.params.permlink
24-
console.log(username)
2525
res.render('single', {
26+
category: category,
2627
username: username,
2728
permlink: permlink
2829
});

views/single.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extends layout
33
block content
44
include partials/nav
55

6-
main.single(data-username=`${username}` data-permlink=`${permlink}`)
6+
main.single(data-username=`${username}` data-permlink=`${permlink}` data-category=`${category}`)
77
h1 Profile
88
h2 #{username}
99
h2 #{permlink}

0 commit comments

Comments
 (0)