Skip to content

Commit

Permalink
refactor: move data connector validation code lower
Browse files Browse the repository at this point in the history
  • Loading branch information
ciyer committed Nov 1, 2024
1 parent c01376c commit 64d6745
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ export default function DataConnectorModalBody({
function AddOrEditDataConnector({
storageSecrets,
}: AddOrEditDataConnectorProps) {
const {
cloudStorageState,
flatDataConnector,
schemata,
validationSucceeded,
} = useAppSelector((state) => state.dataConnectorFormSlice);
const { cloudStorageState, flatDataConnector, schemata, validationResult } =
useAppSelector((state) => state.dataConnectorFormSlice);
const dispatch = useAppDispatch();
const setState = useCallback(
(newState: Partial<AddCloudStorageState>) => {
Expand Down Expand Up @@ -135,7 +131,7 @@ function AddOrEditDataConnector({
setState={setState}
setStorage={setFlatDataConnector}
storageSecrets={storageSecrets}
validationSucceeded={validationSucceeded ?? false}
validationSucceeded={validationResult?.isSuccess ?? false}
isV2={true}
/>
</>
Expand Down Expand Up @@ -245,7 +241,7 @@ export function DataConnectorMount() {
]
);

const { validationSucceeded } = useAppSelector(
const { validationResult } = useAppSelector(
(state) => state.dataConnectorFormSlice
);
const options = getSchemaOptions(
Expand Down Expand Up @@ -534,7 +530,7 @@ export function DataConnectorMount() {

{flatDataConnector.dataConnectorId == null &&
hasPasswordFieldWithInput &&
validationSucceeded && (
validationResult?.isSuccess && (
<DataConnectorSaveCredentialsInfo
control={control}
onFieldValueChange={onFieldValueChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import useAppDispatch from "../../../../utils/customHooks/useAppDispatch.hook";
import useAppSelector from "../../../../utils/customHooks/useAppSelector.hook";

import AddStorageBreadcrumbNavbar from "../../../project/components/cloudStorage/AddStorageBreadcrumbNavbar";
import { useTestCloudStorageConnectionMutation } from "../../../project/components/cloudStorage/projectCloudStorage.api";
import {
AddCloudStorageState,
CloudStorageDetailsOptions,
TestCloudStorageConnectionParams,
} from "../../../project/components/cloudStorage/projectCloudStorage.types";

import {
Expand Down Expand Up @@ -82,9 +80,10 @@ export default function DataConnectorModalFooter({
const {
cloudStorageState,
flatDataConnector,
isActionOngoing,
schemata,
success,
validationSucceeded,
validationResult,
} = useAppSelector((state) => state.dataConnectorFormSlice);

// Enhanced setters
Expand All @@ -107,45 +106,18 @@ export default function DataConnectorModalFooter({
usePatchDataConnectorsByDataConnectorIdSecretsMutation();
const [createProjectLink, createProjectLinkResult] =
usePostDataConnectorsByDataConnectorIdProjectLinksMutation();
const [validateCloudStorageConnection, validationResult] =
useTestCloudStorageConnectionMutation();

const reset = useCallback(() => {
const resetStatus = dataConnectorToFlattened(dataConnector);
createResult.reset();
updateResult.reset();
validationResult.reset();
dispatch(
dataConnectorFormSlice.actions.reset({
flatDataConnector: resetStatus,
hasDataConnector: dataConnector != null,
})
);
}, [createResult, dataConnector, dispatch, updateResult, validationResult]);

const validateConnection = useCallback(() => {
const validateParameters: TestCloudStorageConnectionParams = {
configuration: {
type: flatDataConnector.schema,
},
source_path: flatDataConnector.sourcePath ?? "/",
};
if (flatDataConnector.provider) {
validateParameters.configuration.provider = flatDataConnector.provider;
}
if (
flatDataConnector.options &&
Object.keys(flatDataConnector.options).length > 0
) {
const options = flatDataConnector.options as CloudStorageDetailsOptions;
Object.entries(options).forEach(([key, value]) => {
if (value != undefined && value !== "") {
validateParameters.configuration[key] = value;
}
});
}
validateCloudStorageConnection(validateParameters);
}, [flatDataConnector, validateCloudStorageConnection]);
}, [createResult, dataConnector, dispatch, updateResult]);

const addOrEditStorage = useCallback(() => {
const dataConnectorPost = dataConnectorPostFromFlattened(
Expand Down Expand Up @@ -211,7 +183,7 @@ export default function DataConnectorModalFooter({
const shouldSaveCredentials = shouldSaveDataConnectorCredentials(
flatDataConnector.options,
cloudStorageState.saveCredentials,
validationSucceeded ?? false
validationResult?.isSuccess ?? false
);
if (!shouldSaveCredentials) return;

Expand Down Expand Up @@ -241,27 +213,28 @@ export default function DataConnectorModalFooter({
schemata,
flatDataConnector.options,
flatDataConnector.schema,
validationSucceeded,
validationResult?.isSuccess,
]);

useEffect(() => {
const status = !validationSucceeded
? "none"
: createResult.data?.id == null || saveCredentialsResult.isUninitialized
? "none"
: saveCredentialsResult.isLoading
? "trying"
: saveCredentialsResult.isSuccess
? "success"
: saveCredentialsResult.isError
? "failure"
: "none";
const status =
validationResult?.isSuccess != true
? "none"
: createResult.data?.id == null || saveCredentialsResult.isUninitialized
? "none"
: saveCredentialsResult.isLoading
? "trying"
: saveCredentialsResult.isSuccess
? "success"
: saveCredentialsResult.isError
? "failure"
: "none";
dispatch(
dataConnectorFormSlice.actions.setCredentialSaveStatus({
credentialSaveStatus: status,
})
);
}, [createResult, dispatch, saveCredentialsResult, validationSucceeded]);
}, [createResult, dispatch, saveCredentialsResult, validationResult]);

useEffect(() => {
const dataConnectorId = createResult.data?.id;
Expand Down Expand Up @@ -337,7 +310,7 @@ export default function DataConnectorModalFooter({

return (
<>
<DataConnectorConnectionTestResult validationResult={validationResult} />
<DataConnectorConnectionTestResult />
{actionError && (
<div className="w-100">
<RtkOrNotebooksError error={actionError} />
Expand All @@ -353,7 +326,7 @@ export default function DataConnectorModalFooter({
<Button
color="outline-danger"
data-cy="data-connector-edit-rest-button"
disabled={validationResult.isLoading}
disabled={isActionOngoing}
onClick={() => {
reset();
}}
Expand All @@ -363,11 +336,7 @@ export default function DataConnectorModalFooter({
</Button>
)}
{!isResultLoading && (
<DataConnectorModalBackButton
success={success}
toggle={toggle}
validationResult={validationResult}
/>
<DataConnectorModalBackButton success={success} toggle={toggle} />
)}
{!success && (
<DataConnectorModalContinueButton
Expand All @@ -378,8 +347,6 @@ export default function DataConnectorModalFooter({
hasStoredCredentialsInConfig={hasStoredCredentialsInConfig}
isResultLoading={isResultLoading}
dataConnectorId={dataConnectorId}
validateConnection={validateConnection}
validationResult={validationResult}
/>
)}
</>
Expand Down
Loading

0 comments on commit 64d6745

Please sign in to comment.