16
16
* specific language governing permissions and limitations
17
17
* under the License.
18
18
*/
19
- import { Input , Button , Box , Spacer , HStack , Field , Stack , VStack , Textarea } from "@chakra-ui/react" ;
19
+ import { Input , Button , Box , Spacer , HStack , Field , Stack , VStack , Spinner } from "@chakra-ui/react" ;
20
20
import { Select } from "chakra-react-select" ;
21
21
import { useEffect , useState } from "react" ;
22
22
import { useForm , Controller } from "react-hook-form" ;
23
- import { FiEye , FiEyeOff , FiSave } from "react-icons/fi" ;
23
+ import { FiSave } from "react-icons/fi" ;
24
24
25
25
import { ErrorAlert } from "src/components/ErrorAlert" ;
26
26
import { FlexibleForm , flexibleFormExtraFieldSection } from "src/components/FlexibleForm" ;
27
27
import { JsonEditor } from "src/components/JsonEditor" ;
28
28
import { Accordion } from "src/components/ui" ;
29
- import { useAddConnection } from "src/queries/useAddConnection" ;
30
- import type { ConnectionMetaEntry } from "src/queries/useConnectionTypeMeta" ;
29
+ import { useConnectionTypeMeta } from "src/queries/useConnectionTypeMeta" ;
31
30
import type { ParamsSpec } from "src/queries/useDagParams" ;
32
31
import { useParamStore } from "src/queries/useParamStore" ;
33
32
34
- import type { AddConnectionParams } from "./AddConnectionButton" ;
33
+ import StandardFields from "./ConnectionStandardFields" ;
34
+ import type { ConnectionBody } from "./Connections" ;
35
35
36
36
type AddConnectionFormProps = {
37
- readonly connectionTypeMeta : Record < string , ConnectionMetaEntry > ;
38
- readonly connectionTypes : Array < string > ;
39
- readonly onClose : ( ) => void ;
37
+ readonly error : unknown ;
38
+ readonly initialConnection : ConnectionBody ;
39
+ readonly isPending : boolean ;
40
+ readonly mutateConnection : ( requestBody : ConnectionBody ) => void ;
40
41
} ;
41
42
42
- const ConnectionForm = ( { connectionTypeMeta, connectionTypes, onClose } : AddConnectionFormProps ) => {
43
+ const ConnectionForm = ( {
44
+ error,
45
+ initialConnection,
46
+ isPending,
47
+ mutateConnection,
48
+ } : AddConnectionFormProps ) => {
43
49
const [ errors , setErrors ] = useState < { conf ?: string } > ( { } ) ;
44
- const { addConnection, error, isPending } = useAddConnection ( { onSuccessConfirm : onClose } ) ;
45
- const { conf, setConf } = useParamStore ( ) ;
46
- const [ showPassword , setShowPassword ] = useState ( false ) ;
50
+ const {
51
+ formattedData : connectionTypeMeta ,
52
+ isPending : isMetaPending ,
53
+ keysList : connectionTypes ,
54
+ } = useConnectionTypeMeta ( ) ;
55
+ const { conf : extra , setConf } = useParamStore ( ) ;
47
56
const {
48
57
control,
49
58
formState : { isValid } ,
50
59
handleSubmit,
51
60
reset,
52
61
watch,
53
- } = useForm < AddConnectionParams > ( {
54
- defaultValues : {
55
- conf,
56
- conn_type : "" ,
57
- connection_id : "" ,
58
- description : "" ,
59
- host : "" ,
60
- login : "" ,
61
- password : "" ,
62
- port : "" ,
63
- schema : "" ,
64
- } ,
62
+ } = useForm < ConnectionBody > ( {
63
+ defaultValues : initialConnection ,
65
64
mode : "onBlur" ,
66
65
} ) ;
67
66
@@ -71,27 +70,23 @@ const ConnectionForm = ({ connectionTypeMeta, connectionTypes, onClose }: AddCon
71
70
72
71
useEffect ( ( ) => {
73
72
reset ( ( prevValues ) => ( {
74
- ...prevValues ,
73
+ ...initialConnection ,
75
74
conn_type : selectedConnType ,
76
- description : "" ,
77
- host : "" ,
78
- login : "" ,
79
- password : "" ,
80
- port : "" ,
81
- schema : "" ,
75
+ connection_id : prevValues . connection_id ,
82
76
} ) ) ;
83
- } , [ selectedConnType , reset ] ) ;
77
+ setConf ( JSON . stringify ( JSON . parse ( initialConnection . extra ) , undefined , 2 ) ) ;
78
+ } , [ selectedConnType , reset , initialConnection , setConf ] ) ;
84
79
85
80
// Automatically reset form when conf is fetched
86
81
useEffect ( ( ) => {
87
82
reset ( ( prevValues ) => ( {
88
83
...prevValues , // Retain existing form values
89
- conf ,
84
+ extra ,
90
85
} ) ) ;
91
- } , [ conf , reset , setConf ] ) ;
86
+ } , [ extra , reset , setConf ] ) ;
92
87
93
- const onSubmit = ( data : AddConnectionParams ) => {
94
- addConnection ( data ) ;
88
+ const onSubmit = ( data : ConnectionBody ) => {
89
+ mutateConnection ( data ) ;
95
90
} ;
96
91
97
92
const validateAndPrettifyJson = ( value : string ) => {
@@ -101,7 +96,7 @@ const ConnectionForm = ({ connectionTypeMeta, connectionTypes, onClose }: AddCon
101
96
setErrors ( ( prev ) => ( { ...prev , conf : undefined } ) ) ;
102
97
const formattedJson = JSON . stringify ( parsedJson , undefined , 2 ) ;
103
98
104
- if ( formattedJson !== conf ) {
99
+ if ( formattedJson !== extra ) {
105
100
setConf ( formattedJson ) ; // Update only if the value is different
106
101
}
107
102
@@ -137,7 +132,7 @@ const ConnectionForm = ({ connectionTypeMeta, connectionTypes, onClose }: AddCon
137
132
</ Field . Label >
138
133
</ Stack >
139
134
< Stack css = { { flexBasis : "70%" } } >
140
- < Input { ...field } required size = "sm" />
135
+ < Input { ...field } disabled = { Boolean ( initialConnection . connection_id ) } required size = "sm" />
141
136
{ fieldState . error ? < Field . ErrorText > { fieldState . error . message } </ Field . ErrorText > : undefined }
142
137
</ Stack >
143
138
</ Field . Root >
@@ -159,13 +154,19 @@ const ConnectionForm = ({ connectionTypeMeta, connectionTypes, onClose }: AddCon
159
154
</ Field . Label >
160
155
</ Stack >
161
156
< Stack css = { { flexBasis : "70%" } } >
162
- < Select
163
- { ...Field }
164
- onChange = { ( val ) => onChange ( val ?. value ) }
165
- options = { connTypesOptions }
166
- placeholder = "Select Connection Type"
167
- value = { connTypesOptions . find ( ( type ) => type . value === value ) }
168
- />
157
+ < Stack >
158
+ { isMetaPending ? (
159
+ < Spinner size = "sm" style = { { left : "60%" , position : "absolute" , top : "20%" } } />
160
+ ) : undefined }
161
+ < Select
162
+ { ...Field }
163
+ isDisabled = { isMetaPending }
164
+ onChange = { ( val ) => onChange ( val ?. value ) }
165
+ options = { connTypesOptions }
166
+ placeholder = "Select Connection Type"
167
+ value = { connTypesOptions . find ( ( type ) => type . value === value ) }
168
+ />
169
+ </ Stack >
169
170
< Field . HelperText >
170
171
Connection type missing? Make sure you have installed the corresponding Airflow Providers
171
172
Package.
@@ -188,60 +189,9 @@ const ConnectionForm = ({ connectionTypeMeta, connectionTypes, onClose }: AddCon
188
189
variant = "enclosed"
189
190
>
190
191
< Accordion . Item key = "standardFields" value = "standardFields" >
191
- < Accordion . ItemTrigger cursor = "button" > Standard Fields</ Accordion . ItemTrigger >
192
+ < Accordion . ItemTrigger > Standard Fields</ Accordion . ItemTrigger >
192
193
< Accordion . ItemContent >
193
- < Stack pb = { 3 } pl = { 3 } pr = { 3 } >
194
- { Object . entries ( standardFields ) . map ( ( [ key , fields ] ) => {
195
- if ( Boolean ( fields . hidden ) ) {
196
- return undefined ;
197
- } // Skip hidden fields
198
-
199
- return (
200
- < Controller
201
- control = { control }
202
- key = { key }
203
- name = { key as keyof AddConnectionParams }
204
- render = { ( { field } ) => (
205
- < Field . Root mt = { 3 } orientation = "horizontal" >
206
- < Stack >
207
- < Field . Label fontSize = "md" style = { { flexBasis : "30%" } } >
208
- { fields . title ?? key }
209
- </ Field . Label >
210
- </ Stack >
211
- < Stack css = { { flexBasis : "70%" , position : "relative" } } >
212
- { key === "description" ? (
213
- < Textarea { ...field } placeholder = { fields . placeholder ?? "" } />
214
- ) : (
215
- < div style = { { position : "relative" , width : "100%" } } >
216
- < Input
217
- { ...field }
218
- placeholder = { fields . placeholder ?? "" }
219
- type = { key === "password" && ! showPassword ? "password" : "text" }
220
- />
221
- { key === "password" && (
222
- < button
223
- onClick = { ( ) => setShowPassword ( ! showPassword ) }
224
- style = { {
225
- cursor : "pointer" ,
226
- position : "absolute" ,
227
- right : "10px" ,
228
- top : "50%" ,
229
- transform : "translateY(-50%)" ,
230
- } }
231
- type = "button"
232
- >
233
- { showPassword ? < FiEye size = { 15 } /> : < FiEyeOff size = { 15 } /> }
234
- </ button >
235
- ) }
236
- </ div >
237
- ) }
238
- </ Stack >
239
- </ Field . Root >
240
- ) }
241
- />
242
- ) ;
243
- } ) }
244
- </ Stack >
194
+ < StandardFields control = { control } standardFields = { standardFields } />
245
195
</ Accordion . ItemContent >
246
196
</ Accordion . Item >
247
197
< FlexibleForm
@@ -254,7 +204,7 @@ const ConnectionForm = ({ connectionTypeMeta, connectionTypes, onClose }: AddCon
254
204
< Accordion . ItemContent >
255
205
< Controller
256
206
control = { control }
257
- name = "conf "
207
+ name = "extra "
258
208
render = { ( { field } ) => (
259
209
< Field . Root invalid = { Boolean ( errors . conf ) } >
260
210
< JsonEditor
@@ -286,7 +236,6 @@ const ConnectionForm = ({ connectionTypeMeta, connectionTypes, onClose }: AddCon
286
236
</ HStack >
287
237
</ Box >
288
238
</ >
289
- // eslint-disable-next-line max-lines
290
239
) ;
291
240
} ;
292
241
0 commit comments