From 6a9d45d91d07e66d8f744aae37d2f1030cdb9894 Mon Sep 17 00:00:00 2001 From: slawiko Date: Tue, 26 Jun 2018 22:31:46 +0300 Subject: [PATCH] 112: refactors comment component a bit - replaces few onDo/onUndo methods with their toggle equivalent --- web/app/components/comment/comment.jsx | 83 +++++++------------------- 1 file changed, 23 insertions(+), 60 deletions(-) diff --git a/web/app/components/comment/comment.jsx b/web/app/components/comment/comment.jsx index 39d8ede2..3cf7aaa4 100644 --- a/web/app/components/comment/comment.jsx +++ b/web/app/components/comment/comment.jsx @@ -35,10 +35,8 @@ export default class Comment extends Component { this.onReply = this.onReply.bind(this); this.onPinClick = this.onPinClick.bind(this); this.onUnpinClick = this.onUnpinClick.bind(this); - this.onVerifyClick = this.onVerifyClick.bind(this); - this.onUnverifyClick = this.onUnverifyClick.bind(this); - this.onBlockClick = this.onBlockClick.bind(this); - this.onUnblockClick = this.onUnblockClick.bind(this); + this.toggleVerify = this.toggleVerify.bind(this); + this.toggleBlock = this.toggleBlock.bind(this); this.onDeleteClick = this.onDeleteClick.bind(this); } @@ -155,51 +153,29 @@ export default class Comment extends Component { } } - onVerifyClick() { + toggleVerify(isVerified) { const { id, user: { id: userId } } = this.props.data; + const promptMessage = isVerified ? 'Do you want to verify this user?' : 'Do you want to verify this user?'; - if (confirm('Do you want to verify this user?')) { - this.setState({ isUserVerified: true }); + if (confirm(promptMessage)) { + this.setState({ isUserVerified: !isVerified }); - api.setVerifyStatus({ id: userId }).then(() => { - api.getComment({ id }).then(comment => store.replaceComment(comment)); - }); + (isVerified ? api.removeVerifyStatus : api.setVerifyStatus)({ id: userId }) + .then(api.getComment({ id })) + .then(comment => store.replaceComment(comment)); } } - onUnverifyClick() { + toggleBlock(isBlocked) { const { id, user: { id: userId } } = this.props.data; + const promptMessage = isBlocked ? 'Do you want to unblock this user?' : 'Do you want to block this user?'; - if (confirm('Do you want to unverify this user?')) { - this.setState({ isUserVerified: false }); + if (confirm(promptMessage)) { + this.setState({ userBlocked: !isBlocked }); - api.removeVerifyStatus({ id: userId }).then(() => { - api.getComment({ id }).then(comment => store.replaceComment(comment)); - }); - } - } - - onBlockClick() { - const { id, user: { id: userId } } = this.props.data; - - if (confirm('Do you want to block this user?')) { - this.setState({ userBlocked: true }); - - api.blockUser({ id: userId }).then(() => { - api.getComment({ id }).then(comment => store.replaceComment(comment)); - }); - } - } - - onUnblockClick() { - const { id, user: { id: userId } } = this.props.data; - - if (confirm('Do you want to unblock this user?')) { - this.setState({ userBlocked: false }); - - api.unblockUser({ id: userId }).then(() => { - api.getComment({ id }).then(comment => store.replaceComment(comment)); - }); + (isBlocked ? api.unblockUser : api.blockUser)({ id: userId }) + .then(api.getComment({ id })) + .then(comment => store.replaceComment(comment)); } } @@ -413,7 +389,7 @@ export default class Comment extends Component { { isAdmin && mods.view !== 'user' && ( this.toggleVerify(o.user.verified)} aria-label="Toggle verification" title={o.user.verified ? 'Verified user' : 'Unverified user'} className={b('comment__verification', {}, { active: o.user.verified, clickable: true })} @@ -546,25 +522,12 @@ export default class Comment extends Component { } { - userBlocked && ( - Unblock - ) - } - - { - !userBlocked && ( - Block - ) + this.toggleBlock(userBlocked)} + >Unblock } {