Skip to content

Commit

Permalink
create postData formatting function
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbernst committed May 3, 2018
1 parent 59fb1cf commit 4dad099
Showing 1 changed file with 67 additions and 77 deletions.
144 changes: 67 additions & 77 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,52 @@ function round(number, precision) {
}

function formatPostData(postData) {

const title = postData.title
const body = postData.body
const timeSincePosted = new Date(postData.created + 'Z')
const jsonMetadata = JSON.parse(postData.json_metadata)
const tags = jsonMetadata.tags


let image
if (typeof jsonMetadata.image !== 'undefined')
image = jsonMetadata.image[0]
else
image = jsonMetadata.thumbnail

let isDtube = false
let isDlive = false
if (typeof jsonMetadata.users !== 'undefined')
isDtube = jsonMetadata.users.includes('dtube')
else
// right now dlive only app not in users section of json metadata
isDlive = true

const numberOfVotes = postData.active_votes.length
const pendingPayoutValue =
Number(postData.pending_payout_value.slice(0, postData.pending_payout_value.indexOf(' '))).toFixed(2)
const totalPayoutValue =
Number(postData.total_payout_value.slice(0, postData.total_payout_value.indexOf(' ')))
const curatorPayoutValue =
Number(postData.curator_payout_value.slice(0, postData.curator_payout_value.indexOf(' ')))
const payoutValue =
Number(pendingPayoutValue) === 0
? round(Number(totalPayoutValue) + Number(curatorPayoutValue), 2).toFixed(2)
: pendingPayoutValue
const permlink = postData.permlink

return {
title,
body,
timeSincePosted,
tags,
image,
isDtube,
isDlive,
numberOfVotes,
payoutValue,
permlink
}
}

// redux thunks
Expand All @@ -112,52 +157,7 @@ export const getRecentPosts = () => async dispatch => {
const recentPosts = await getPosts()

const formattedPostsData = recentPosts.map(post => {
const title = post.title
const body = post.body
const timeSincePosted = new Date(post.created + 'Z')
const jsonMetadata = JSON.parse(post.json_metadata)
const tags = jsonMetadata.tags


let image
if (typeof jsonMetadata.image !== 'undefined')
image = jsonMetadata.image[0]
else
image = jsonMetadata.thumbnail

let isDtube = false
let isDlive = false
if (typeof jsonMetadata.users !== 'undefined')
isDtube = jsonMetadata.users.includes('dtube')
else
// right now dlive only app not in users section of json metadata
isDlive = true

const numberOfVotes = post.active_votes.length
const pendingPayoutValue =
Number(post.pending_payout_value.slice(0, post.pending_payout_value.indexOf(' '))).toFixed(2)
const totalPayoutValue =
Number(post.total_payout_value.slice(0, post.total_payout_value.indexOf(' ')))
const curatorPayoutValue =
Number(post.curator_payout_value.slice(0, post.curator_payout_value.indexOf(' ')))
const payoutValue =
Number(pendingPayoutValue) === 0
? round(Number(totalPayoutValue) + Number(curatorPayoutValue), 2).toFixed(2)
: pendingPayoutValue
const permlink = post.permlink

return {
title,
body,
timeSincePosted,
tags,
image,
isDtube,
isDlive,
numberOfVotes,
payoutValue,
permlink
}
return formatPostData(post)
})

dispatch(getRecentPostsSuccess(formattedPostsData))
Expand All @@ -176,36 +176,26 @@ export const getSinglePost = permlink => async dispatch => {
const [singlePost] = await getPost(permlink)
console.log('single post response: ', singlePost)

const title = singlePost.title
const body = singlePost.body
const timeSincePosted = new Date(singlePost.created + 'Z')
const tags = JSON.parse(singlePost.json_metadata).tags
const image = JSON.parse(singlePost.json_metadata).image[0]
const dtubeOrDlive = JSON.parse(singlePost.json_metadata).users.includes('dtube', 'dlive')
const numberOfVotes = singlePost.active_votes.length
const pendingPayoutValue =
Number(singlePost.pending_payout_value.slice(0, singlePost.pending_payout_value.indexOf(' '))).toFixed(2)
const totalPayoutValue =
Number(singlePost.total_payout_value.slice(0, singlePost.total_payout_value.indexOf(' ')))
const curatorPayoutValue =
Number(singlePost.curator_payout_value.slice(0, singlePost.curator_payout_value.indexOf(' ')))
const payoutValue =
Number(pendingPayoutValue) === 0
? round(Number(totalPayoutValue) + Number(curatorPayoutValue), 2).toFixed(2)
: pendingPayoutValue
const postPermlink = singlePost.permlink

dispatch(getSinglePostSuccess({
title,
body,
timeSincePosted,
tags,
image,
dtubeOrDlive,
numberOfVotes,
payoutValue,
permlink: postPermlink
}))
// const title = singlePost.title
// const body = singlePost.body
// const timeSincePosted = new Date(singlePost.created + 'Z')
// const tags = JSON.parse(singlePost.json_metadata).tags
// const image = JSON.parse(singlePost.json_metadata).image[0]
// const dtubeOrDlive = JSON.parse(singlePost.json_metadata).users.includes('dtube', 'dlive')
// const numberOfVotes = singlePost.active_votes.length
// const pendingPayoutValue =
// Number(singlePost.pending_payout_value.slice(0, singlePost.pending_payout_value.indexOf(' '))).toFixed(2)
// const totalPayoutValue =
// Number(singlePost.total_payout_value.slice(0, singlePost.total_payout_value.indexOf(' ')))
// const curatorPayoutValue =
// Number(singlePost.curator_payout_value.slice(0, singlePost.curator_payout_value.indexOf(' ')))
// const payoutValue =
// Number(pendingPayoutValue) === 0
// ? round(Number(totalPayoutValue) + Number(curatorPayoutValue), 2).toFixed(2)
// : pendingPayoutValue
// const postPermlink = singlePost.permlink

dispatch(getSinglePostSuccess(formatPostData(singlePost)))

} catch (err) {
dispatch(getSinglePostError(err))
Expand Down

0 comments on commit 4dad099

Please sign in to comment.