-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
🪟 🔧 [DRAFT] Refactor ServiceForm and ConnectorCard to move Formik and ServiceFormContext on top level #18229
Closed
Closed
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8910287
Moves Formik from ServiceForm component to ConnectorCard component
YatsukBogdan1 a125887
Merge branch 'master' into byatusk/move-connector-card-buttons
YatsukBogdan1 7a81b8a
Merge branch 'master' into byatusk/move-connector-card-buttons
YatsukBogdan1 be76008
Refactors ServiceForm, ConnectorCard and DeleteBlock; Updates styles …
YatsukBogdan1 a94a4e9
Merge branch 'master' into byatusk/move-connector-card-buttons
YatsukBogdan1 a06f74a
Merge branch 'master' into byatusk/move-connector-card-buttons
YatsukBogdan1 caa1503
Merge branch 'master' into byatusk/move-connector-card-buttons
YatsukBogdan1 779e870
Merge branch 'master' into byatusk/move-connector-card-buttons
YatsukBogdan1 efed94f
Removes redundant functions
YatsukBogdan1 ed069d4
Splits useConnectorCardService hook into two hooks useOnHandleSubmit …
YatsukBogdan1 c9f0d20
Changes save button position to match figma; Updates text for save bu…
YatsukBogdan1 14591eb
Merge branch 'master' into byatusk/move-connector-card-buttons
YatsukBogdan1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface DeleteBlockProps { | ||
type: "source" | "destination" | "connection"; | ||
onDelete: () => Promise<unknown>; | ||
} |
32 changes: 32 additions & 0 deletions
32
airbyte-webapp/src/components/DeleteBlock/useDeleteModal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useCallback } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
import { useConfirmationModalService } from "hooks/services/ConfirmationModal"; | ||
|
||
import { DeleteBlockProps } from "./interfaces"; | ||
|
||
export const useDeleteModal = ({ type, onDelete }: Partial<DeleteBlockProps>): { onDeleteButtonClick: () => void } => { | ||
const { openConfirmationModal, closeConfirmationModal } = useConfirmationModalService(); | ||
const navigate = useNavigate(); | ||
|
||
const onDeleteButtonClick = useCallback(() => { | ||
if (!onDelete) { | ||
return; | ||
} | ||
openConfirmationModal({ | ||
text: `tables.${type}DeleteModalText`, | ||
title: `tables.${type}DeleteConfirm`, | ||
submitButtonText: "form.delete", | ||
onSubmit: async () => { | ||
await onDelete(); | ||
closeConfirmationModal(); | ||
navigate("../.."); | ||
}, | ||
submitButtonDataId: "delete", | ||
}); | ||
}, [closeConfirmationModal, onDelete, openConfirmationModal, navigate, type]); | ||
|
||
return { | ||
onDeleteButtonClick, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 104 additions & 111 deletions
215
airbyte-webapp/src/views/Connector/ConnectorCard/ConnectorCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,121 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { Formik } from "formik"; | ||
import React 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 { 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 { ServiceForm } from "views/Connector/ServiceForm"; | ||
|
||
import { generateMessageFromError } from "../../../utils/errorStatusMessage"; | ||
import { ConnectorServiceTypeControl } from "../ServiceForm/components/Controls/ConnectorServiceTypeControl"; | ||
import { FormControls } from "../ServiceForm/FormControls"; | ||
import { ServiceFormContextProvider } from "../ServiceForm/serviceFormContext"; | ||
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 { | ||
title?: React.ReactNode; | ||
full?: boolean; | ||
jobInfo?: SynchronousJobRead | null; | ||
additionalSelectorComponent?: React.ReactNode; | ||
} | ||
|
||
interface ConnectorCardCreateProps extends ConnectorCardBaseProps { | ||
isEditMode?: false; | ||
} | ||
|
||
interface ConnectorCardEditProps extends ConnectorCardBaseProps { | ||
isEditMode: true; | ||
connector: ConnectorT; | ||
} | ||
|
||
export const ConnectorCard: React.FC<ConnectorCardCreateProps | ConnectorCardEditProps> = ({ | ||
title, | ||
full, | ||
jobInfo, | ||
onSubmit, | ||
additionalSelectorComponent, | ||
...props | ||
}) => { | ||
const [saved, setSaved] = useState(false); | ||
const [errorStatusRequest, setErrorStatusRequest] = useState<Error | null>(null); | ||
const [isFormSubmitting, setIsFormSubmitting] = useState(false); | ||
const [advancedMode] = useAdvancedModeSetting(); | ||
|
||
const { testConnector, isTestConnectionInProgress, onStopTesting, error, reset } = useTestConnector(props); | ||
const { trackTestConnectorFailure, trackTestConnectorSuccess, trackTestConnectorStarted } = | ||
useAnalyticsTrackFunctions(props.formType); | ||
|
||
useEffect(() => { | ||
// Whenever the selected connector changed, reset the check connection call and other errors | ||
reset(); | ||
setErrorStatusRequest(null); | ||
}, [props.selectedConnectorDefinitionSpecification, reset]); | ||
|
||
const onHandleSubmit = async (values: ServiceFormValues) => { | ||
setErrorStatusRequest(null); | ||
setIsFormSubmitting(true); | ||
|
||
const connector = props.availableServices.find((item) => Connector.id(item) === values.serviceType); | ||
|
||
const testConnectorWithTracking = async () => { | ||
trackTestConnectorStarted(connector); | ||
try { | ||
await testConnector(values); | ||
trackTestConnectorSuccess(connector); | ||
} catch (e) { | ||
trackTestConnectorFailure(connector); | ||
throw e; | ||
} | ||
}; | ||
|
||
try { | ||
await testConnectorWithTracking(); | ||
onSubmit(values); | ||
setSaved(true); | ||
} catch (e) { | ||
setErrorStatusRequest(e); | ||
setIsFormSubmitting(false); | ||
} | ||
}; | ||
|
||
const job = jobInfo || LogsRequestError.extractJobInfo(errorStatusRequest); | ||
|
||
const { selectedConnectorDefinitionSpecification, onServiceSelect, availableServices, isEditMode } = props; | ||
const selectedConnectorDefinitionSpecificationId = | ||
selectedConnectorDefinitionSpecification && ConnectorSpecification.id(selectedConnectorDefinitionSpecification); | ||
import { ConnectorCardProps } from "./interfaces"; | ||
import { useConnectorCardService } from "./useConnectorCardService"; | ||
|
||
export const ConnectorCard: React.FC<ConnectorCardProps> = (props) => { | ||
const { | ||
title, | ||
full, | ||
isLoading, | ||
additionalSelectorComponent, | ||
formType, | ||
selectedConnectorDefinitionSpecification, | ||
isEditMode, | ||
availableServices, | ||
onServiceSelect, | ||
onDelete, | ||
} = props; | ||
const { | ||
advancedMode, | ||
error, | ||
formFields, | ||
getValues, | ||
initialValues, | ||
isFormSubmitting, | ||
isTestConnectionInProgress, | ||
job, | ||
jsonSchema, | ||
onFormSubmit, | ||
onHandleSubmit, | ||
onStopTesting, | ||
resetUiWidgetsInfo, | ||
saved, | ||
selectedConnectorDefinitionSpecificationId, | ||
setUiWidgetsInfo, | ||
testConnector, | ||
uiWidgetsInfo, | ||
uniqueFormId, | ||
validationSchema, | ||
} = useConnectorCardService(props); | ||
|
||
return ( | ||
<Card title={title} fullWidth={full}> | ||
<div className={styles.cardForm}> | ||
<div className={styles.connectorSelectControl}> | ||
<ConnectorServiceTypeControl | ||
formType={props.formType} | ||
onChangeServiceType={onServiceSelect} | ||
availableServices={availableServices} | ||
isEditMode={isEditMode} | ||
selectedServiceId={selectedConnectorDefinitionSpecificationId} | ||
disabled={isFormSubmitting} | ||
/> | ||
</div> | ||
{additionalSelectorComponent} | ||
<Formik | ||
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. Initially I thought it felt slightly weird to have the |
||
validateOnBlur | ||
validateOnChange | ||
initialValues={initialValues} | ||
validationSchema={validationSchema} | ||
onSubmit={onFormSubmit} | ||
enableReinitialize | ||
> | ||
<ServiceFormContextProvider | ||
widgetsInfo={uiWidgetsInfo} | ||
getValues={getValues} | ||
setUiWidgetsInfo={setUiWidgetsInfo} | ||
resetUiWidgetsInfo={resetUiWidgetsInfo} | ||
formType={formType} | ||
selectedConnector={selectedConnectorDefinitionSpecification} | ||
availableServices={availableServices} | ||
isEditMode={isEditMode} | ||
isLoadingSchema={isLoading} | ||
validationSchema={validationSchema} | ||
> | ||
<div> | ||
<ServiceForm | ||
{...props} | ||
<Card title={title} fullWidth={full}> | ||
<div className={styles.cardForm}> | ||
<div className={styles.connectorSelectControl}> | ||
<ConnectorServiceTypeControl | ||
formType={props.formType} | ||
onChangeServiceType={onServiceSelect} | ||
availableServices={availableServices} | ||
isEditMode={isEditMode} | ||
selectedServiceId={selectedConnectorDefinitionSpecificationId} | ||
disabled={isFormSubmitting} | ||
/> | ||
</div> | ||
{additionalSelectorComponent} | ||
<div> | ||
<ServiceForm | ||
{...props} | ||
formId={uniqueFormId} | ||
jsonSchema={jsonSchema} | ||
isTestConnectionInProgress={isTestConnectionInProgress} | ||
onSubmit={onHandleSubmit} | ||
formFields={formFields} | ||
initialValues={initialValues} | ||
validationSchema={validationSchema} | ||
successMessage={ | ||
props.successMessage || (saved && props.isEditMode && <FormattedMessage id="form.changesSaved" />) | ||
} | ||
/> | ||
{/* Show the job log only if advanced mode is turned on or the actual job failed (not the check inside the job) */} | ||
{job && (advancedMode || !job.succeeded) && <JobItem job={job} />} | ||
</div> | ||
</div> | ||
</Card> | ||
<FormControls | ||
onDelete={onDelete} | ||
errorMessage={props.errorMessage || (error && generateMessageFromError(error))} | ||
isTestConnectionInProgress={isTestConnectionInProgress} | ||
onStopTesting={onStopTesting} | ||
testConnector={testConnector} | ||
onSubmit={onHandleSubmit} | ||
successMessage={ | ||
props.successMessage || (saved && props.isEditMode && <FormattedMessage id="form.changesSaved" />) | ||
} | ||
onStopTestingConnector={onStopTesting ? () => onStopTesting() : undefined} | ||
onRetest={testConnector ? async () => await testConnector() : undefined} | ||
formFields={formFields} | ||
selectedConnector={selectedConnectorDefinitionSpecification} | ||
/> | ||
{/* Show the job log only if advanced mode is turned on or the actual job failed (not the check inside the job) */} | ||
{job && (advancedMode || !job.succeeded) && <JobItem job={job} />} | ||
</div> | ||
</div> | ||
</Card> | ||
</ServiceFormContextProvider> | ||
</Formik> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like the introduction of this hook - its a nice way to keep this logic encapsulated while allowing it to be used in either a DeleteBlock or directly by a button