1- import type { Reducer } from 'redux' ;
1+ import type { AnyAction , Reducer } from 'redux' ;
22import { parseFilter } from 'mongodb-query-parser' ;
33import type { DataService } from '@mongodb-js/compass-connections/provider' ;
44import type { CreateNamespaceThunkAction } from '../stores/create-namespace' ;
@@ -28,72 +28,138 @@ export const INITIAL_STATE = {
2828 currentTopologyType : 'Unknown' as const ,
2929} ;
3030
31- export const RESET = 'databases-collections/reset' ;
31+ export const enum CreateNamespaceActionTypes {
32+ Reset = 'databases-collections/Reset' ,
33+ Open = 'databases-collections/create-collection/Open' ,
34+ HandleError = 'databases-collections/error/HandleError' ,
35+ ClearError = 'databases-collections/error/ClearError' ,
36+ ToggleIsRunning = 'databases-collections/is-running/ToggleIsRunning' ,
37+ ToggleIsVisible = 'databases-collections/is-visible/ToggleIsVisible' ,
38+ TopologyChanged = 'databases-collections/TopologyChanged' ,
39+ InstanceProvided = 'databases-collections/InstanceProvided' ,
40+ DataServiceProvided = 'databases-collections/DataServiceProvided' ,
41+ }
3242
33- export const reset = ( ) => ( {
34- type : RESET ,
35- } ) ;
43+ export type ResetAction = {
44+ type : CreateNamespaceActionTypes . Reset ;
45+ } ;
46+
47+ export type OpenAction = {
48+ type : CreateNamespaceActionTypes . Open ;
49+ databaseName : string | null ;
50+ } ;
51+
52+ export type HandleErrorAction = {
53+ type : CreateNamespaceActionTypes . HandleError ;
54+ error : Error ;
55+ } ;
3656
37- const OPEN = 'databases-collections/create-collection/OPEN' ;
57+ export type ClearErrorAction = {
58+ type : CreateNamespaceActionTypes . ClearError ;
59+ } ;
60+
61+ export type ToggleIsRunningAction = {
62+ type : CreateNamespaceActionTypes . ToggleIsRunning ;
63+ isRunning : boolean ;
64+ } ;
65+
66+ export type ToggleIsVisibleAction = {
67+ type : CreateNamespaceActionTypes . ToggleIsVisible ;
68+ isVisible : boolean ;
69+ } ;
70+
71+ export type TopologyChangedAction = {
72+ type : CreateNamespaceActionTypes . TopologyChanged ;
73+ newTopology : string ;
74+ } ;
75+
76+ export type InstanceProvidedAction = {
77+ type : CreateNamespaceActionTypes . InstanceProvided ;
78+ topology : string ;
79+ serverVersion : string ;
80+ } ;
81+
82+ export type DataServiceProvidedAction = {
83+ type : CreateNamespaceActionTypes . DataServiceProvided ;
84+ configuredKMSProviders : ReturnType < DataService [ 'configuredKMSProviders' ] > ;
85+ } ;
86+
87+ export const reset = ( ) : ResetAction => ( {
88+ type : CreateNamespaceActionTypes . Reset ,
89+ } ) ;
3890
3991export const open = (
4092 dbName : string | null = null
41- ) : CreateNamespaceThunkAction < void > => {
93+ ) : CreateNamespaceThunkAction < void , OpenAction > => {
4294 return ( dispatch , _getState , { logger : { track } } ) => {
4395 track ( 'Screen' , {
4496 name : dbName ? 'create_collection_modal' : 'create_database_modal' ,
4597 } ) ;
4698
4799 dispatch ( {
48- type : OPEN ,
100+ type : CreateNamespaceActionTypes . Open ,
49101 databaseName : dbName ,
50102 } ) ;
51103 } ;
52104} ;
53105
54- export const HANDLE_ERROR = `databases-collections/error/HANDLE_ERROR` ;
55-
56- export const handleError = ( error : Error ) => ( {
57- type : HANDLE_ERROR ,
106+ export const handleError = ( error : Error ) : HandleErrorAction => ( {
107+ type : CreateNamespaceActionTypes . HandleError ,
58108 error : error ,
59109} ) ;
60110
61- export const CLEAR_ERROR = `databases-collections/error/CLEAR_ERROR` ;
62-
63- export const clearError = ( ) => ( {
64- type : CLEAR_ERROR ,
111+ export const clearError = ( ) : ClearErrorAction => ( {
112+ type : CreateNamespaceActionTypes . ClearError ,
65113} ) ;
66114
67- export const TOGGLE_IS_RUNNING =
68- 'databases-collections/is-running/TOGGLE_IS_RUNNING' ;
69-
70- export const toggleIsRunning = ( isRunning : boolean ) => ( {
71- type : TOGGLE_IS_RUNNING ,
115+ export const toggleIsRunning = ( isRunning : boolean ) : ToggleIsRunningAction => ( {
116+ type : CreateNamespaceActionTypes . ToggleIsRunning ,
72117 isRunning : isRunning ,
73118} ) ;
74119
75- export const TOGGLE_IS_VISIBLE = `databases-collections/is-visible/TOGGLE_IS_VISIBLE` ;
76-
77- export const toggleIsVisible = ( isVisible : boolean ) => ( {
78- type : TOGGLE_IS_VISIBLE ,
120+ export const toggleIsVisible = ( isVisible : boolean ) : ToggleIsVisibleAction => ( {
121+ type : CreateNamespaceActionTypes . ToggleIsVisible ,
79122 isVisible : isVisible ,
80123} ) ;
81124
82- export const TOPOLOGY_CHANGED = `databases-collections/TOPOLOGY_CHANGED` ;
83-
84- export const topologyChanged = ( newTopology : string ) => ( {
85- type : TOPOLOGY_CHANGED ,
125+ export const topologyChanged = (
126+ newTopology : string
127+ ) : TopologyChangedAction => ( {
128+ type : CreateNamespaceActionTypes . TopologyChanged ,
86129 newTopology : newTopology ,
87130} ) ;
88131
132+ export const instanceProvided = ( params : {
133+ serverVersion : string ;
134+ topology : string ;
135+ } ) : InstanceProvidedAction => ( {
136+ type : CreateNamespaceActionTypes . InstanceProvided ,
137+ ...params ,
138+ } ) ;
139+
140+ export const dataServiceProvided = ( params : {
141+ configuredKMSProviders : ReturnType < DataService [ 'configuredKMSProviders' ] > ;
142+ } ) : DataServiceProvidedAction => ( {
143+ type : CreateNamespaceActionTypes . DataServiceProvided ,
144+ ...params ,
145+ } ) ;
146+
147+ function isAction < A extends AnyAction > (
148+ action : AnyAction ,
149+ type : A [ 'type' ]
150+ ) : action is A {
151+ return action . type === type ;
152+ }
153+
89154const reducer : Reducer < CreateNamespaceState > = (
90155 state = INITIAL_STATE ,
91156 action
92157) => {
93- if ( action . type === RESET ) {
158+ if ( isAction < ResetAction > ( action , CreateNamespaceActionTypes . Reset ) ) {
94159 return { ...INITIAL_STATE , serverVersion : state . serverVersion } ;
95160 }
96- if ( action . type === OPEN ) {
161+
162+ if ( isAction < OpenAction > ( action , CreateNamespaceActionTypes . Open ) ) {
97163 return {
98164 ...state ,
99165 databaseName : action . databaseName ,
@@ -103,30 +169,73 @@ const reducer: Reducer<CreateNamespaceState> = (
103169 error : null ,
104170 } ;
105171 }
106- if ( action . type === HANDLE_ERROR ) {
172+
173+ if (
174+ isAction < HandleErrorAction > ( action , CreateNamespaceActionTypes . HandleError )
175+ ) {
107176 return {
108177 ...state ,
109178 error : action . error ,
110179 } ;
111180 }
112- if ( action . type === CLEAR_ERROR ) {
181+
182+ if (
183+ isAction < ClearErrorAction > ( action , CreateNamespaceActionTypes . ClearError )
184+ ) {
113185 return {
114186 ...state ,
115187 error : null ,
116188 } ;
117189 }
118- if ( action . type === TOGGLE_IS_VISIBLE ) {
190+
191+ if (
192+ isAction < ToggleIsVisibleAction > (
193+ action ,
194+ CreateNamespaceActionTypes . ToggleIsVisible
195+ )
196+ ) {
119197 return {
120198 ...state ,
121199 isVisible : action . isVisible ,
122200 } ;
123201 }
124- if ( action . type === TOPOLOGY_CHANGED ) {
202+
203+ if (
204+ isAction < TopologyChangedAction > (
205+ action ,
206+ CreateNamespaceActionTypes . TopologyChanged
207+ )
208+ ) {
125209 return {
126210 ...state ,
127211 currentTopologyType : action . newTopology ,
128212 } ;
129213 }
214+
215+ if (
216+ isAction < InstanceProvidedAction > (
217+ action ,
218+ CreateNamespaceActionTypes . InstanceProvided
219+ )
220+ ) {
221+ return {
222+ ...state ,
223+ serverVersion : action . serverVersion ,
224+ currentTopologyType : action . topology ,
225+ } ;
226+ }
227+
228+ if (
229+ isAction < DataServiceProvidedAction > (
230+ action ,
231+ CreateNamespaceActionTypes . DataServiceProvided
232+ )
233+ ) {
234+ return {
235+ ...state ,
236+ configuredKMSProviders : action . configuredKMSProviders ,
237+ } ;
238+ }
130239 return state ;
131240} ;
132241
0 commit comments