Skip to content

Commit 42eff87

Browse files
committed
updated the state to hold coveredQueriesArr instead
1 parent 2b197dc commit 42eff87

File tree

2 files changed

+29
-51
lines changed

2 files changed

+29
-51
lines changed

packages/compass-indexes/src/components/create-index-form/index-flow-section.tsx

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,18 @@ export type IndexFlowSectionProps = {
110110
collectionName: string;
111111
onErrorEncountered: (error: string) => void;
112112
onErrorCleared: () => void;
113-
onCoveredQueriesFetched: ({
114-
coveredQueries,
115-
optimalQueries,
116-
showCoveredQueries,
117-
}: CoveredQueriesFetchedProps) => void;
118-
coveredQueriesObj: {
119-
coveredQueries: JSX.Element | null;
120-
optimalQueries: string | JSX.Element | null;
121-
showCoveredQueries: boolean;
122-
};
113+
onCoveredQueriesFetched: ({ fields }: CoveredQueriesFetchedProps) => void;
114+
coveredQueriesArr: Array<Record<string, number>> | null;
123115
hasIndexFieldChanges: boolean;
124116
};
125117

126118
export const generateCoveredQueries = (
127-
coveredQueriesArr: Array<Record<string, number>>,
119+
coveredQueriesArr: Array<Record<string, number>> | null,
128120
track: TrackFunction
129121
) => {
122+
if (!coveredQueriesArr) {
123+
return;
124+
}
130125
const rows = [];
131126
for (let i = 0; i < coveredQueriesArr.length; i++) {
132127
const currentRow = Object.assign({}, ...coveredQueriesArr.slice(0, i + 1));
@@ -150,8 +145,11 @@ export const generateCoveredQueries = (
150145
};
151146

152147
export const generateOptimalQueries = (
153-
coveredQueriesArr: Array<Record<string, number>>
148+
coveredQueriesArr: Array<Record<string, number>> | null
154149
) => {
150+
if (!coveredQueriesArr) {
151+
return;
152+
}
155153
const numOfFields = coveredQueriesArr.length;
156154

157155
// Do not show for 1 field or less
@@ -215,8 +213,7 @@ const IndexFlowSection = ({
215213
onErrorEncountered,
216214
onErrorCleared,
217215
onCoveredQueriesFetched,
218-
coveredQueriesObj,
219-
216+
coveredQueriesArr,
220217
hasIndexFieldChanges,
221218
}: IndexFlowSectionProps) => {
222219
const darkMode = useDarkMode();
@@ -244,17 +241,13 @@ const IndexFlowSection = ({
244241
);
245242

246243
const onCoveredQueriesButtonClick = useCallback(() => {
247-
const coveredQueriesArr = generateCoveredQueriesArr(fields);
248-
249244
track('Covered Queries Button Clicked', {
250245
context: 'Create Index Modal',
251246
});
252247

253248
try {
254249
onCoveredQueriesFetched({
255-
coveredQueries: generateCoveredQueries(coveredQueriesArr, track),
256-
optimalQueries: generateOptimalQueries(coveredQueriesArr),
257-
showCoveredQueries: true,
250+
fields,
258251
});
259252
} catch (e) {
260253
onErrorEncountered(e instanceof Error ? e.message : String(e));
@@ -265,8 +258,9 @@ const IndexFlowSection = ({
265258
onErrorCleared();
266259
}, [fields, onErrorCleared]);
267260

268-
const { coveredQueries, optimalQueries, showCoveredQueries } =
269-
coveredQueriesObj;
261+
const coveredQueries = generateCoveredQueries(coveredQueriesArr, track);
262+
const optimalQueries = generateOptimalQueries(coveredQueriesArr);
263+
const showCoveredQueries = coveredQueriesArr !== null;
270264

271265
return (
272266
<div>
@@ -431,9 +425,9 @@ const IndexFlowSection = ({
431425
};
432426

433427
const mapState = ({ createIndex }: RootState) => {
434-
const { coveredQueriesObj, hasIndexFieldChanges } = createIndex;
428+
const { coveredQueriesArr, hasIndexFieldChanges } = createIndex;
435429
return {
436-
coveredQueriesObj,
430+
coveredQueriesArr,
437431
hasIndexFieldChanges,
438432
};
439433
};

packages/compass-indexes/src/modules/create-index.tsx

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,8 @@ export type State = {
323323
// to determine whether there has been new query changes since user last pressed the button
324324
hasQueryChanges: boolean;
325325

326-
// covered queries obj for the index flow
327-
coveredQueriesObj: {
328-
coveredQueries: JSX.Element | null;
329-
optimalQueries: string | JSX.Element | null;
330-
showCoveredQueries: boolean;
331-
};
326+
// covered queries array for the index flow to keep track of what the user last sees after pressing the covered queries button
327+
coveredQueriesArr: Array<Record<string, number>> | null;
332328

333329
// to determine whether there has been new index field changes since user last pressed the button
334330
hasIndexFieldChanges: boolean;
@@ -350,11 +346,7 @@ export const INITIAL_STATE: State = {
350346
hasQueryChanges: false,
351347

352348
// Index flow
353-
coveredQueriesObj: {
354-
coveredQueries: null,
355-
optimalQueries: null,
356-
showCoveredQueries: false,
357-
},
349+
coveredQueriesArr: null,
358350
hasIndexFieldChanges: false,
359351
};
360352

@@ -408,16 +400,12 @@ export type SuggestedIndexFetchedProps = {
408400
};
409401

410402
export type CoveredQueriesFetchedProps = {
411-
coveredQueries: JSX.Element;
412-
optimalQueries: string | JSX.Element;
413-
showCoveredQueries: boolean;
403+
fields: Field[];
414404
};
415405

416406
export type CoveredQueriesFetchedAction = {
417407
type: ActionTypes.CoveredQueriesFetched;
418-
coveredQueries: JSX.Element;
419-
optimalQueries: string | JSX.Element;
420-
showCoveredQueries: boolean;
408+
coveredQueriesArr: Array<Record<string, number>>;
421409
};
422410

423411
export type QueryUpdatedAction = {
@@ -504,19 +492,19 @@ export const fetchIndexSuggestions = ({
504492
};
505493

506494
export const fetchCoveredQueries = ({
507-
coveredQueries,
508-
optimalQueries,
509-
showCoveredQueries,
495+
fields,
510496
}: CoveredQueriesFetchedProps): IndexesThunkAction<
511497
void,
512498
CoveredQueriesFetchedAction
513499
> => {
514500
return (dispatch) => {
501+
const coveredQueriesArr = fields.map((field, index) => {
502+
return { [field.name]: index + 1 };
503+
});
504+
515505
dispatch({
516506
type: ActionTypes.CoveredQueriesFetched,
517-
coveredQueries,
518-
optimalQueries,
519-
showCoveredQueries,
507+
coveredQueriesArr,
520508
});
521509
};
522510
};
@@ -866,11 +854,7 @@ const reducer: Reducer<State, Action> = (state = INITIAL_STATE, action) => {
866854
) {
867855
return {
868856
...state,
869-
coveredQueriesObj: {
870-
coveredQueries: action.coveredQueries,
871-
optimalQueries: action.optimalQueries,
872-
showCoveredQueries: action.showCoveredQueries,
873-
},
857+
coveredQueriesArr: action.coveredQueriesArr,
874858
hasIndexFieldChanges: false,
875859
};
876860
}

0 commit comments

Comments
 (0)