Merge pull request #1056 from esteemapp/feature/downvote

Feature/downvote
This commit is contained in:
uğur erdal 2019-08-23 16:01:06 +03:00 committed by GitHub
commit 8ba29df957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 148 additions and 44 deletions

View File

@ -53,7 +53,12 @@ class UpvoteContainer extends PureComponent {
} = this.props; } = this.props;
const author = get(content, 'author'); const author = get(content, 'author');
const isVoted = get(content, 'is_voted'); const isVoted =
get(content, 'is_voted', false) && parseInt(get(content, 'is_voted'), 10) / 10000;
const isDownVoted =
get(content, 'is_down_voted', false) &&
(parseInt(get(content, 'is_down_voted'), 10) / 10000) * -1;
const totalPayout = get(content, 'total_payout'); const totalPayout = get(content, 'total_payout');
const isDecinedPayout = get(content, 'is_declined_payout'); const isDecinedPayout = get(content, 'is_declined_payout');
const permlink = get(content, 'permlink'); const permlink = get(content, 'permlink');
@ -80,6 +85,7 @@ class UpvoteContainer extends PureComponent {
isLoggedIn={isLoggedIn} isLoggedIn={isLoggedIn}
isShowPayoutValue={isShowPayoutValue} isShowPayoutValue={isShowPayoutValue}
isVoted={isVoted} isVoted={isVoted}
isDownVoted={isDownVoted}
payoutDate={payoutDate} payoutDate={payoutDate}
pendingPayout={pendingPayout} pendingPayout={pendingPayout}
permlink={permlink} permlink={permlink}

View File

@ -48,8 +48,8 @@ export default EStyleSheet.create({
marginLeft: 8, marginLeft: 8,
}, },
percent: { percent: {
width: 40,
color: '$primaryDarkGray', color: '$primaryDarkGray',
marginRight: 5,
}, },
slider: { slider: {
flex: 1, flex: 1,

View File

@ -30,11 +30,15 @@ class UpvoteView extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
sliderValue: get(props, 'upvotePercent', 1), sliderValue:
get(props, 'isVoted', false) ||
get(props, 'isDownVoted', 1) ||
get(props, 'upvotePercent', 1),
isVoting: false, isVoting: false,
isVoted: get(props, 'isVoted', false), isVoted: get(props, 'isVoted', false),
amount: '0.00000', amount: '0.00000',
isShowDetails: false, isShowDetails: false,
downvote: get(props, 'isDownVoted', false),
}; };
} }
@ -52,7 +56,12 @@ class UpvoteView extends Component {
} }
if (upvotePercent !== get(nextProps, 'upvotePercent')) { if (upvotePercent !== get(nextProps, 'upvotePercent')) {
this.setState({ sliderValue: get(nextProps, 'upvotePercent') }); this.setState({
sliderValue:
get(nextProps, 'isVoted', false) ||
get(nextProps, 'isDownVoted', 1) ||
get(nextProps, 'upvotePercent', 1),
});
} }
} }
@ -80,7 +89,7 @@ class UpvoteView extends Component {
} }
}; };
_upvoteContent = async () => { _upvoteContent = closePopover => {
const { const {
author, author,
currentAccount, currentAccount,
@ -89,40 +98,95 @@ class UpvoteView extends Component {
permlink, permlink,
pinCode, pinCode,
} = this.props; } = this.props;
const { sliderValue } = this.state; const { sliderValue, downvote } = this.state;
this.setState( if (!downvote) {
{ closePopover();
isVoting: true, this.setState(
}, {
() => { isVoting: true,
handleSetUpvotePercent(sliderValue); },
}, () => {
); handleSetUpvotePercent(sliderValue);
},
);
const weight = sliderValue ? (sliderValue * 100).toFixed(0) * 100 : 0; const weight = sliderValue ? (sliderValue * 100).toFixed(0) * 100 : 0;
vote(currentAccount, pinCode, author, permlink, weight) vote(currentAccount, pinCode, author, permlink, weight)
.then(() => { .then(() => {
this.setState( this.setState(
{ {
isVoted: !!sliderValue, isVoted: !!sliderValue,
isVoting: false,
},
() => {
if (fetchPost) {
fetchPost();
}
},
);
})
.catch(err => {
Alert.alert('Failed!', err.message);
this.setState({
isVoted: false,
isVoting: false, isVoting: false,
}, });
() => {
if (fetchPost) {
fetchPost();
}
},
);
})
.catch(err => {
Alert.alert('Failed!', err.message);
this.setState({
isVoted: false,
isVoting: false,
}); });
}); } else {
this.setState({ sliderValue: 1, downvote: false });
}
};
_downvoteContent = closePopover => {
const {
author,
currentAccount,
fetchPost,
handleSetUpvotePercent,
permlink,
pinCode,
} = this.props;
const { sliderValue, downvote } = this.state;
if (downvote) {
closePopover();
this.setState(
{
isVoting: true,
},
() => {
handleSetUpvotePercent(sliderValue);
},
);
const weight = sliderValue ? (sliderValue * 100).toFixed(0) * 100 * -1 : 0;
vote(currentAccount, pinCode, author, permlink, weight)
.then(() => {
this.setState(
{
isVoted: !!sliderValue,
isVoting: false,
},
() => {
if (fetchPost) {
fetchPost();
}
},
);
})
.catch(err => {
Alert.alert('Failed!', err.message);
this.setState({
isVoted: false,
isVoting: false,
});
});
} else {
this.setState({ sliderValue: 1, downvote: true });
}
}; };
_handleOnPopoverClose = () => { _handleOnPopoverClose = () => {
@ -145,8 +209,9 @@ class UpvoteView extends Component {
curationPayout, curationPayout,
payoutDate, payoutDate,
intl, intl,
isDownVoted,
} = this.props; } = this.props;
const { isVoting, amount, sliderValue, isVoted, isShowDetails } = this.state; const { isVoting, amount, sliderValue, isVoted, isShowDetails, downvote } = this.state;
let iconName = 'ios-arrow-dropup'; let iconName = 'ios-arrow-dropup';
let iconType; let iconType;
@ -156,9 +221,10 @@ class UpvoteView extends Component {
iconType = 'AntDesign'; iconType = 'AntDesign';
} }
const _percent = `${(sliderValue * 100).toFixed(0)}%`; const _percent = `${downvote ? '-' : ''}${(sliderValue * 100).toFixed(0)}%`;
const _amount = `$${amount}`; const _amount = `$${amount}`;
const _totalPayout = totalPayout || '0.000'; const _totalPayout = totalPayout || '0.000';
const sliderColor = downvote ? '#ec8b88' : '#357ce6';
return ( return (
<PopoverController> <PopoverController>
@ -185,10 +251,10 @@ class UpvoteView extends Component {
</View> </View>
) : ( ) : (
<Icon <Icon
style={[styles.upvoteIcon]} style={[styles.upvoteIcon, isDownVoted && { color: '#ec8b88' }]}
active={!isLoggedIn} active={!isLoggedIn}
iconType={iconType} iconType={isDownVoted ? 'AntDesign' : iconType}
name={iconName} name={isDownVoted ? 'downcircle' : iconName}
/> />
)} )}
</Fragment> </Fragment>
@ -253,8 +319,7 @@ class UpvoteView extends Component {
<Fragment> <Fragment>
<TouchableOpacity <TouchableOpacity
onPress={() => { onPress={() => {
closePopover(); this._upvoteContent(closePopover);
this._upvoteContent();
}} }}
style={styles.upvoteButton} style={styles.upvoteButton}
> >
@ -269,7 +334,7 @@ class UpvoteView extends Component {
<Text style={styles.amount}>{_amount}</Text> <Text style={styles.amount}>{_amount}</Text>
<Slider <Slider
style={styles.slider} style={styles.slider}
minimumTrackTintColor="#357ce6" minimumTrackTintColor={sliderColor}
trackStyle={styles.track} trackStyle={styles.track}
thumbStyle={styles.thumb} thumbStyle={styles.thumb}
thumbTintColor="#007ee5" thumbTintColor="#007ee5"
@ -281,6 +346,18 @@ class UpvoteView extends Component {
}} }}
/> />
<Text style={styles.percent}>{_percent}</Text> <Text style={styles.percent}>{_percent}</Text>
<TouchableOpacity
onPress={() => this._downvoteContent(closePopover)}
style={styles.upvoteButton}
>
<Icon
size={20}
style={[styles.upvoteIcon, { color: '#ec8b88' }]}
active={!isLoggedIn}
iconType="AntDesign"
name="downcircle"
/>
</TouchableOpacity>
</Fragment> </Fragment>
)} )}
</View> </View>

View File

@ -42,8 +42,10 @@ export const parsePost = async (post, currentUserName, isPromoted) => {
if (currentUserName) { if (currentUserName) {
post.is_voted = isVoted(post.active_votes, currentUserName); post.is_voted = isVoted(post.active_votes, currentUserName);
post.is_down_voted = isDownVoted(post.active_votes, currentUserName);
} else { } else {
post.is_voted = false; post.is_voted = false;
post.is_down_voted = false;
} }
const totalPayout = const totalPayout =
@ -133,8 +135,10 @@ export const parseComments = async (comments, currentUserName) => {
if (currentUserName && activeVotes && activeVotes.length > 0) { if (currentUserName && activeVotes && activeVotes.length > 0) {
comment.is_voted = isVoted(activeVotes, currentUserName); comment.is_voted = isVoted(activeVotes, currentUserName);
comment.is_down_voted = isDownVoted(comment.active_votes, currentUserName);
} else { } else {
comment.is_voted = false; comment.is_voted = false;
comment.is_down_voted = false;
} }
return comment; return comment;
}); });
@ -144,5 +148,22 @@ export const parseComments = async (comments, currentUserName) => {
return _comments; return _comments;
}; };
const isVoted = (activeVotes, currentUserName) => const isVoted = (activeVotes, currentUserName) => {
activeVotes.some(v => v.voter === currentUserName && v.percent > 0); const result = activeVotes.find(
element => get(element, 'voter') === currentUserName && get(element, 'percent', 0) > 0,
);
if (result) {
return result.percent;
}
return false;
};
const isDownVoted = (activeVotes, currentUserName) => {
const result = activeVotes.find(
element => get(element, 'voter') === currentUserName && get(element, 'percent') < 0,
);
if (result) {
return result.percent;
}
return false;
};