Merge pull request #1884 from ecency/upvote-refresh-fix

Upvote refresh fix
This commit is contained in:
Feruz M 2021-03-30 17:58:02 +03:00 committed by GitHub
commit ea22d79553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 38 deletions

View File

@ -30,16 +30,8 @@ const PostCardContainer = ({
imageHeight,
setImageHeight,
}) => {
const [_content, setContent] = useState(content);
const [reblogs, setReblogs] = useState([]);
const [activeVotes, setActiveVotes] = useState(get(_content, 'active_votes', []));
//NOTE: potentially unnessacry fetch
// useEffect(() => {
// if (isRefresh) {
// _fetchPost();
// }
// }, [isRefresh]);
const activeVotes = get(content, 'active_votes', []);
useEffect(() => {
let isCancelled = false;
@ -59,24 +51,24 @@ const PostCardContainer = ({
}
};
if (_content) {
fetchData(_content);
if (content) {
fetchData(content);
}
return () => {
isCancelled = true;
};
}, [_content]);
}, [content]);
const _handleOnUserPress = () => {
if (_content && get(currentAccount, 'name') !== get(_content, 'author')) {
if (content && get(currentAccount, 'name') !== get(content, 'author')) {
navigation.navigate({
routeName: ROUTES.SCREENS.PROFILE,
params: {
username: get(_content, 'author'),
reputation: get(_content, 'author_reputation'),
username: get(content, 'author'),
reputation: get(content, 'author_reputation'),
},
key: get(_content, 'author'),
key: get(content, 'author'),
});
}
};
@ -98,9 +90,9 @@ const PostCardContainer = ({
routeName: ROUTES.SCREENS.VOTERS,
params: {
activeVotes,
content: _content,
content: content,
},
key: get(_content, 'permlink'),
key: get(content, 'permlink'),
});
};
@ -110,33 +102,17 @@ const PostCardContainer = ({
params: {
reblogs,
},
key: get(_content, 'permlink', get(_content, 'author', '')),
key: get(content, 'permlink', get(content, 'author', '')),
});
};
const _fetchPost = async () => {
await getPost(
get(_content, 'author'),
get(_content, 'permlink'),
get(currentAccount, 'username'),
)
.then((result) => {
if (result) {
setContent(result);
setActiveVotes(get(result, 'active_votes', []));
}
})
.catch(() => {});
};
return (
<PostCardView
handleOnUserPress={_handleOnUserPress}
handleOnContentPress={_handleOnContentPress}
handleOnVotersPress={_handleOnVotersPress}
handleOnReblogsPress={_handleOnReblogsPress}
fetchPost={_fetchPost}
content={_content || content}
content={content}
isHideImage={isHideImage}
isNsfwPost={nsfw || '1'}
reblogs={reblogs}

View File

@ -40,11 +40,15 @@ const PostCardView = ({
imageHeight,
setImageHeight,
}) => {
const [activeVotesCount, setActiveVotesCount] = useState(activeVotes.length || 0);
//local state to manage fake upvote if available
const [activeVotesCount, setActiveVotesCount] = useState(0);
const [calcImgHeight, setCalcImgHeight] = useState(imageHeight || 300);
// Component Functions
useEffect(() => {
setActiveVotesCount(activeVotes ? activeVotes.length : 0);
}, [activeVotes]);
// Component Functions
const _handleOnUserPress = () => {
if (handleOnUserPress) {
handleOnUserPress();
@ -66,6 +70,7 @@ const PostCardView = ({
};
const _handleIncrementVoteCount = () => {
//fake increment vote using based on local change
setActiveVotesCount(activeVotesCount + 1);
};