Skip to content

Commit e41df6b

Browse files
authored
refactor(compass-schema): replace reflux with redux COMPASS-8797 (#6656)
1 parent d3e1965 commit e41df6b

File tree

12 files changed

+467
-453
lines changed

12 files changed

+467
-453
lines changed

package-lock.json

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-schema/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@
9797
"react": "^17.0.2",
9898
"react-leaflet": "^2.4.0",
9999
"react-leaflet-draw": "^0.19.0",
100-
"reflux": "^0.4.1",
101-
"@mongodb-js/reflux-state-mixin": "^1.1.5"
100+
"react-redux": "^8.1.3",
101+
"redux": "^4.2.1",
102+
"redux-thunk": "^2.4.2"
102103
},
103104
"is_compass_plugin": true
104105
}

packages/compass-schema/src/actions/index.spec.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/compass-schema/src/actions/index.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/compass-schema/src/components/compass-schema.tsx

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useCallback } from 'react';
2-
2+
import type { Schema as MongodbSchema } from 'mongodb-schema';
3+
import { connect } from 'react-redux';
34
import type { AnalysisState } from '../constants/analysis-states';
45
import {
56
ANALYSIS_STATE_INITIAL,
@@ -29,12 +30,13 @@ import {
2930
Badge,
3031
Icon,
3132
} from '@mongodb-js/compass-components';
32-
import type { configureActions } from '../actions';
3333
import { usePreference } from 'compass-preferences-model/provider';
3434
import { useConnectionInfo } from '@mongodb-js/compass-connections/provider';
3535
import { getAtlasPerformanceAdvisorLink } from '../utils';
3636
import { useIsLastAppliedQueryOutdated } from '@mongodb-js/compass-query-bar';
3737
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
38+
import type { RootState } from '../stores/store';
39+
import { startAnalysis, stopAnalysis } from '../stores/reducer';
3840

3941
const rootStyles = css({
4042
width: '100%',
@@ -296,10 +298,9 @@ const AnalyzingScreen: React.FunctionComponent<{
296298
};
297299

298300
const FieldList: React.FunctionComponent<{
299-
schema: any;
301+
schema: MongodbSchema | null;
300302
analysisState: AnalysisState;
301-
actions: Record<string, any>;
302-
}> = ({ schema, analysisState, actions }) => {
303+
}> = ({ schema, analysisState }) => {
303304
const darkMode = useDarkMode();
304305

305306
if (analysisState !== ANALYSIS_STATE_COMPLETE) {
@@ -327,7 +328,7 @@ const FieldList: React.FunctionComponent<{
327328
>
328329
<div data-testid="schema-field-list">
329330
{fields.map((field: any) => (
330-
<Field key={field.name} actions={actions} {...field} />
331+
<Field key={field.name} {...field} />
331332
))}
332333
</div>
333334
</div>
@@ -366,25 +367,25 @@ const PerformanceAdvisorBanner = () => {
366367
};
367368

368369
const Schema: React.FunctionComponent<{
369-
actions: ReturnType<typeof configureActions>;
370370
analysisState: AnalysisState;
371371
errorMessage?: string;
372372
maxTimeMS?: number;
373-
schema?: any;
373+
schema: MongodbSchema | null;
374374
count?: number;
375375
resultId?: string;
376-
}> = ({ actions, analysisState, errorMessage, schema, resultId }) => {
376+
onStartAnalysis: () => Promise<void>;
377+
onStopAnalysis: () => void;
378+
}> = ({
379+
analysisState,
380+
errorMessage,
381+
schema,
382+
resultId,
383+
onStartAnalysis,
384+
onStopAnalysis,
385+
}) => {
377386
const onApplyClicked = useCallback(() => {
378-
actions.startAnalysis();
379-
}, [actions]);
380-
381-
const onCancelClicked = useCallback(() => {
382-
actions.stopAnalysis();
383-
}, [actions]);
384-
385-
const onResetClicked = useCallback(() => {
386-
actions.startAnalysis();
387-
}, [actions]);
387+
void onStartAnalysis();
388+
}, [onStartAnalysis]);
388389

389390
const outdated = useIsLastAppliedQueryOutdated('schema');
390391

@@ -398,7 +399,7 @@ const Schema: React.FunctionComponent<{
398399
toolbar={
399400
<SchemaToolbar
400401
onAnalyzeSchemaClicked={onApplyClicked}
401-
onResetClicked={onResetClicked}
402+
onResetClicked={onApplyClicked}
402403
analysisState={analysisState}
403404
errorMessage={errorMessage || ''}
404405
isOutdated={!!outdated}
@@ -413,19 +414,26 @@ const Schema: React.FunctionComponent<{
413414
<InitialScreen onApplyClicked={onApplyClicked} />
414415
)}
415416
{analysisState === ANALYSIS_STATE_ANALYZING && (
416-
<AnalyzingScreen onCancelClicked={onCancelClicked} />
417+
<AnalyzingScreen onCancelClicked={onStopAnalysis} />
417418
)}
418419
{analysisState === ANALYSIS_STATE_COMPLETE && (
419-
<FieldList
420-
schema={schema}
421-
analysisState={analysisState}
422-
actions={actions}
423-
/>
420+
<FieldList schema={schema} analysisState={analysisState} />
424421
)}
425422
</div>
426423
</WorkspaceContainer>
427424
</div>
428425
);
429426
};
430427

431-
export default Schema;
428+
export default connect(
429+
(state: RootState) => ({
430+
analysisState: state.analysisState,
431+
errorMessage: state.errorMessage,
432+
schema: state.schema,
433+
resultId: state.resultId,
434+
}),
435+
{
436+
onStartAnalysis: startAnalysis,
437+
onStopAnalysis: stopAnalysis,
438+
}
439+
)(Schema);

packages/compass-schema/src/components/coordinates-minichart/coordinates-minichart.jsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { PureComponent, useCallback } from 'react';
22
import PropTypes from 'prop-types';
3+
import { connect } from 'react-redux';
34

45
import L from 'leaflet';
56

@@ -17,6 +18,11 @@ import GeoscatterMapItem from './marker';
1718
import { LIGHTMODE_TILE_URL, DARKMODE_TILE_URL } from './constants';
1819
import { getHereAttributionMessage } from './utils';
1920
import { debounce } from 'lodash';
21+
import {
22+
geoLayerAdded,
23+
geoLayersDeleted,
24+
geoLayersEdited,
25+
} from '../../stores/reducer';
2026

2127
// TODO: Disable boxZoom handler for circle lasso.
2228
//
@@ -124,10 +130,12 @@ class UnthemedCoordinatesMinichart extends PureComponent {
124130
unique: PropTypes.number,
125131
values: PropTypes.array,
126132
}),
127-
actions: PropTypes.object.isRequired,
128133
fieldName: PropTypes.string.isRequired,
129134
darkMode: PropTypes.bool,
130135
onGeoQueryChanged: PropTypes.func.isRequired,
136+
geoLayerAdded: PropTypes.func.isRequired,
137+
geoLayersEdited: PropTypes.func.isRequired,
138+
geoLayersDeleted: PropTypes.func.isRequired,
131139
};
132140

133141
state = {
@@ -239,26 +247,23 @@ class UnthemedCoordinatesMinichart extends PureComponent {
239247
}
240248

241249
onCreated = (evt) => {
242-
this.props.actions.geoLayerAdded(
250+
this.props.geoLayerAdded(
243251
this.props.fieldName,
244252
evt.layer,
245253
this.props.onGeoQueryChanged
246254
);
247255
};
248256

249257
onEdited = (evt) => {
250-
this.props.actions.geoLayersEdited(
258+
this.props.geoLayersEdited(
251259
this.props.fieldName,
252260
evt.layers,
253261
this.props.onGeoQueryChanged
254262
);
255263
};
256264

257265
onDeleted = (evt) => {
258-
this.props.actions.geoLayersDeleted(
259-
evt.layers,
260-
this.props.onGeoQueryChanged
261-
);
266+
this.props.geoLayersDeleted(evt.layers, this.props.onGeoQueryChanged);
262267
};
263268

264269
/**
@@ -328,5 +333,10 @@ CoordinatesMinichart.propTypes = {
328333
onQueryChanged: PropTypes.func,
329334
};
330335

331-
export default CoordinatesMinichart;
332-
export { CoordinatesMinichart };
336+
const ConnectedCoordinatesMinichart = connect(undefined, {
337+
geoLayerAdded,
338+
geoLayersEdited,
339+
geoLayersDeleted,
340+
})(CoordinatesMinichart);
341+
342+
export default ConnectedCoordinatesMinichart;

packages/compass-schema/src/components/field.spec.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
type SchemaType,
1414
} from 'mongodb-schema';
1515
import { BSON, Decimal128 } from 'bson';
16-
import { configureActions } from '../actions';
1716
import Field, { shouldShowUnboundArrayInsight } from './field';
1817
import QueryBarPlugin from '@mongodb-js/compass-query-bar';
1918
import {
@@ -44,7 +43,6 @@ function renderField(
4443
<MockQueryBarPlugin {...(queryBarProps as any)}>
4544
<Field
4645
enableMaps={false}
47-
actions={configureActions()}
4846
name="testFieldName"
4947
path={['test']}
5048
{...props}

packages/compass-schema/src/components/field.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import type {
2121
import { FieldType, sortTypes } from './type';
2222
import Minichart from './minichart';
2323
import detectCoordinates from '../modules/detect-coordinates';
24-
import type { configureActions } from '../actions';
2524
import { useQueryBarQuery } from '@mongodb-js/compass-query-bar';
2625
import { useChangeQueryBarQuery } from '@mongodb-js/compass-query-bar';
2726

@@ -110,7 +109,6 @@ const nestedFieldStyles = css({
110109
});
111110

112111
type FieldProps = {
113-
actions: ReturnType<typeof configureActions>;
114112
name: string;
115113
path: string[];
116114
types: SchemaType[];
@@ -196,7 +194,7 @@ export function shouldShowUnboundArrayInsight(
196194
);
197195
}
198196

199-
function Field({ actions, name, path, types, enableMaps }: FieldProps) {
197+
function Field({ name, path, types, enableMaps }: FieldProps) {
200198
const query = useQueryBarQuery();
201199
const changeQuery = useChangeQueryBarQuery();
202200
const [isExpanded, setIsExpanded] = useState(false);
@@ -308,7 +306,6 @@ function Field({ actions, name, path, types, enableMaps }: FieldProps) {
308306
fieldValue={query.filter?.[fieldName]}
309307
type={activeType}
310308
nestedDocType={nestedDocType}
311-
actions={actions}
312309
onQueryChanged={debouncedChangeQuery}
313310
/>
314311
</div>
@@ -326,7 +323,7 @@ function Field({ actions, name, path, types, enableMaps }: FieldProps) {
326323
{(getNestedDocType(types)?.fields || []).map(
327324
(field: SchemaField) => (
328325
<div className={nestedFieldStyles} key={field.name}>
329-
<Field actions={actions} enableMaps={enableMaps} {...field} />
326+
<Field enableMaps={enableMaps} {...field} />
330327
</div>
331328
)
332329
)}

packages/compass-schema/src/components/minichart/minichart.jsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@ class MiniChart extends PureComponent {
3434
// but it is not noticable to the user.
3535
this.resizeListener();
3636
window.addEventListener('resize', this.resizeListener);
37-
this.unsubscribeMiniChartResize =
38-
this.props.actions.resizeMiniCharts.listen(this.resizeListener);
3937
}
4038

4139
componentWillUnmount() {
4240
window.removeEventListener('resize', this.resizeListener);
43-
this.unsubscribeMiniChartResize();
4441
}
4542

4643
/**

0 commit comments

Comments
 (0)