@@ -12,6 +12,7 @@ import type {
1212 QueryProperty ,
1313 BaseQuery ,
1414 QueryFormFields ,
15+ FormField ,
1516} from '../constants/query-properties' ;
1617import {
1718 mapFormFieldsToQuery ,
@@ -44,7 +45,6 @@ type QueryBarState = {
4445 host ?: string ;
4546 recentQueries : RecentQuery [ ] ;
4647 favoriteQueries : FavoriteQuery [ ] ;
47- preferencesMaxTimeMS : number | null ;
4848} ;
4949
5050export const INITIAL_STATE : QueryBarState = {
@@ -57,7 +57,6 @@ export const INITIAL_STATE: QueryBarState = {
5757 namespace : '' ,
5858 recentQueries : [ ] ,
5959 favoriteQueries : [ ] ,
60- preferencesMaxTimeMS : null ,
6160} ;
6261
6362export enum QueryBarActions {
@@ -70,14 +69,8 @@ export enum QueryBarActions {
7069 ApplyFromHistory = 'compass-query-bar/ApplyFromHistory' ,
7170 RecentQueriesFetched = 'compass-query-bar/RecentQueriesFetched' ,
7271 FavoriteQueriesFetched = 'compass-query-bar/FavoriteQueriesFetched' ,
73- UpdatePreferencesMaxTimeMS = 'compass-query-bar/UpdatePreferencesMaxTimeMS' ,
7472}
7573
76- type UpdatePreferencesMaxTimeMSAction = {
77- type : QueryBarActions . UpdatePreferencesMaxTimeMS ;
78- maxTimeMS : number | null ;
79- } ;
80-
8174type ChangeReadonlyConnectionStatusAction = {
8275 type : QueryBarActions . ChangeReadonlyConnectionStatus ;
8376 readonly : boolean ;
@@ -94,10 +87,10 @@ export const toggleQueryOptions = (
9487 return { type : QueryBarActions . ToggleQueryOptions , force } ;
9588} ;
9689
97- type ChangeFieldAction = {
90+ type ChangeFieldAction < T = QueryProperty > = {
9891 type : QueryBarActions . ChangeField ;
99- name : QueryProperty ;
100- value : string ;
92+ name : T ;
93+ value : FormField < T > ;
10194} ;
10295
10396/**
@@ -117,20 +110,25 @@ const emitOnQueryChange = (): QueryBarThunkAction<void> => {
117110 } ;
118111} ;
119112
120- export const updatePreferencesMaxTimeMS = (
121- maxTimeMS : number | undefined
122- ) : UpdatePreferencesMaxTimeMSAction => {
123- return {
124- type : QueryBarActions . UpdatePreferencesMaxTimeMS ,
125- maxTimeMS : maxTimeMS ?? null ,
126- } ;
127- } ;
128-
129113export const changeField = (
130114 name : QueryProperty ,
131- value : string
132- ) : ChangeFieldAction => {
133- return { type : QueryBarActions . ChangeField , name, value } ;
115+ stringValue : string
116+ ) : QueryBarThunkAction < void , ChangeFieldAction > => {
117+ return ( dispatch , getState , { preferences } ) => {
118+ const parsedValue = validateField ( name , stringValue , {
119+ maxTimeMS : preferences . getPreferences ( ) . maxTimeMS ?? undefined ,
120+ } ) ;
121+ const isValid = parsedValue !== false ;
122+ dispatch ( {
123+ type : QueryBarActions . ChangeField ,
124+ name,
125+ value : {
126+ string : stringValue ,
127+ valid : isValid ,
128+ value : isValid ? parsedValue : getState ( ) . queryBar . fields [ name ] . value ,
129+ } ,
130+ } ) ;
131+ } ;
134132} ;
135133
136134type ApplyQueryAction = {
@@ -160,16 +158,21 @@ export const applyQuery = (): QueryBarThunkAction<
160158
161159type ResetQueryAction = {
162160 type : QueryBarActions . ResetQuery ;
161+ fields : QueryFormFields ;
163162} ;
164163
165164export const resetQuery = ( ) : QueryBarThunkAction <
166165 false | Record < string , unknown >
167166> => {
168- return ( dispatch , getState , { localAppRegistry } ) => {
167+ return ( dispatch , getState , { localAppRegistry, preferences } ) => {
169168 if ( isEqualDefaultQuery ( getState ( ) . queryBar . fields ) ) {
170169 return false ;
171170 }
172- dispatch ( { type : QueryBarActions . ResetQuery } ) ;
171+ const fields = mapQueryToFormFields (
172+ { maxTimeMS : preferences . getPreferences ( ) . maxTimeMS } ,
173+ DEFAULT_FIELD_VALUES
174+ ) ;
175+ dispatch ( { type : QueryBarActions . ResetQuery , fields } ) ;
173176 dispatch ( emitOnQueryChange ( ) ) ;
174177 const defaultQuery = cloneDeep ( DEFAULT_QUERY_VALUES ) ;
175178 localAppRegistry ?. emit ( 'query-reset' , defaultQuery ) ;
@@ -179,11 +182,19 @@ export const resetQuery = (): QueryBarThunkAction<
179182
180183type SetQueryAction = {
181184 type : QueryBarActions . SetQuery ;
182- query : BaseQuery ;
185+ fields : QueryFormFields ;
183186} ;
184187
185- export const setQuery = ( query : BaseQuery ) : SetQueryAction => {
186- return { type : QueryBarActions . SetQuery , query } ;
188+ export const setQuery = (
189+ query : BaseQuery
190+ ) : QueryBarThunkAction < void , SetQueryAction > => {
191+ return ( dispatch , getState , { preferences } ) => {
192+ const fields = mapQueryToFormFields (
193+ { maxTimeMS : preferences . getPreferences ( ) . maxTimeMS } ,
194+ query
195+ ) ;
196+ dispatch ( { type : QueryBarActions . SetQuery , fields } ) ;
197+ } ;
187198} ;
188199
189200export const applyFilterChange = (
@@ -219,16 +230,23 @@ export const openExportToLanguage = (): QueryBarThunkAction<void> => {
219230
220231type ApplyFromHistoryAction = {
221232 type : QueryBarActions . ApplyFromHistory ;
222- query : BaseQuery ;
233+ fields : QueryFormFields ;
223234} ;
224235
225236export const applyFromHistory = (
226237 query : BaseQuery & { update ?: Document }
227238) : QueryBarThunkAction < void , ApplyFromHistoryAction > => {
228- return ( dispatch , getState , { localAppRegistry } ) => {
239+ return ( dispatch , getState , { localAppRegistry, preferences } ) => {
240+ const fields = mapQueryToFormFields (
241+ { maxTimeMS : preferences . getPreferences ( ) . maxTimeMS } ,
242+ {
243+ ...DEFAULT_FIELD_VALUES ,
244+ ...query ,
245+ }
246+ ) ;
229247 dispatch ( {
230248 type : QueryBarActions . ApplyFromHistory ,
231- query ,
249+ fields ,
232250 } ) ;
233251
234252 if ( query . update ) {
@@ -466,44 +484,21 @@ export const queryBarReducer: Reducer<QueryBarState> = (
466484 }
467485
468486 if ( isAction < ChangeFieldAction > ( action , QueryBarActions . ChangeField ) ) {
469- const newValue = validateField ( action . name , action . value , {
470- maxTimeMS : state . preferencesMaxTimeMS ?? undefined ,
471- } ) ;
472- const valid = newValue !== false ;
473487 return {
474488 ...state ,
475489 fields : {
476490 ...state . fields ,
477- [ action . name ] : {
478- string : action . value ,
479- valid : valid ,
480- value : valid ? newValue : state . fields [ action . name ] . value ,
481- } ,
491+ [ action . name ] : action . value ,
482492 } ,
483493 } ;
484494 }
485495
486- if (
487- isAction < UpdatePreferencesMaxTimeMSAction > (
488- action ,
489- QueryBarActions . UpdatePreferencesMaxTimeMS
490- )
491- ) {
492- return {
493- ...state ,
494- preferencesMaxTimeMS : action . maxTimeMS ,
495- } ;
496- }
497-
498496 if ( isAction < SetQueryAction > ( action , QueryBarActions . SetQuery ) ) {
499497 return {
500498 ...state ,
501499 fields : {
502500 ...state . fields ,
503- ...mapQueryToFormFields (
504- { maxTimeMS : state . preferencesMaxTimeMS ?? undefined } ,
505- action . query
506- ) ,
501+ ...action . fields ,
507502 } ,
508503 } ;
509504 }
@@ -520,10 +515,7 @@ export const queryBarReducer: Reducer<QueryBarState> = (
520515 return {
521516 ...state ,
522517 lastAppliedQuery : null ,
523- fields : mapQueryToFormFields (
524- { maxTimeMS : state . preferencesMaxTimeMS ?? undefined } ,
525- DEFAULT_FIELD_VALUES
526- ) ,
518+ fields : action . fields ,
527519 } ;
528520 }
529521
@@ -532,13 +524,7 @@ export const queryBarReducer: Reducer<QueryBarState> = (
532524 ) {
533525 return {
534526 ...state ,
535- fields : mapQueryToFormFields (
536- { maxTimeMS : state . preferencesMaxTimeMS ?? undefined } ,
537- {
538- ...DEFAULT_FIELD_VALUES ,
539- ...( action . query ?? { } ) ,
540- }
541- ) ,
527+ fields : action . fields ,
542528 } ;
543529 }
544530
0 commit comments