-
Notifications
You must be signed in to change notification settings - Fork 4.6k
🪟 🔧 Move the serviceType
prop out from Formik form (<ServiceForm/> refactoring part 2)
#18168
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
Changes from 12 commits
cf2b5b5
92ce4bc
2a190d6
8897903
176cef3
c4ee73c
59bb27d
96439f6
f1df51f
b7d55cf
2d4fd37
3d6fe3d
f2a9e92
4588f4c
1da56c3
53ce62c
e328916
8dc42ac
c931344
97f9ba7
e5b4149
da7ac94
8b8647a
7a5773d
f70e1de
73d87ae
0e7c761
89e0431
3dc4247
18f5b37
80a3b9d
19583a5
1c6c803
df70625
a78c2de
11156a9
e5dc205
88dcb96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,51 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import React, { useEffect, useMemo, useState } from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
import { JobItem } from "components/JobItem/JobItem"; | ||
import { Card } from "components/ui/Card"; | ||
|
||
import { Connector, ConnectorSpecification, ConnectorT } from "core/domain/connector"; | ||
import { | ||
Connector, | ||
ConnectorDefinition, | ||
ConnectorDefinitionSpecification, | ||
ConnectorSpecification, | ||
ConnectorT, | ||
} from "core/domain/connector"; | ||
import { SynchronousJobRead } from "core/request/AirbyteClient"; | ||
import { LogsRequestError } from "core/request/LogsRequestError"; | ||
import { useAdvancedModeSetting } from "hooks/services/useAdvancedModeSetting"; | ||
import { generateMessageFromError } from "utils/errorStatusMessage"; | ||
import { ServiceForm, ServiceFormProps, ServiceFormValues } from "views/Connector/ServiceForm"; | ||
import { ConnectorFormValues, ServiceForm, ServiceFormValues } from "views/Connector/ServiceForm"; | ||
|
||
import { useDocumentationPanelContext } from "../ConnectorDocumentationLayout/DocumentationPanelContext"; | ||
import { ConnectorServiceTypeControl } from "../ServiceForm/components/Controls/ConnectorServiceTypeControl"; | ||
import styles from "./ConnectorCard.module.scss"; | ||
import { useAnalyticsTrackFunctions } from "./useAnalyticsTrackFunctions"; | ||
import { useTestConnector } from "./useTestConnector"; | ||
|
||
type ConnectorCardProvidedProps = Omit< | ||
ServiceFormProps, | ||
"isKeyConnectionInProgress" | "isSuccess" | "onStopTesting" | "testConnector" | ||
>; | ||
|
||
interface ConnectorCardBaseProps extends ConnectorCardProvidedProps { | ||
// TODO: need to clean up the ConnectorCard and ServiceForm props, | ||
// since some of props are used in both components, and some of them used just as a prop-drill | ||
interface ConnectorCardBaseProps { | ||
title?: React.ReactNode; | ||
full?: boolean; | ||
jobInfo?: SynchronousJobRead | null; | ||
additionalSelectorComponent?: React.ReactNode; | ||
onSubmit: (values: ConnectorFormValues) => Promise<void> | void; | ||
onServiceSelect?: (id: string) => void; | ||
availableServices: ConnectorDefinition[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we're here changing things anyway, we should go ahead and try to get rid of the term "service" from all of these places. It is confusing that this component and the ServiceForm component are the only places that refer to connectors and connector definitions as "services", so we should fix that. In this case, better names for these props would be Ideally we would also rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also was thinking about that, but wasn't sure 😊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decided to try to rename all "service" related usages - got more modified files than I expected 😕 |
||
|
||
// used in ConnectorCard and ServiceForm | ||
formType: "source" | "destination"; | ||
selectedConnectorDefinitionSpecification?: ConnectorDefinitionSpecification; | ||
isEditMode?: boolean; | ||
|
||
// used in ServiceForm | ||
formId?: string; | ||
fetchingConnectorError?: Error | null; | ||
errorMessage?: React.ReactNode; | ||
successMessage?: React.ReactNode; | ||
hasSuccess?: boolean; | ||
isLoading?: boolean; | ||
} | ||
|
||
interface ConnectorCardCreateProps extends ConnectorCardBaseProps { | ||
|
@@ -50,6 +70,7 @@ export const ConnectorCard: React.FC<ConnectorCardCreateProps | ConnectorCardEdi | |
const [isFormSubmitting, setIsFormSubmitting] = useState(false); | ||
const [advancedMode] = useAdvancedModeSetting(); | ||
|
||
const { setDocumentationUrl, setDocumentationPanelOpen } = useDocumentationPanelContext(); | ||
const { testConnector, isTestConnectionInProgress, onStopTesting, error, reset } = useTestConnector(props); | ||
const { trackTestConnectorFailure, trackTestConnectorSuccess, trackTestConnectorStarted } = | ||
useAnalyticsTrackFunctions(props.formType); | ||
|
@@ -60,26 +81,50 @@ export const ConnectorCard: React.FC<ConnectorCardCreateProps | ConnectorCardEdi | |
setErrorStatusRequest(null); | ||
}, [props.selectedConnectorDefinitionSpecification, reset]); | ||
|
||
const { selectedConnectorDefinitionSpecification, onServiceSelect, availableServices, isEditMode } = props; | ||
|
||
const selectedConnectorDefinitionSpecificationId = | ||
selectedConnectorDefinitionSpecification && ConnectorSpecification.id(selectedConnectorDefinitionSpecification); | ||
|
||
const selectedService = useMemo( | ||
() => availableServices.find((s) => Connector.id(s) === selectedConnectorDefinitionSpecificationId), | ||
[availableServices, selectedConnectorDefinitionSpecificationId] | ||
); | ||
|
||
// Handle Doc panel | ||
useEffect(() => { | ||
if (!selectedService) { | ||
return; | ||
} | ||
|
||
setDocumentationUrl(selectedService?.documentationUrl ?? ""); | ||
setDocumentationPanelOpen(true); | ||
}, [selectedConnectorDefinitionSpecification, selectedService, setDocumentationPanelOpen, setDocumentationUrl]); | ||
|
||
const onHandleSubmit = async (values: ServiceFormValues) => { | ||
if (!selectedService) { | ||
return; | ||
} | ||
setErrorStatusRequest(null); | ||
setIsFormSubmitting(true); | ||
|
||
const connector = props.availableServices.find((item) => Connector.id(item) === values.serviceType); | ||
// combine the "ServiceFormValues" and serviceType to make "ConnectorFormValues" | ||
const connectorFormValues: ConnectorFormValues = { ...values, serviceType: Connector.id(selectedService) }; | ||
|
||
const testConnectorWithTracking = async () => { | ||
trackTestConnectorStarted(connector); | ||
trackTestConnectorStarted(selectedService); | ||
try { | ||
await testConnector(values); | ||
trackTestConnectorSuccess(connector); | ||
await testConnector(connectorFormValues); | ||
trackTestConnectorSuccess(selectedService); | ||
} catch (e) { | ||
trackTestConnectorFailure(connector); | ||
trackTestConnectorFailure(selectedService); | ||
throw e; | ||
} | ||
}; | ||
|
||
try { | ||
await testConnectorWithTracking(); | ||
onSubmit(values); | ||
onSubmit(connectorFormValues); | ||
setSaved(true); | ||
} catch (e) { | ||
setErrorStatusRequest(e); | ||
|
@@ -89,9 +134,8 @@ export const ConnectorCard: React.FC<ConnectorCardCreateProps | ConnectorCardEdi | |
|
||
const job = jobInfo || LogsRequestError.extractJobInfo(errorStatusRequest); | ||
|
||
const { selectedConnectorDefinitionSpecification, onServiceSelect, availableServices, isEditMode } = props; | ||
const selectedConnectorDefinitionSpecificationId = | ||
selectedConnectorDefinitionSpecification && ConnectorSpecification.id(selectedConnectorDefinitionSpecification); | ||
// Fill form with existing connector values otherwise set the default service name | ||
const formValues = isEditMode ? props.connector : { name: selectedService?.name }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest we move the default name logic into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds reasonable, and I also did a try:
Also did a try to pass the Should I continue digging into it? |
||
|
||
return ( | ||
<Card title={title} fullWidth={full}> | ||
|
@@ -110,11 +154,13 @@ export const ConnectorCard: React.FC<ConnectorCardCreateProps | ConnectorCardEdi | |
<div> | ||
<ServiceForm | ||
{...props} | ||
errorMessage={props.errorMessage || (error && generateMessageFromError(error))} | ||
selectedService={selectedService} | ||
isTestConnectionInProgress={isTestConnectionInProgress} | ||
onStopTesting={onStopTesting} | ||
testConnector={testConnector} | ||
onSubmit={onHandleSubmit} | ||
formValues={formValues} | ||
errorMessage={props.errorMessage || (error && generateMessageFromError(error))} | ||
successMessage={ | ||
props.successMessage || (saved && props.isEditMode && <FormattedMessage id="form.changesSaved" />) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you create an issue for this, which captures why the current state is bad and what might be a better solution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, here is it: #18553
I will add it to comment also