From bf4db744d2e62fad765ff04ac2dfb49bc06760ec Mon Sep 17 00:00:00 2001 From: slawiko Date: Tue, 26 Jun 2018 22:59:04 +0300 Subject: [PATCH] 112: refactors comment component a bit - replaces few onDo/onUndo methods with their toggle equivalent part 2 --- web/app/components/comment/comment.jsx | 69 ++++++++------------------ 1 file changed, 20 insertions(+), 49 deletions(-) diff --git a/web/app/components/comment/comment.jsx b/web/app/components/comment/comment.jsx index 3cf7aaa4..833f9bb6 100644 --- a/web/app/components/comment/comment.jsx +++ b/web/app/components/comment/comment.jsx @@ -33,8 +33,7 @@ export default class Comment extends Component { this.scrollToParent = this.scrollToParent.bind(this); this.onEdit = this.onEdit.bind(this); this.onReply = this.onReply.bind(this); - this.onPinClick = this.onPinClick.bind(this); - this.onUnpinClick = this.onUnpinClick.bind(this); + this.togglePin = this.togglePin.bind(this); this.toggleVerify = this.toggleVerify.bind(this); this.toggleBlock = this.toggleBlock.bind(this); this.onDeleteClick = this.onDeleteClick.bind(this); @@ -129,27 +128,16 @@ export default class Comment extends Component { this.setState({ isUserInfoShown: !this.state.isUserInfoShown }); } - onPinClick() { + togglePin(isPinned) { const { id } = this.props.data; + const promptMessage = isPinned ? 'Do you want to pin this comment?' : 'Do you want to pin this comment?'; - if (confirm('Do you want to pin this comment?')) { - this.setState({ pinned: true }); + if (confirm(promptMessage)) { + this.setState({ pinned: !isPinned }); - api.pinComment({ id, url }).then(() => { - api.getComment({ id }).then(comment => store.replaceComment(comment)); - }); - } - } - - onUnpinClick() { - const { id } = this.props.data; - - if (confirm('Do you want to unpin this comment?')) { - this.setState({ pinned: false }); - - api.unpinComment({ id, url }).then(() => { - api.getComment({ id }).then(comment => store.replaceComment(comment)); - }); + (isPinned ? api.unpinComment : api.pinComment)({ id, url }) + .then(api.getComment({ id })) + .then(comment => store.replaceComment(comment)); } } @@ -499,36 +487,19 @@ export default class Comment extends Component { { !deleted && isAdmin && ( - { - !pinned && ( - Pin - ) - } + this.togglePin(pinned)} + >{pinned ? 'Unpin' : 'Pin'} - { - pinned && ( - Unpin - ) - } - - { - this.toggleBlock(userBlocked)} - >Unblock - } + this.toggleBlock(userBlocked)} + >{userBlocked ? 'Unblock' : 'Block'} { !deleted && (