Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Removes Redux state mutations - iteration 3 #23637

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions superset-frontend/src/components/Chart/ChartRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { snakeCase, isEqual } from 'lodash';
import { snakeCase, isEqual, cloneDeep } from 'lodash';
import PropTypes from 'prop-types';
import React from 'react';
import {
Expand Down Expand Up @@ -117,6 +117,11 @@ class ChartRenderer extends React.Component {
this.props.actions?.updateDataMask(this.props.chartId, dataMask);
},
};

// TODO: queriesResponse comes from Redux store but it's being edited by
// the plugins, hence we need to clone it to avoid state mutation
// until we change the reducers to use Redux Toolkit with Immer
this.mutableQueriesResponse = cloneDeep(this.props.queriesResponse);
}

shouldComponentUpdate(nextProps, nextState) {
Expand All @@ -131,6 +136,11 @@ class ChartRenderer extends React.Component {
}
this.hasQueryResponseChange =
nextProps.queriesResponse !== this.props.queriesResponse;

if (this.hasQueryResponseChange) {
this.mutableQueriesResponse = cloneDeep(nextProps.queriesResponse);
}

return (
this.hasQueryResponseChange ||
!isEqual(nextProps.datasource, this.props.datasource) ||
Expand Down Expand Up @@ -246,7 +256,6 @@ class ChartRenderer extends React.Component {
chartIsStale,
formData,
latestQueryFormData,
queriesResponse,
postTransformProps,
} = this.props;

Expand Down Expand Up @@ -339,7 +348,7 @@ class ChartRenderer extends React.Component {
filterState={filterState}
hooks={this.hooks}
behaviors={behaviors}
queriesData={queriesResponse}
queriesData={this.mutableQueriesResponse}
onRenderSuccess={this.handleRenderSuccess}
onRenderFailure={this.handleRenderFailure}
noResults={noResultsComponent}
Expand Down