Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select connector type before name #11388

Merged
merged 5 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions airbyte-webapp-e2e-tests/cypress/support/commands/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Cypress.Commands.add("fillEmail", (email) => {
Cypress.Commands.add("fillTestLocalJsonForm", (name) => {
cy.intercept("/api/v1/destination_definition_specifications/get").as("getDestinationSpecifications");

cy.get("input[name=name]").type(name);
cy.get("div[data-testid='serviceType']").click();
cy.get("div").contains("Local JSON").click();

cy.wait("@getDestinationSpecifications");


cy.get("input[name=name]").type(name);
cy.get("input[name='connectionConfiguration.destination_path']").type("/local");
})

Expand Down
8 changes: 4 additions & 4 deletions airbyte-webapp-e2e-tests/cypress/support/commands/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ Cypress.Commands.add("fillPgSourceForm", (name) => {
cy.intercept("/api/v1/source_definition_specifications/get").as(
"getSourceSpecifications"
);

cy.get("input[name=name]").type(name);

cy.get("div[data-testid='serviceType']").click();
cy.get("div").contains("Postgres").click();

cy.wait("@getSourceSpecifications");


cy.get("input[name=name]").type(name);
cy.get("input[name='connectionConfiguration.host']").type("localhost");
cy.get("input[name='connectionConfiguration.port']").type("{selectAll}{del}5433");
cy.get("input[name='connectionConfiguration.database']").type("airbyte_ci");
Expand Down
39 changes: 36 additions & 3 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, getIn, setIn, useFormikContext } from "formik";
import { Formik, getIn, setIn, useField, useFormikContext } from "formik";
import { JSONSchema7 } from "json-schema";
import { useToggle } from "react-use";

Expand All @@ -21,6 +21,7 @@ import { FormBaseItem } from "core/form/types";
import { ConnectorNameControl } from "./components/Controls/ConnectorNameControl";
import { ConnectorServiceTypeControl } from "./components/Controls/ConnectorServiceTypeControl";
import {
Connector,
ConnectorDefinition,
ConnectorDefinitionSpecification,
} from "core/domain/connector";
Expand Down Expand Up @@ -83,9 +84,40 @@ const PatchInitialValuesWithWidgetConfig: React.FC<{ schema: JSONSchema7 }> = ({
return null;
};

/**
* A component that will observe whenever the serviceType (selected connector)
* changes and set the name of the connector to match the connector definition name.
*/
const SetDefaultName: React.FC<{
availableConnectors: ConnectorDefinition[];
}> = ({ availableConnectors }) => {
Copy link
Contributor

@jamakase jamakase Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing availableConnectors and searching - you could use useServiceForm hook that has selectedService prop.

So this method will shorten to something like

const { selectedService } = useServiceForm();
const [,,helpers] = useField("name");

useEffect(() => {
  const name = selectedService?.name ?? "";
  helpers.setValue(name);
}, [selectedService, helpers]);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will use useServiceForm here.

I tried using useField initially, but the problem is that the helpers it's returning isn't a stable reference, thus causing this useEffect to trigger in an endless loop, thus using the setFieldValue here instead (which is a stable reference).

const { setFieldValue, setFieldTouched } = useFormikContext();
const [serviceTypeField] = useField("serviceType");

useEffect(() => {
const name =
availableConnectors.find(
(service) => Connector.id(service) === serviceTypeField.value
)?.name ?? "";
// Formik has an issue, that prevents us from setting a field value directly in code here
// It won't change the value at all, unless we push it one execution slot further with setTimeout
setTimeout(() => {
timroes marked this conversation as resolved.
Show resolved Hide resolved
setFieldValue("name", name);
});
}, [
availableConnectors,
serviceTypeField.value,
setFieldTouched,
setFieldValue,
]);

return null;
};

const ServiceForm: React.FC<ServiceFormProps> = (props) => {
const [isOpenRequestModal, toggleOpenRequestModal] = useToggle(false);
const {
availableServices,
formType,
formValues,
onSubmit,
Expand All @@ -100,8 +132,8 @@ const ServiceForm: React.FC<ServiceFormProps> = (props) => {
() => ({
type: "object",
properties: {
name: { type: "string" },
serviceType: { type: "string" },
...(selectedConnector ? { name: { type: "string" } } : {}),
...Object.fromEntries(
Object.entries({
connectionConfiguration: isLoading ? null : specifications,
Expand All @@ -110,7 +142,7 @@ const ServiceForm: React.FC<ServiceFormProps> = (props) => {
},
required: ["name", "serviceType"],
}),
[isLoading, specifications]
[isLoading, selectedConnector, specifications]
);

const { formFields, initialValues } = useBuildForm(jsonSchema, formValues);
Expand Down Expand Up @@ -206,6 +238,7 @@ const ServiceForm: React.FC<ServiceFormProps> = (props) => {
isEditMode={props.isEditMode}
isLoadingSchema={props.isLoading}
>
<SetDefaultName availableConnectors={availableServices} />
<FormikPatch />
<PatchInitialValuesWithWidgetConfig schema={jsonSchema} />
<FormRoot
Expand Down