Skip to content

Commit ee4ee62

Browse files
committed
Add query field to create-index redux store/actions
1 parent 2e115df commit ee4ee62

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

packages/compass-indexes/src/modules/create-index.spec.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import { expect } from 'chai';
33
import { setupStore } from '../../test/setup-store';
44

55
import {
6+
createIndexClosed,
67
createIndexFormSubmitted,
7-
updateFieldName,
8+
createIndexOpened,
9+
errorCleared,
810
fieldAdded,
911
fieldRemoved,
1012
fieldTypeUpdated,
1113
optionChanged,
1214
optionToggled,
13-
createIndexOpened,
14-
createIndexClosed,
15-
errorCleared,
15+
updateFieldName,
1616
} from './create-index';
1717
import type { IndexesStore } from '../stores/store';
18+
import { EJSON } from 'bson';
1819

1920
describe('create-index module', function () {
2021
let store: IndexesStore;
@@ -225,11 +226,30 @@ describe('create-index module', function () {
225226
});
226227

227228
describe('createIndexOpened', function () {
229+
const query = EJSON.serialize({});
228230
it('sets isVisible=true', function () {
229231
store.dispatch(createIndexOpened());
230232

231233
expect(store.getState().createIndex.isVisible).to.equal(true);
232234
});
235+
236+
it('sets isVisible=true with a query', function () {
237+
store.dispatch(createIndexOpened({ query }));
238+
239+
expect(store.getState().createIndex.isVisible).to.equal(true);
240+
});
241+
242+
it('sets currentTab=IndexFlow if no query is provided', function () {
243+
store.dispatch(createIndexOpened());
244+
245+
expect(store.getState().createIndex.currentTab).to.equal('IndexFlow');
246+
});
247+
248+
it('sets currentTab=QueryFlow if a query is provided', function () {
249+
store.dispatch(createIndexOpened({ query }));
250+
251+
expect(store.getState().createIndex.currentTab).to.equal('QueryFlow');
252+
});
233253
});
234254

235255
describe('createIndexClosed', function () {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { Document } from 'mongodb';
2+
import type { Document as BsonDocument } from 'bson';
23
import { EJSON, ObjectId } from 'bson';
34
import type { CreateIndexesOptions, IndexDirection } from 'mongodb';
45
import { isCollationValid } from 'mongodb-query-parser';
56
import React from 'react';
6-
import type { Action, Reducer, Dispatch } from 'redux';
7+
import type { Action, Dispatch, Reducer } from 'redux';
78
import { Badge, Link } from '@mongodb-js/compass-components';
89
import { isAction } from '../utils/is-action';
9-
import type { IndexesThunkAction } from '.';
10-
import type { RootState } from '.';
10+
import type { IndexesThunkAction, RootState } from '.';
1111
import { createRegularIndex } from './regular-indexes';
1212
import * as mql from 'mongodb-mql-engines';
1313
import _parseShellBSON, { ParseMode } from '@mongodb-js/shell-bson-parser';
@@ -77,6 +77,7 @@ type ErrorClearedAction = {
7777

7878
export type CreateIndexOpenedAction = {
7979
type: ActionTypes.CreateIndexOpened;
80+
query?: BsonDocument;
8081
};
8182

8283
type CreateIndexClosedAction = {
@@ -314,6 +315,9 @@ export type State = {
314315

315316
// sample documents used for getting index suggestions
316317
sampleDocs: Array<Document> | null;
318+
319+
// base query to be used for query flow index creation
320+
query: BsonDocument | null;
317321
};
318322

319323
export const INITIAL_STATE: State = {
@@ -327,6 +331,7 @@ export const INITIAL_STATE: State = {
327331
fetchingSuggestionsError: null,
328332
indexSuggestions: null,
329333
sampleDocs: null,
334+
query: null,
330335
};
331336

332337
function getInitialState(): State {
@@ -338,8 +343,9 @@ function getInitialState(): State {
338343

339344
//-------
340345

341-
export const createIndexOpened = () => ({
346+
export const createIndexOpened = (query?: BsonDocument) => ({
342347
type: ActionTypes.CreateIndexOpened,
348+
query,
343349
});
344350

345351
export const createIndexClosed = () => ({
@@ -706,6 +712,8 @@ const reducer: Reducer<State, Action> = (state = INITIAL_STATE, action) => {
706712
return {
707713
...getInitialState(),
708714
isVisible: true,
715+
query: action.query ?? null,
716+
currentTab: action.query ? 'QueryFlow' : 'IndexFlow',
709717
};
710718
}
711719

0 commit comments

Comments
 (0)