From fc14e9d2fd97b4c0e61328fdbbf98df502e6a7d8 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Wed, 21 Jun 2017 01:27:01 +0700 Subject: [PATCH] Prevent double execution of fetchMore --- .../Moderation/components/ModerationQueue.js | 11 +++++++- .../src/components/Comment.js | 26 ++++++++++++++----- .../src/components/LoadMore.js | 12 +++++---- .../src/components/Stream.js | 16 +++++++++++- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/client/coral-admin/src/routes/Moderation/components/ModerationQueue.js b/client/coral-admin/src/routes/Moderation/components/ModerationQueue.js index 490c2a985..d656c7a82 100644 --- a/client/coral-admin/src/routes/Moderation/components/ModerationQueue.js +++ b/client/coral-admin/src/routes/Moderation/components/ModerationQueue.js @@ -8,6 +8,7 @@ import LoadMore from './LoadMore'; import t from 'coral-framework/services/i18n'; class ModerationQueue extends React.Component { + isLoadingMore = false; static propTypes = { viewUserDetail: PropTypes.func.isRequired, @@ -22,7 +23,15 @@ class ModerationQueue extends React.Component { } loadMore = () => { - this.props.loadMore(this.props.activeTab); + if (!this.isLoadingMore) { + this.isLoadingMore = true; + this.props.loadMore(this.props.activeTab) + .then(() => this.isLoadingMore = false) + .catch((e) => { + this.isLoadingMore = false; + throw e; + }); + } } componentDidUpdate (prev) { diff --git a/client/coral-embed-stream/src/components/Comment.js b/client/coral-embed-stream/src/components/Comment.js index f14e4c991..53ac52edf 100644 --- a/client/coral-embed-stream/src/components/Comment.js +++ b/client/coral-embed-stream/src/components/Comment.js @@ -75,6 +75,8 @@ const ActionButton = ({children}) => { }; export default class Comment extends React.Component { + isLoadingReplies = false; + constructor(props) { super(props); @@ -210,14 +212,24 @@ export default class Comment extends React.Component { } loadNewReplies = () => { - const {replies, replyCount, id} = this.props.comment; - if (replyCount > replies.nodes.length) { - this.props.loadMore(id).then(() => { - this.setState(resetCursors(this.state, this.props)); - }); - return; + if (!this.isLoadingReplies) { + this.isLoadingReplies = true; + const {replies, replyCount, id} = this.props.comment; + if (replyCount > replies.nodes.length) { + this.props.loadMore(id) + .then(() => { + this.setState(resetCursors(this.state, this.props)); + this.isLoadingReplies = false; + }) + .catch((e) => { + this.isLoadingReplies = false; + throw e; + }); + return; + } + this.setState(resetCursors); + this.isLoadingReplies = false; } - this.setState(resetCursors); }; showReplyBox = () => { diff --git a/client/coral-embed-stream/src/components/LoadMore.js b/client/coral-embed-stream/src/components/LoadMore.js index ca2816d10..3d1ad4679 100644 --- a/client/coral-embed-stream/src/components/LoadMore.js +++ b/client/coral-embed-stream/src/components/LoadMore.js @@ -23,15 +23,17 @@ class LoadMore extends React.Component { } } + loadMore = () => { + this.initialState = false; + this.props.loadMore(); + } + render () { - const {topLevel, moreComments, loadMore, replyCount} = this.props; + const {topLevel, moreComments, replyCount} = this.props; return moreComments ?
diff --git a/client/coral-embed-stream/src/components/Stream.js b/client/coral-embed-stream/src/components/Stream.js index 62a58129b..4519f6417 100644 --- a/client/coral-embed-stream/src/components/Stream.js +++ b/client/coral-embed-stream/src/components/Stream.js @@ -53,6 +53,8 @@ function invalidateCursor(invalidated, state, props) { class Stream extends React.Component { + isLoadingMoreMore = false; + constructor(props) { super(props); this.state = resetCursors(this.state, props); @@ -95,6 +97,18 @@ class Stream extends React.Component { } }; + loadMoreComments = () => { + if (!this.isLoadingMore) { + this.isLoadingMore = true; + this.props.loadMoreComments() + .then(() => this.isLoadingMore = false) + .catch((e) => { + this.isLoadingMore = false; + throw e; + }); + } + } + // getVisibileComments returns a list containing comments // which were authored by current user or comes after the `idCursor`. getVisibleComments() { @@ -288,7 +302,7 @@ class Stream extends React.Component { }