From f9e289561e4ed3335b2d12fc75047b866f85af02 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 21 Dec 2017 18:29:18 +0100 Subject: [PATCH] Refactor --- .../Moderation/components/ModerationQueue.js | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/client/coral-admin/src/routes/Moderation/components/ModerationQueue.js b/client/coral-admin/src/routes/Moderation/components/ModerationQueue.js index 59c3b8b7e..72df10682 100644 --- a/client/coral-admin/src/routes/Moderation/components/ModerationQueue.js +++ b/client/coral-admin/src/routes/Moderation/components/ModerationQueue.js @@ -195,22 +195,17 @@ class ModerationQueue extends React.Component { // Reflow virtual list. this.reflowList(); - - // Scroll to selected comment. - if (this.props.selectedCommentId) { - const view = this.state.view; - const index = view.findIndex(({id}) => id === selectedCommentId); - this.listRef.scrollToRow(index); - } } - // Switching to single mode. - if (!prev.singleView && this.props.singleView) { + if ( - // Scroll to comment. - if (this.props.selectedCommentId) { - document.querySelector(`#comment_${this.props.selectedCommentId}`).scrollIntoView(); - } + // Switching mode. + prev.singleView !== this.props.singleView || + + // Select different comment. + prev.selectedCommentId !== selectedCommentId && selectedCommentId + ) { + this.scrollToSelectedComment(); } // If the user just moderated the last (visible) comment @@ -219,15 +214,6 @@ class ModerationQueue extends React.Component { if (prev.comments.length > 0 && this.getCommentCountWithoutDagling() === 0 && commentCount > 0) { this.props.loadMore(); } - - // Scroll to new selected comment. - if (prev.selectedCommentId !== selectedCommentId && this.listRef) { - - const view = this.state.view; - const index = view.findIndex(({id}) => id === selectedCommentId); - - this.listRef.scrollToRow(index); - } } // Returns comment counts without dangling comments. @@ -265,6 +251,17 @@ class ModerationQueue extends React.Component { this.listRef = list; }; + scrollToSelectedComment = (props = this.props, state = this.state) => { + if (props.singleMode) { + document.querySelector(`#comment_${props.selectedCommentId}`).scrollIntoView(); + } + else if(this.listRef) { + const view = state.view; + const index = view.findIndex(({id}) => id === props.selectedCommentId); + this.listRef.scrollToRow(index); + } + } + viewNewComments = (callback) => { this.setState(resetCursors, () => { this.reflowList();