From 0d76c08de59ea911d827720c4df4b528c7af68aa Mon Sep 17 00:00:00 2001 From: u-e Date: Mon, 17 Jun 2019 23:43:32 +0300 Subject: [PATCH] wip on comments vote --- .../comments/container/commentsContainer.js | 25 +++--- src/providers/steem/dsteem.js | 81 +++++++++++++------ src/utils/postParser.js | 57 ++++++++----- 3 files changed, 108 insertions(+), 55 deletions(-) diff --git a/src/components/comments/container/commentsContainer.js b/src/components/comments/container/commentsContainer.js index 978900452..076f7cba9 100644 --- a/src/components/comments/container/commentsContainer.js +++ b/src/components/comments/container/commentsContainer.js @@ -5,7 +5,7 @@ import { injectIntl } from 'react-intl'; import get from 'lodash/get'; import { getComments, deleteComment } from '../../../providers/steem/dsteem'; - +import { parseComments } from '../../../utils/postParser'; // Services and Actions import { writeToClipboard } from '../../../utils/clipboard'; import { toastNotification } from '../../../redux/actions/uiAction'; @@ -56,9 +56,9 @@ class CommentsContainer extends Component { const { comments: parent } = this.state; const allPayout = c => - parseFloat(c.pending_payout_value.split(' ')[0]) + - parseFloat(c.total_payout_value.split(' ')[0]) + - parseFloat(c.curator_payout_value.split(' ')[0]); + parseFloat(get(c, 'pending_payout_value').split(' ')[0]) + + parseFloat(get(c, 'total_payout_value').split(' ')[0]) + + parseFloat(get(c, 'curator_payout_value').split(' ')[0]); const absNegative = a => a.net_rshares < 0; @@ -122,17 +122,22 @@ class CommentsContainer extends Component { return parent; }; - _getComments = () => { + _getComments = async () => { const { author, permlink, currentAccount: { name }, } = this.props; + let com; - getComments(author, permlink, name).then(comments => { - this.setState({ - comments, - }); + await getComments(author, permlink, name) + .then(async comments => { + com = comments; + }) + .catch(() => {}); + + await this.setState({ + comments: await parseComments(com, name), }); }; @@ -218,7 +223,7 @@ class CommentsContainer extends Component { isShowMoreButton={isShowMoreButton} commentNumber={commentNumber || 1} commentCount={commentCount} - comments={_comments || comments} + comments={parseComments(_comments || comments, currentAccount.name)} currentAccountUsername={currentAccount.name} handleOnEditPress={this._handleOnEditPress} handleOnReplyPress={this._handleOnReplyPress} diff --git a/src/providers/steem/dsteem.js b/src/providers/steem/dsteem.js index 77bc57fd2..915d143ba 100644 --- a/src/providers/steem/dsteem.js +++ b/src/providers/steem/dsteem.js @@ -65,13 +65,13 @@ export const fetchGlobalProps = async () => { } const steemPerMVests = - (parseToken(globalDynamic.total_vesting_fund_steem) / - parseToken(globalDynamic.total_vesting_shares)) * + (parseToken(get(globalDynamic, 'total_vesting_fund_steem')) / + parseToken(get(globalDynamic, 'total_vesting_shares'))) * 1e6; - const base = parseToken(feedHistory.current_median_history.base); - const quote = parseToken(feedHistory.current_median_history.quote); - const fundRecentClaims = rewardFund.recent_claims; - const fundRewardBalance = parseToken(rewardFund.reward_balance); + const base = parseToken(get(feedHistory, 'current_median_history.base')); + const quote = parseToken(get(feedHistory, 'current_median_history.quote')); + const fundRecentClaims = get(rewardFund, 'recent_claims'); + const fundRewardBalance = parseToken(get(rewardFund, 'reward_balance')); const globalProps = { steemPerMVests, base, @@ -288,8 +288,15 @@ export const getPosts = async (by, query, user) => { } }; -export const getActiveVotes = (author, permlink) => - client.database.call('get_active_votes', [author, permlink]); +export const getActiveVotes = async (author, permlink) => { + if (!author || !permlink) return null; + + try { + return await client.database.call('get_active_votes', [author, permlink]); + } catch (error) { + return error; + } +}; export const getPostsSummary = async (by, query, currentUserName, filterNsfw) => { try { @@ -312,7 +319,7 @@ export const getPostsSummary = async (by, query, currentUserName, filterNsfw) => export const getUserComments = async query => { try { let comments = await client.database.getDiscussions('comments', query); - comments = parseComments(comments); + comments = await parseComments(comments); return comments; } catch (error) { return error; @@ -326,7 +333,7 @@ export const getRepliesByLastUpdate = async query => { query.start_permlink, query.limit, ]); - replies = parseComments(replies); + replies = await parseComments(replies); return replies; } catch (error) { return error; @@ -343,6 +350,8 @@ export const getPost = async (author, permlink, currentUserName = null) => { try { const post = await client.database.call('get_content', [author, permlink]); + const ugur = await getState(`/${post.category}/@${post.author}/${post.permlink}`); + console.log(ugur); return post ? await parsePost(post, currentUserName) : null; } catch (error) { return error; @@ -400,21 +409,43 @@ export const deleteComment = (currentAccount, pin, permlink) => { * @param user post author * @param permlink post permlink */ -export const getComments = (user, permlink, currentUserName) => { - let comments; - return new Promise((resolve, reject) => { - client.database - .call('get_content_replies', [user, permlink]) - .then(result => { - comments = parseComments(result, currentUserName); - }) - .then(() => { - resolve(parseComments(comments, currentUserName)); - }) - .catch(error => { - reject(error); - }); - }); +// export const getComments = async (user, permlink, currentUserName) => { +// return new Promise((resolve, reject) => { +// client.database +// .call('get_content_replies', [user, permlink]) +// .then(async result => { +// const comments = await parseComments(result, currentUserName); +// resolve(comments); +// }) +// .catch(error => { +// reject(error); +// }); +// }); +// }; + +// export const getComments = async (user, permlink, currentUserName) => { +// try { +// const comments = client.database.call('get_content_replies', [user, permlink]); +// const groomedComments = await parseComments(comments, currentUserName); + +// return groomedComments; +// } catch (error) { +// return error; +// } +// }; + +export const getComments = async (author, permlink, currentUserName = null) => { + try { + const post = await client.database.call('get_content_replies', [author, permlink]); + + // const ugur = await getState(`/esteem/@${author}/${permlink}`); + // console.log(post); + // console.log(ugur); + // return post ? await parseComments(post, currentUserName) : null; + return post; + } catch (error) { + return error; + } }; /** diff --git a/src/utils/postParser.js b/src/utils/postParser.js index 7f5ea51b4..374754f90 100644 --- a/src/utils/postParser.js +++ b/src/utils/postParser.js @@ -1,6 +1,12 @@ import isEmpty from 'lodash/isEmpty'; import forEach from 'lodash/forEach'; +import get from 'lodash/get'; + import { postBodySummary, renderPostBody } from '@esteemapp/esteem-render-helpers'; + +// dsteem +import { getActiveVotes } from '../providers/steem/dsteem'; + // Utils import { getReputation } from './reputation'; @@ -47,7 +53,7 @@ export const parsePost = (post, currentUserName) => { forEach(post.active_votes, value => { post.vote_perecent = value.voter === currentUserName ? value.percent : null; value.value = (value.rshares * ratio).toFixed(3); - value.reputation = getReputation(value.reputation); + value.reputation = getReputation(get(value, 'reputation')); value.percent /= 100; value.is_down_vote = Math.sign(value.percent) < 0; value.avatar = `https://steemitimages.com/u/${value.voter}/avatar/small`; @@ -57,9 +63,6 @@ export const parsePost = (post, currentUserName) => { return post; }; -const isVoted = (activeVotes, currentUserName) => - activeVotes.some(v => v.voter === currentUserName && v.percent > 0); - const postImage = (metaData, body) => { const imgTagRegex = /(]*>)/g; const markdownImageRegex = /!\[[^\]]*\]\((.*?)\s*("(?:.*[^"])")?\s*\)/g; @@ -98,21 +101,35 @@ const postImage = (metaData, body) => { return ''; }; -export const parseComments = (comments, currentUserName) => { - forEach(comments, comment => { - comment.pending_payout_value = parseFloat(comment.pending_payout_value).toFixed(3); - comment.vote_count = comment.active_votes.length; - comment.author_reputation = getReputation(comment.author_reputation); - comment.avatar = `https://steemitimages.com/u/${comment.author}/avatar/small`; - comment.markdownBody = comment.body; - comment.body = renderPostBody(comment); - comment.summary = `"${postBodySummary(comment, 100, true)}"`; +export const parseComments = (comments, currentUsername) => + !comments ? null : comments.map(comment => parseComment(comment, currentUsername)); - if (currentUserName) { - comment.is_voted = isVoted(comment.active_votes, currentUserName); - } else { - comment.is_voted = false; - } - }); - return comments; +export const parseComment = async (comment, currentUsername) => { + if (!comment) { + return null; + } + + const activeVotes = await getActiveVotes(get(comment, 'author'), get(comment, 'permlink')); + + comment.pending_payout_value = parseFloat( + get(comment, 'pending_payout_value') ? get(comment, 'pending_payout_value') : 0, + ).toFixed(3); + comment.author_reputation = getReputation(get(comment, 'author_reputation')); + comment.avatar = `https://steemitimages.com/u/${get(comment, 'author')}/avatar/small`; + comment.markdownBody = get(comment, 'body'); + comment.body = renderPostBody(comment); + comment.summary = `"${postBodySummary(comment, 100, true)}"`; + comment.active_votes = activeVotes; + comment.vote_count = activeVotes && activeVotes.length; + + if (currentUsername && activeVotes && activeVotes.length > 0) { + comment.is_voted = isVoted(activeVotes, currentUsername); + } else { + comment.is_voted = false; + } + + return comment; }; + +const isVoted = (activeVotes, currentUserName) => + activeVotes.some(v => v.voter === currentUserName && v.percent > 0);