1
1
import { Formik , getIn , setIn , useFormikContext } from "formik" ;
2
2
import { JSONSchema7 } from "json-schema" ;
3
- import React , { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
4
- import { useToggle } from "react-use" ;
3
+ import React , { useCallback , useEffect , useMemo , useState } from "react" ;
4
+ import { useDeepCompareEffect , useToggle } from "react-use" ;
5
5
6
6
import { FormChangeTracker } from "components/FormChangeTracker" ;
7
7
@@ -13,7 +13,6 @@ import { useFormChangeTrackerService, useUniqueFormId } from "hooks/services/For
13
13
import { isDefined } from "utils/common" ;
14
14
import RequestConnectorModal from "views/Connector/RequestConnectorModal" ;
15
15
16
- import { ConnectionConfiguration } from "../../../core/domain/connection" ;
17
16
import { CheckConnectionRead } from "../../../core/request/AirbyteClient" ;
18
17
import { useDocumentationPanelContext } from "../ConnectorDocumentationLayout/DocumentationPanelContext" ;
19
18
import { ConnectorNameControl } from "./components/Controls/ConnectorNameControl" ;
@@ -41,31 +40,28 @@ const FormikPatch: React.FC = () => {
41
40
*/
42
41
const PatchInitialValuesWithWidgetConfig : React . FC < {
43
42
schema : JSONSchema7 ;
44
- } > = ( { schema } ) => {
43
+ initialValues : ServiceFormValues ;
44
+ } > = ( { schema, initialValues } ) => {
45
45
const { widgetsInfo } = useServiceForm ( ) ;
46
- const { values , resetForm } = useFormikContext < ServiceFormValues > ( ) ;
46
+ const { setFieldValue } = useFormikContext < ServiceFormValues > ( ) ;
47
47
48
- const valueRef = useRef < ConnectionConfiguration > ( ) ;
49
- valueRef . current = values . connectionConfiguration ;
48
+ useDeepCompareEffect ( ( ) => {
49
+ const widgetsInfoEntries = Object . entries ( widgetsInfo ) ;
50
50
51
- useEffect ( ( ) => {
52
51
// set all const fields to form field values, so we could send form
53
- const constPatchedValues = Object . entries ( widgetsInfo )
54
- . filter ( ( [ _ , v ] ) => isDefined ( v . const ) )
55
- . reduce ( ( acc , [ k , v ] ) => setIn ( acc , k , v . const ) , valueRef . current ) ;
52
+ const patchedConstValues = widgetsInfoEntries
53
+ . filter ( ( [ _ , value ] ) => isDefined ( value . const ) )
54
+ . reduce ( ( acc , [ key , value ] ) => setIn ( acc , key , value . const ) , initialValues ) ;
56
55
57
56
// set default fields as current values, so values could be populated correctly
58
57
// fix for https://github.com/airbytehq/airbyte/issues/6791
59
- const defaultPatchedValues = Object . entries ( widgetsInfo )
60
- . filter ( ( [ k , v ] ) => isDefined ( v . default ) && ! isDefined ( getIn ( constPatchedValues , k ) ) )
61
- . reduce ( ( acc , [ k , v ] ) => setIn ( acc , k , v . default ) , constPatchedValues ) ;
62
-
63
- resetForm ( {
64
- values : {
65
- ...values ,
66
- connectionConfiguration : defaultPatchedValues ,
67
- } ,
68
- } ) ;
58
+ const patchedDefaultValues = widgetsInfoEntries
59
+ . filter ( ( [ key , value ] ) => isDefined ( value . default ) && ! isDefined ( getIn ( patchedConstValues , key ) ) )
60
+ . reduce ( ( acc , [ key , value ] ) => setIn ( acc , key , value . default ) , patchedConstValues ) ;
61
+
62
+ if ( patchedDefaultValues ?. connectionConfiguration ) {
63
+ setFieldValue ( "connectionConfiguration" , patchedDefaultValues . connectionConfiguration ) ;
64
+ }
69
65
70
66
// eslint-disable-next-line react-hooks/exhaustive-deps
71
67
} , [ schema ] ) ;
@@ -237,6 +233,7 @@ const ServiceForm: React.FC<ServiceFormProps> = (props) => {
237
233
initialValues = { initialValues }
238
234
validationSchema = { validationSchema }
239
235
onSubmit = { onFormSubmit }
236
+ enableReinitialize
240
237
>
241
238
{ ( { dirty } ) => (
242
239
< ServiceFormContextProvider
@@ -252,7 +249,7 @@ const ServiceForm: React.FC<ServiceFormProps> = (props) => {
252
249
{ ! props . isEditMode && < SetDefaultName /> }
253
250
< FormikPatch />
254
251
< FormChangeTracker changed = { dirty } formId = { formId } />
255
- < PatchInitialValuesWithWidgetConfig schema = { jsonSchema } />
252
+ < PatchInitialValuesWithWidgetConfig schema = { jsonSchema } initialValues = { initialValues } />
256
253
< FormRoot
257
254
{ ...props }
258
255
errorMessage = { props . errorMessage }
0 commit comments