Skip to content

Commit

Permalink
Set default values as current values in editMode (#10486)
Browse files Browse the repository at this point in the history
* Set default values as current values in editMode

* Fix unit tests
  • Loading branch information
jamakase authored Mar 3, 2022
1 parent bf6afae commit 542bf94
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
9 changes: 7 additions & 2 deletions airbyte-webapp/src/core/form/uiWidget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,19 @@ test("should select correct key for enum", () => {
);
expect(uiWidgetState).toEqual({
"key.dataset_name": {},
"key.format": {},
"key.format": {
default: "csv",
},
"key.provider": {
selectedItem: "GCS: Google Cloud Storage",
},
"key.provider.reader_impl": {},
"key.provider.reader_impl": {
default: "gcsfs",
},
"key.provider.service_account_json": {},
"key.provider.storage": {
const: "GCS",
default: "GCS",
},
"key.reader_options": {},
"key.url": {},
Expand Down
4 changes: 4 additions & 0 deletions airbyte-webapp/src/core/form/uiWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export const buildPathInitialState = (
resultObject.const = formItem.const;
}

if (isDefined(formItem.default)) {
resultObject.default = formItem.default;
}

widgetStateBuilder[formItem.path] = resultObject;
return widgetStateBuilder;
}
Expand Down
23 changes: 17 additions & 6 deletions airbyte-webapp/src/views/Connector/ServiceForm/ServiceForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useEffect, useMemo } from "react";
import { Formik, setIn, useFormikContext } from "formik";
import { Formik, getIn, setIn, useFormikContext } from "formik";
import { JSONSchema7 } from "json-schema";
import { useToggle } from "react-use";

Expand Down Expand Up @@ -59,16 +59,27 @@ const PatchInitialValuesWithWidgetConfig: React.FC<{ schema: JSONSchema7 }> = ({
}) => {
const { widgetsInfo } = useServiceForm();
const { values, setValues } = useFormikContext();
const formInitialValues = useMemo(() => {
return Object.entries(widgetsInfo)

useEffect(() => {
// set all const fields to form field values, so we could send form
const constPatchedValues = Object.entries(widgetsInfo)
.filter(([_, v]) => isDefined(v.const))
.reduce((acc, [k, v]) => setIn(acc, k, v.const), values);

// set default fields as current values, so values could be populated correctly
// fix for https://github.com/airbytehq/airbyte/issues/6791
const defaultPatchedValues = Object.entries(widgetsInfo)
.filter(
([k, v]) =>
isDefined(v.default) && !isDefined(getIn(constPatchedValues, k))
)
.reduce((acc, [k, v]) => setIn(acc, k, v.default), constPatchedValues);

setValues(defaultPatchedValues);

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [schema]);

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => setValues(formInitialValues), [formInitialValues]);

return null;
};

Expand Down

0 comments on commit 542bf94

Please sign in to comment.