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

View File

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