Skip to content

Commit 7f38405

Browse files
authored
chore(react18): Migrate legacy react methods (#34892)
1 parent 0defcb6 commit 7f38405

File tree

8 files changed

+107
-108
lines changed

8 files changed

+107
-108
lines changed

superset-frontend/.eslintrc.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,6 @@ module.exports = {
413413
'icons/no-fa-icons-usage': 'error',
414414
'i18n-strings/no-template-vars': ['error', true],
415415
'i18n-strings/sentence-case-buttons': 'error',
416-
camelcase: [
417-
'error',
418-
{
419-
allow: ['^UNSAFE_'],
420-
properties: 'never',
421-
},
422-
],
423416
'class-methods-use-this': 0,
424417
curly: 2,
425418
'func-names': 0,

superset-frontend/src/components/Datasource/components/CollectionTable/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ export default class CRUDCollection extends PureComponent<
104104
this.toggleExpand = this.toggleExpand.bind(this);
105105
}
106106

107-
UNSAFE_componentWillReceiveProps(nextProps: CRUDCollectionProps) {
108-
if (nextProps.collection !== this.props.collection) {
107+
componentDidUpdate(prevProps: CRUDCollectionProps) {
108+
if (this.props.collection !== prevProps.collection) {
109109
const { collection, collectionArray } = createKeyedCollection(
110-
nextProps.collection,
110+
this.props.collection,
111111
);
112112
this.setState(prevState => ({
113113
collection,

superset-frontend/src/dashboard/components/Dashboard.jsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,12 @@ class Dashboard extends PureComponent {
120120
this.applyCharts();
121121
}
122122

123-
componentDidUpdate() {
123+
componentDidUpdate(prevProps) {
124124
this.applyCharts();
125-
}
126-
127-
UNSAFE_componentWillReceiveProps(nextProps) {
128-
const currentChartIds = getChartIdsFromLayout(this.props.layout);
129-
const nextChartIds = getChartIdsFromLayout(nextProps.layout);
125+
const currentChartIds = getChartIdsFromLayout(prevProps.layout);
126+
const nextChartIds = getChartIdsFromLayout(this.props.layout);
130127

131-
if (this.props.dashboardId !== nextProps.dashboardId) {
128+
if (prevProps.dashboardId !== this.props.dashboardId) {
132129
// single-page-app navigation check
133130
return;
134131
}
@@ -140,7 +137,7 @@ class Dashboard extends PureComponent {
140137
newChartIds.forEach(newChartId =>
141138
this.props.actions.addSliceToDashboard(
142139
newChartId,
143-
getLayoutComponentFromChartId(nextProps.layout, newChartId),
140+
getLayoutComponentFromChartId(this.props.layout, newChartId),
144141
),
145142
);
146143
} else if (currentChartIds.length > nextChartIds.length) {

superset-frontend/src/dashboard/components/SliceAdder.tsx

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ export function sortByComparator(attr: keyof Slice) {
155155
};
156156
}
157157

158+
function getFilteredSortedSlices(
159+
slices: SliceAdderProps['slices'],
160+
searchTerm: string,
161+
sortBy: keyof Slice,
162+
showOnlyMyCharts: boolean,
163+
userId: number,
164+
) {
165+
return Object.values(slices)
166+
.filter(slice =>
167+
showOnlyMyCharts
168+
? slice?.owners?.find(owner => owner.id === userId) ||
169+
slice?.created_by?.id === userId
170+
: true,
171+
)
172+
.filter(createFilter(searchTerm, KEYS_TO_FILTERS))
173+
.sort(sortByComparator(sortBy));
174+
}
158175
class SliceAdder extends Component<SliceAdderProps, SliceAdderState> {
159176
private slicesRequest?: AbortController | Promise<void>;
160177

@@ -195,19 +212,20 @@ class SliceAdder extends Component<SliceAdderProps, SliceAdderState> {
195212
);
196213
}
197214

198-
UNSAFE_componentWillReceiveProps(nextProps: SliceAdderProps) {
215+
componentDidUpdate(prevProps: SliceAdderProps) {
199216
const nextState: SliceAdderState = {} as SliceAdderState;
200-
if (nextProps.lastUpdated !== this.props.lastUpdated) {
201-
nextState.filteredSlices = this.getFilteredSortedSlices(
202-
nextProps.slices,
217+
if (this.props.lastUpdated !== prevProps.lastUpdated) {
218+
nextState.filteredSlices = getFilteredSortedSlices(
219+
this.props.slices,
203220
this.state.searchTerm,
204221
this.state.sortBy,
205222
this.state.showOnlyMyCharts,
223+
this.props.userId,
206224
);
207225
}
208226

209-
if (nextProps.selectedSliceIds !== this.props.selectedSliceIds) {
210-
nextState.selectedSliceIdsSet = new Set(nextProps.selectedSliceIds);
227+
if (prevProps.selectedSliceIds !== this.props.selectedSliceIds) {
228+
nextState.selectedSliceIdsSet = new Set(this.props.selectedSliceIds);
211229
}
212230

213231
if (Object.keys(nextState).length) {
@@ -227,23 +245,6 @@ class SliceAdder extends Component<SliceAdderProps, SliceAdderState> {
227245
}
228246
}
229247

230-
getFilteredSortedSlices(
231-
slices: SliceAdderProps['slices'],
232-
searchTerm: string,
233-
sortBy: keyof Slice,
234-
showOnlyMyCharts: boolean,
235-
) {
236-
return Object.values(slices)
237-
.filter(slice =>
238-
showOnlyMyCharts
239-
? slice?.owners?.find(owner => owner.id === this.props.userId) ||
240-
slice?.created_by?.id === this.props.userId
241-
: true,
242-
)
243-
.filter(createFilter(searchTerm, KEYS_TO_FILTERS))
244-
.sort(sortByComparator(sortBy));
245-
}
246-
247248
handleChange = debounce(value => {
248249
this.searchUpdated(value);
249250
this.slicesRequest = this.props.fetchSlices(
@@ -256,23 +257,25 @@ class SliceAdder extends Component<SliceAdderProps, SliceAdderState> {
256257
searchUpdated(searchTerm: string) {
257258
this.setState(prevState => ({
258259
searchTerm,
259-
filteredSlices: this.getFilteredSortedSlices(
260+
filteredSlices: getFilteredSortedSlices(
260261
this.props.slices,
261262
searchTerm,
262263
prevState.sortBy,
263264
prevState.showOnlyMyCharts,
265+
this.props.userId,
264266
),
265267
}));
266268
}
267269

268270
handleSelect(sortBy: keyof Slice) {
269271
this.setState(prevState => ({
270272
sortBy,
271-
filteredSlices: this.getFilteredSortedSlices(
273+
filteredSlices: getFilteredSortedSlices(
272274
this.props.slices,
273275
prevState.searchTerm,
274276
sortBy,
275277
prevState.showOnlyMyCharts,
278+
this.props.userId,
276279
),
277280
}));
278281
this.slicesRequest = this.props.fetchSlices(
@@ -340,11 +343,12 @@ class SliceAdder extends Component<SliceAdderProps, SliceAdderState> {
340343
}
341344
this.setState(prevState => ({
342345
showOnlyMyCharts,
343-
filteredSlices: this.getFilteredSortedSlices(
346+
filteredSlices: getFilteredSortedSlices(
344347
this.props.slices,
345348
prevState.searchTerm,
346349
prevState.sortBy,
347350
showOnlyMyCharts,
351+
this.props.userId,
348352
),
349353
}));
350354
setItem(LocalStorageKeys.DashboardEditorShowOnlyMyCharts, showOnlyMyCharts);

superset-frontend/src/dashboard/components/menu/WithPopoverMenu.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ export default class WithPopoverMenu extends PureComponent<
129129
this.handleClick = this.handleClick.bind(this);
130130
}
131131

132-
UNSAFE_componentWillReceiveProps(nextProps: WithPopoverMenuProps) {
133-
if (nextProps.editMode && nextProps.isFocused && !this.state.isFocused) {
132+
componentDidUpdate(prevProps: WithPopoverMenuProps) {
133+
if (this.props.editMode && this.props.isFocused && !this.state.isFocused) {
134134
document.addEventListener('click', this.handleClick);
135135
document.addEventListener('drag', this.handleClick);
136136
this.setState({ isFocused: true });
137-
} else if (this.state.isFocused && !nextProps.editMode) {
137+
} else if (this.state.isFocused && !this.props.editMode) {
138138
document.removeEventListener('click', this.handleClick);
139139
document.removeEventListener('drag', this.handleClick);
140140
this.setState({ isFocused: false });

superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,23 @@ class AnnotationLayerControl extends PureComponent<Props, PopoverState> {
107107
AnnotationLayer.preload();
108108
}
109109

110-
UNSAFE_componentWillReceiveProps(nextProps: Props) {
111-
const { name, annotationError, validationErrors, value } = nextProps;
112-
if (Object.keys(annotationError).length && !validationErrors.length) {
113-
this.props.actions.setControlValue(
114-
name,
115-
value,
116-
Object.keys(annotationError),
117-
);
118-
}
119-
if (!Object.keys(annotationError).length && validationErrors.length) {
120-
this.props.actions.setControlValue(name, value, []);
110+
componentDidUpdate(prevProps: Props) {
111+
const { name, annotationError, validationErrors, value } = this.props;
112+
if (
113+
(Object.keys(annotationError).length && !validationErrors.length) ||
114+
(!Object.keys(annotationError).length && validationErrors.length)
115+
) {
116+
if (
117+
annotationError !== prevProps.annotationError ||
118+
validationErrors !== prevProps.validationErrors ||
119+
value !== prevProps.value
120+
) {
121+
this.props.actions.setControlValue(
122+
name,
123+
value,
124+
Object.keys(annotationError),
125+
);
126+
}
121127
}
122128
}
123129

superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterControl/index.jsx

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,48 @@ function isDictionaryForAdhocFilter(value) {
8787
return value && !(value instanceof AdhocFilter) && value.expressionType;
8888
}
8989

90+
function optionsForSelect(props) {
91+
const options = [
92+
...props.columns,
93+
...ensureIsArray(props.selectedMetrics).map(
94+
metric =>
95+
metric &&
96+
(typeof metric === 'string'
97+
? { saved_metric_name: metric }
98+
: new AdhocMetric(metric)),
99+
),
100+
].filter(option => option);
101+
102+
return options
103+
.reduce((results, option) => {
104+
if (option.saved_metric_name) {
105+
results.push({
106+
...option,
107+
filterOptionName: option.saved_metric_name,
108+
});
109+
} else if (option.column_name) {
110+
results.push({
111+
...option,
112+
filterOptionName: `_col_${option.column_name}`,
113+
});
114+
} else if (option instanceof AdhocMetric) {
115+
results.push({
116+
...option,
117+
filterOptionName: `_adhocmetric_${option.label}`,
118+
});
119+
}
120+
return results;
121+
}, [])
122+
.sort((a, b) =>
123+
(a.saved_metric_name || a.column_name || a.label).localeCompare(
124+
b.saved_metric_name || b.column_name || b.label,
125+
),
126+
);
127+
}
128+
90129
class AdhocFilterControl extends Component {
91130
constructor(props) {
92131
super(props);
93-
this.optionsForSelect = this.optionsForSelect.bind(this);
94132
this.onRemoveFilter = this.onRemoveFilter.bind(this);
95133
this.onNewFilter = this.onNewFilter.bind(this);
96134
this.onFilterEdit = this.onFilterEdit.bind(this);
@@ -126,7 +164,7 @@ class AdhocFilterControl extends Component {
126164
);
127165
this.state = {
128166
values: filters,
129-
options: this.optionsForSelect(this.props),
167+
options: optionsForSelect(this.props),
130168
partitionColumn: null,
131169
};
132170
}
@@ -173,13 +211,13 @@ class AdhocFilterControl extends Component {
173211
}
174212
}
175213

176-
UNSAFE_componentWillReceiveProps(nextProps) {
177-
if (this.props.columns !== nextProps.columns) {
178-
this.setState({ options: this.optionsForSelect(nextProps) });
214+
componentDidUpdate(prevProps) {
215+
if (this.props.columns !== prevProps.columns) {
216+
this.setState({ options: optionsForSelect(this.props) });
179217
}
180-
if (this.props.value !== nextProps.value) {
218+
if (this.props.value !== prevProps.value) {
181219
this.setState({
182-
values: (nextProps.value || []).map(filter =>
220+
values: (this.props.value || []).map(filter =>
183221
isDictionaryForAdhocFilter(filter) ? new AdhocFilter(filter) : filter,
184222
),
185223
});
@@ -298,45 +336,6 @@ class AdhocFilterControl extends Component {
298336
return null;
299337
}
300338

301-
optionsForSelect(props) {
302-
const options = [
303-
...props.columns,
304-
...ensureIsArray(props.selectedMetrics).map(
305-
metric =>
306-
metric &&
307-
(typeof metric === 'string'
308-
? { saved_metric_name: metric }
309-
: new AdhocMetric(metric)),
310-
),
311-
].filter(option => option);
312-
313-
return options
314-
.reduce((results, option) => {
315-
if (option.saved_metric_name) {
316-
results.push({
317-
...option,
318-
filterOptionName: option.saved_metric_name,
319-
});
320-
} else if (option.column_name) {
321-
results.push({
322-
...option,
323-
filterOptionName: `_col_${option.column_name}`,
324-
});
325-
} else if (option instanceof AdhocMetric) {
326-
results.push({
327-
...option,
328-
filterOptionName: `_adhocmetric_${option.label}`,
329-
});
330-
}
331-
return results;
332-
}, [])
333-
.sort((a, b) =>
334-
(a.saved_metric_name || a.column_name || a.label).localeCompare(
335-
b.saved_metric_name || b.column_name || b.label,
336-
),
337-
);
338-
}
339-
340339
addNewFilterPopoverTrigger(trigger) {
341340
return (
342341
<AdhocFilterPopoverTrigger

superset-frontend/src/explore/components/controls/SelectControl.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ export default class SelectControl extends PureComponent {
167167
this.handleFilterOptions = this.handleFilterOptions.bind(this);
168168
}
169169

170-
UNSAFE_componentWillReceiveProps(nextProps) {
170+
componentDidUpdate(prevProps) {
171171
if (
172-
!isEqualArray(nextProps.choices, this.props.choices) ||
173-
!isEqualArray(nextProps.options, this.props.options)
172+
!isEqualArray(this.props.choices, prevProps.choices) ||
173+
!isEqualArray(this.props.options, prevProps.options)
174174
) {
175-
const options = this.getOptions(nextProps);
175+
const options = this.getOptions(this.props);
176176
this.setState({ options });
177177
}
178178
}

0 commit comments

Comments
 (0)