From 440093ebe9ca6b6908e21c34e01c83cc794ab32e Mon Sep 17 00:00:00 2001 From: feruz Date: Fri, 16 Oct 2020 00:51:16 +0300 Subject: [PATCH] WIP voters info improvements fetch on click --- .../postCard/container/postCardContainer.js | 12 +++------ src/components/postCard/view/postCardView.js | 7 +++++- .../votersDisplay/view/votersDisplayView.js | 9 +++++++ src/screens/voters/screen/votersScreen.js | 25 +++++++++++++++++-- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/components/postCard/container/postCardContainer.js b/src/components/postCard/container/postCardContainer.js index 737a70ede..95d41c2df 100644 --- a/src/components/postCard/container/postCardContainer.js +++ b/src/components/postCard/container/postCardContainer.js @@ -29,6 +29,7 @@ const PostCardContainer = ({ nsfw, }) => { const [activeVotes, setActiveVotes] = useState([]); + const [totalVotes, setTotalVotes] = useState(0); const [reblogs, setReblogs] = useState([]); const [_content, setContent] = useState(null); @@ -40,14 +41,7 @@ const PostCardContainer = ({ useEffect(() => { if (content) { - getActiveVotes(get(content, 'author'), get(content, 'permlink')) - .then((result) => { - result.sort((a, b) => b.rshares - a.rshares); - - const _votes = parseActiveVotes({ ...content, active_votes: result }); - setActiveVotes(_votes); - }) - .catch(() => {}); + setTotalVotes(get(content, 'stats.total_votes', 0)); getPostReblogs(content).then((result) => { setReblogs(result); @@ -85,6 +79,7 @@ const PostCardContainer = ({ routeName: ROUTES.SCREENS.VOTERS, params: { activeVotes, + content, }, key: get(content, 'permlink'), }); @@ -121,6 +116,7 @@ const PostCardContainer = ({ isHideImage={isHideImage} isNsfwPost={nsfw === '1'} activeVotes={activeVotes} + totalVotes={totalVotes} reblogs={reblogs} /> ); diff --git a/src/components/postCard/view/postCardView.js b/src/components/postCard/view/postCardView.js index 88c24a0c6..6e03aa617 100644 --- a/src/components/postCard/view/postCardView.js +++ b/src/components/postCard/view/postCardView.js @@ -33,9 +33,11 @@ const PostCardView = ({ isNsfwPost, intl, activeVotes, + totalVotes, }) => { const [rebloggedBy, setRebloggedBy] = useState(get(content, 'reblogged_by[0]', null)); const [activeVot, setActiveVot] = useState(activeVotes); + const [netVotes, setNetVotes] = useState(totalVotes); // Component Functions @@ -77,6 +79,9 @@ const PostCardView = ({ if (activeVotes) { setActiveVot(get(content, 'active_votes')); } + if (totalVotes) { + setNetVotes(totalVotes); + } }, [content]); const _image = _getPostImage(content, isNsfwPost); @@ -132,7 +137,7 @@ const PostCardView = ({ iconStyle={styles.commentIcon} iconType="MaterialCommunityIcons" isClickable - text={activeVot.length} + text={netVotes} onPress={_handleOnVotersPress} /> diff --git a/src/components/votersDisplay/view/votersDisplayView.js b/src/components/votersDisplay/view/votersDisplayView.js index fcd28558d..de7a337aa 100644 --- a/src/components/votersDisplay/view/votersDisplayView.js +++ b/src/components/votersDisplay/view/votersDisplayView.js @@ -18,6 +18,15 @@ import styles from './votersDisplayStyles'; const VotersDisplayView = ({ votes, navigation }) => { const intl = useIntl(); + /*getActiveVotes(get(content, 'author'), get(content, 'permlink')) + .then((result) => { + result.sort((a, b) => b.rshares - a.rshares); + + const _votes = parseActiveVotes({ ...content, active_votes: result }); + setActiveVotes(_votes); + }) + .catch(() => {});*/ + const _handleOnUserPress = (username) => { navigation.navigate({ routeName: ROUTES.SCREENS.PROFILE, diff --git a/src/screens/voters/screen/votersScreen.js b/src/screens/voters/screen/votersScreen.js index 7ab64820c..a9ca3432c 100644 --- a/src/screens/voters/screen/votersScreen.js +++ b/src/screens/voters/screen/votersScreen.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import { SafeAreaView } from 'react-native'; import { useIntl } from 'react-intl'; import get from 'lodash/get'; @@ -10,16 +10,37 @@ import AccountListContainer from '../../../containers/accountListContainer'; // Utils import globalStyles from '../../../globalStyles'; +import { getPost } from '../../../providers/steem/dsteem'; +import { parseActiveVotes } from '../../../utils/postParser'; const filterOptions = ['rewards', 'percent', 'time']; const VotersScreen = ({ navigation }) => { const intl = useIntl(); + const [activeVotes, setActiveVotes] = useState([]); + const [content, setContent] = useState(get(navigation, 'state.params.content')); + const [isLoading, setIsLoading] = useState(false); + const headerTitle = intl.formatMessage({ id: 'voters.voters_info', }); - const activeVotes = get(navigation, 'state.params.activeVotes'); + useEffect(() => { + if (content) { + getPost(content.author, content.permlink) + .then((items) => { + items.active_votes.sort((a, b) => b.rshares - a.rshares); + const _votes = parseActiveVotes(items); + setActiveVotes(_votes); + }) + .catch((err) => { + console.error(err.message); + }); + } + }, []); + + //const activeVotes = get(navigation, 'state.params.activeVotes'); + //const content = get(navigation, 'state.params.content'); return (