From f5db5c38811f4bcb299dcd60b7109ce5b330cb9a Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Thu, 20 Sep 2018 10:14:12 +0200 Subject: [PATCH 1/2] Refactor long if block in getItems in listings [MAILPOET-1537] --- assets/js/src/listing/listing.jsx | 87 +++++++++++++++---------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/assets/js/src/listing/listing.jsx b/assets/js/src/listing/listing.jsx index f754651461..b2e2eb1617 100644 --- a/assets/js/src/listing/listing.jsx +++ b/assets/js/src/listing/listing.jsx @@ -456,55 +456,54 @@ const Listing = React.createClass({ this.initWithParams(params); }, getItems: function getItems() { - if (this.isComponentMounted) { - this.setState({ loading: true }); + if (!this.isComponentMounted) return; - this.clearSelection(); + this.setState({ loading: true }); + this.clearSelection(); - MailPoet.Ajax.post({ - api_version: window.mailpoet_api_version, - endpoint: this.props.endpoint, - action: 'listing', - data: { - params: this.getParams(), - offset: (this.state.page - 1) * this.state.limit, - limit: this.state.limit, - group: this.state.group, - filter: this.state.filter, - search: this.state.search, - sort_by: this.state.sort_by, - sort_order: this.state.sort_order, - }, - }).always(() => { - this.setState({ loading: false }); - }).done((response) => { - this.setState({ - items: response.data || [], - filters: response.meta.filters || {}, - groups: response.meta.groups || [], - count: response.meta.count || 0, - meta: _.omit(response.meta, ['filters', 'groups', 'count']), - }, () => { - // if viewing an empty trash - if (this.state.group === 'trash' && response.meta.count === 0) { - // redirect to default group - this.handleGroup('all'); - } + MailPoet.Ajax.post({ + api_version: window.mailpoet_api_version, + endpoint: this.props.endpoint, + action: 'listing', + data: { + params: this.getParams(), + offset: (this.state.page - 1) * this.state.limit, + limit: this.state.limit, + group: this.state.group, + filter: this.state.filter, + search: this.state.search, + sort_by: this.state.sort_by, + sort_order: this.state.sort_order, + }, + }).always(() => { + this.setState({ loading: false }); + }).done((response) => { + this.setState({ + items: response.data || [], + filters: response.meta.filters || {}, + groups: response.meta.groups || [], + count: response.meta.count || 0, + meta: _.omit(response.meta, ['filters', 'groups', 'count']), + }, () => { + // if viewing an empty trash + if (this.state.group === 'trash' && response.meta.count === 0) { + // redirect to default group + this.handleGroup('all'); + } - // trigger afterGetItems callback if specified - if (this.props.afterGetItems !== undefined) { - this.props.afterGetItems(this.state); - } - }); - }).fail((response) => { - if (response.errors.length > 0) { - MailPoet.Notice.error( - response.errors.map(error => error.message), - { scroll: true } - ); + // trigger afterGetItems callback if specified + if (this.props.afterGetItems !== undefined) { + this.props.afterGetItems(this.state); } }); - } + }).fail((response) => { + if (response.errors.length > 0) { + MailPoet.Notice.error( + response.errors.map(error => error.message), + { scroll: true } + ); + } + }); }, handleRestoreItem: function handleRestoreItem(id) { this.setState({ From 4ae1f4a107eb798b8bba9404789aa114cd4c17d1 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Thu, 20 Sep 2018 10:16:28 +0200 Subject: [PATCH 2/2] Prevent setting state on unmounted listing component [MAILPOET-1537] --- assets/js/src/listing/listing.jsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/assets/js/src/listing/listing.jsx b/assets/js/src/listing/listing.jsx index b2e2eb1617..983bb52b4e 100644 --- a/assets/js/src/listing/listing.jsx +++ b/assets/js/src/listing/listing.jsx @@ -476,8 +476,10 @@ const Listing = React.createClass({ sort_order: this.state.sort_order, }, }).always(() => { + if (!this.isComponentMounted) return; this.setState({ loading: false }); }).done((response) => { + if (!this.isComponentMounted) return; this.setState({ items: response.data || [], filters: response.meta.filters || {},