Skip to content

Commit 4e8b8de

Browse files
authored
fix(query-bar): listen to query-history events in query-bar and open saved items COMPASS-6680 COMPASS-6681 COMPASS-6685 (#4221)
fix(query-bar): listen to query-history events in query-bar and open saved items
1 parent bae033c commit 4e8b8de

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

packages/compass-query-bar/src/stores/query-bar-reducer.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import thunk from 'redux-thunk';
44
import { DEFAULT_QUERY_VALUES } from '../constants/query-bar-store';
55
import type { QueryProperty } from '../constants/query-properties';
66
import {
7+
applyFromHistory,
78
applyQuery,
89
changeField,
910
changeSchemaFields,
@@ -174,4 +175,32 @@ describe('queryBarReducer', function () {
174175
expect(store.getState()).to.have.property('schemaFields').deep.eq(fields);
175176
});
176177
});
178+
179+
describe('applyFromHistory', function () {
180+
it('should reset query to whatever was passed in the action', function () {
181+
const store = createStore();
182+
const newQuery = {
183+
filter: { _id: 2 },
184+
sort: { _id: -1 },
185+
};
186+
187+
store.dispatch(applyFromHistory(newQuery));
188+
189+
expect(store.getState())
190+
.to.have.nested.property('fields.filter.value')
191+
.deep.eq(newQuery.filter);
192+
expect(store.getState()).to.have.nested.property(
193+
'fields.filter.string',
194+
'{_id: 2}'
195+
);
196+
197+
expect(store.getState())
198+
.to.have.nested.property('fields.sort.value')
199+
.deep.eq(newQuery.sort);
200+
expect(store.getState()).to.have.nested.property(
201+
'fields.sort.string',
202+
'{_id: -1}'
203+
);
204+
});
205+
});
177206
});

packages/compass-query-bar/src/stores/query-bar-reducer.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ enum QueryBarActions {
160160
SetQuery = 'compass-query-bar/SetQuery',
161161
ApplyQuery = 'compass-query-bar/ApplyQuery',
162162
ResetQuery = 'compass-query-bar/ResetQuery',
163+
ApplyFromHistory = 'compass-query-bar/ApplyFromHistory',
163164
}
164165

165166
type ToggleQueryOptionsAction = {
@@ -291,6 +292,15 @@ export const openExportToLanguage = (): QueryBarThunkAction<void> => {
291292
};
292293
};
293294

295+
type ApplyFromHistoryAction = {
296+
type: QueryBarActions.ApplyFromHistory;
297+
query: unknown;
298+
};
299+
300+
export const applyFromHistory = (query: unknown): ApplyFromHistoryAction => {
301+
return { type: QueryBarActions.ApplyFromHistory, query };
302+
};
303+
294304
export const queryBarReducer: Reducer<QueryBarState> = (
295305
state = INITIAL_STATE,
296306
action
@@ -360,6 +370,18 @@ export const queryBarReducer: Reducer<QueryBarState> = (
360370
};
361371
}
362372

373+
if (
374+
isAction<ApplyFromHistoryAction>(action, QueryBarActions.ApplyFromHistory)
375+
) {
376+
return {
377+
...state,
378+
fields: mapQueryToValidQueryFields({
379+
...DEFAULT_FIELD_VALUES,
380+
...(action.query ?? {}),
381+
}),
382+
};
383+
}
384+
363385
return state;
364386
};
365387

packages/compass-query-bar/src/stores/query-bar-store.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { expect } from 'chai';
2+
import { EventEmitter } from 'events';
23
import { DEFAULT_QUERY_VALUES } from '../constants/query-bar-store';
34
import { setQuery } from './query-bar-reducer';
45
import configureStore from './query-bar-store';
6+
import type AppRegistry from 'hadron-app-registry';
57

68
describe('QueryBarStore [Store]', function () {
79
describe('getCurrentQuery', function () {
@@ -20,4 +22,25 @@ describe('QueryBarStore [Store]', function () {
2022
});
2123
});
2224
});
25+
26+
describe('when localAppRegistry emits query-history-run-query', function () {
27+
it('should reset query to whatever was passed in the event', function () {
28+
const localAppRegistry = new EventEmitter() as unknown as AppRegistry;
29+
const initialQuery = { filter: { _id: 1 } };
30+
const store = configureStore({ query: initialQuery, localAppRegistry });
31+
expect(store.getCurrentQuery()).to.deep.eq({
32+
...DEFAULT_QUERY_VALUES,
33+
...initialQuery,
34+
});
35+
const newQuery = {
36+
filter: { _id: 2 },
37+
sort: { _id: -1 },
38+
};
39+
localAppRegistry.emit('query-history-run-query', newQuery);
40+
expect(store.getCurrentQuery()).to.deep.eq({
41+
...DEFAULT_QUERY_VALUES,
42+
...newQuery,
43+
});
44+
});
45+
});
2346
});

packages/compass-query-bar/src/stores/query-bar-store.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import {
1111
changeSchemaFields,
1212
pickValuesFromFields,
1313
applyFilterChange,
14+
applyFromHistory,
1415
} from './query-bar-reducer';
1516

1617
type QueryBarStoreOptions = {
1718
serverVersion: string;
1819
globalAppRegistry: AppRegistry;
1920
localAppRegistry: AppRegistry;
20-
query: Record<QueryProperty, unknown>;
21+
query: Partial<Record<QueryProperty, unknown>>;
2122
};
2223

2324
function createStore(options: Partial<QueryBarStoreOptions> = {}) {
@@ -45,7 +46,11 @@ export function configureStore(options: Partial<QueryBarStoreOptions> = {}) {
4546
});
4647

4748
localAppRegistry?.on('query-bar-change-filter', (evt: ChangeFilterEvent) => {
48-
store.dispatch(applyFilterChange(evt) as any);
49+
store.dispatch(applyFilterChange(evt));
50+
});
51+
52+
localAppRegistry?.on('query-history-run-query', (query) => {
53+
store.dispatch(applyFromHistory(query));
4954
});
5055

5156
(store as any).getCurrentQuery = () => {

packages/compass-query-history/src/stores/favorite-list-store.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const configureStore = (options = {}) => {
5656
});
5757
},
5858

59+
// TODO(COMPASS-6691): This (and probably all the other actions in this
60+
// store) actually executes two times when clicking on an item in the list
5961
runQuery(query) {
6062
const existingQuery = this.state.items.find((item) => {
6163
return _.isEqual(comparableQuery(item), query);
@@ -67,7 +69,7 @@ const configureStore = (options = {}) => {
6769
screen: 'documents',
6870
});
6971
}
70-
this.localAppRegistry.emit('compass:query-history:run-query', query);
72+
this.localAppRegistry.emit('query-history-run-query', query);
7173
},
7274

7375
getInitialState() {

packages/compass-query-history/src/stores/recent-list-store.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ const configureStore = (options = {}) => {
108108
});
109109
},
110110

111+
// TODO(COMPASS-6691): This (and probably all the other actions in this
112+
// store) actually executes two times when clicking on an item in the list
111113
runQuery(query) {
112114
if (
113115
this.state.items
@@ -118,7 +120,7 @@ const configureStore = (options = {}) => {
118120
) {
119121
track('Query History Recent Used');
120122
}
121-
this.localAppRegistry.emit('compass:query-history:run-query', query);
123+
this.localAppRegistry.emit('query-history-run-query', query);
122124
},
123125

124126
copyQuery(query) {

0 commit comments

Comments
 (0)