2022-03-30 13:11:25 +00:00
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
|
import styled from "styled-components";
|
2022-12-20 15:10:18 +00:00
|
|
|
import { ActionButton } from "pages/Editor/DataSourceEditor/JSONtoForm";
|
2022-03-30 13:11:25 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
|
import {
|
|
|
|
|
getEntities,
|
|
|
|
|
getPluginTypeFromDatasourceId,
|
|
|
|
|
} from "selectors/entitiesSelector";
|
|
|
|
|
import {
|
|
|
|
|
testDatasource,
|
|
|
|
|
deleteDatasource,
|
|
|
|
|
updateDatasource,
|
|
|
|
|
redirectAuthorizationCode,
|
|
|
|
|
getOAuthAccessToken,
|
2022-12-02 03:06:22 +00:00
|
|
|
setDatasourceViewMode,
|
2022-11-30 05:59:45 +00:00
|
|
|
createDatasourceFromForm,
|
|
|
|
|
toggleSaveActionFlag,
|
2022-03-30 13:11:25 +00:00
|
|
|
} from "actions/datasourceActions";
|
|
|
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
|
|
|
|
import { getCurrentApplicationId } from "selectors/editorSelectors";
|
|
|
|
|
import { useParams, useLocation } from "react-router";
|
|
|
|
|
import { ExplorerURLParams } from "pages/Editor/Explorer/helpers";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2022-03-30 13:11:25 +00:00
|
|
|
import {
|
|
|
|
|
AuthType,
|
|
|
|
|
Datasource,
|
|
|
|
|
AuthenticationStatus,
|
|
|
|
|
} from "entities/Datasource";
|
|
|
|
|
import {
|
2022-12-20 15:10:18 +00:00
|
|
|
CONFIRM_CONTEXT_DELETING,
|
2022-03-30 13:11:25 +00:00
|
|
|
OAUTH_AUTHORIZATION_APPSMITH_ERROR,
|
|
|
|
|
OAUTH_AUTHORIZATION_FAILED,
|
|
|
|
|
} from "@appsmith/constants/messages";
|
2022-12-01 06:30:50 +00:00
|
|
|
import { Category, Toaster, Variant } from "design-system";
|
2022-03-30 13:11:25 +00:00
|
|
|
import {
|
|
|
|
|
CONTEXT_DELETE,
|
|
|
|
|
CONFIRM_CONTEXT_DELETE,
|
|
|
|
|
createMessage,
|
|
|
|
|
} from "@appsmith/constants/messages";
|
|
|
|
|
import { debounce } from "lodash";
|
2022-12-20 15:10:18 +00:00
|
|
|
import { ApiDatasourceForm } from "entities/Datasource/RestAPIForm";
|
2022-11-30 05:59:45 +00:00
|
|
|
import { TEMP_DATASOURCE_ID } from "constants/Datasource";
|
2022-01-14 06:31:54 +00:00
|
|
|
|
2022-12-01 06:30:50 +00:00
|
|
|
import {
|
|
|
|
|
hasDeleteDatasourcePermission,
|
|
|
|
|
hasManageDatasourcePermission,
|
|
|
|
|
} from "@appsmith/utils/permissionHelpers";
|
|
|
|
|
|
2022-01-14 06:31:54 +00:00
|
|
|
interface Props {
|
|
|
|
|
datasource: Datasource;
|
2022-12-20 15:10:18 +00:00
|
|
|
formData: Datasource | ApiDatasourceForm;
|
2022-01-14 06:31:54 +00:00
|
|
|
getSanitizedFormData: () => Datasource;
|
|
|
|
|
isInvalid: boolean;
|
2022-03-17 10:28:54 +00:00
|
|
|
pageId?: string;
|
2022-12-20 15:10:18 +00:00
|
|
|
shouldRender?: boolean;
|
2022-03-30 13:11:25 +00:00
|
|
|
datasourceButtonConfiguration: string[] | undefined;
|
2022-12-20 15:10:18 +00:00
|
|
|
shouldDisplayAuthMessage?: boolean;
|
2022-11-30 05:59:45 +00:00
|
|
|
triggerSave?: boolean;
|
|
|
|
|
isFormDirty?: boolean;
|
|
|
|
|
datasourceDeleteTrigger: () => void;
|
2022-01-14 06:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-30 13:11:25 +00:00
|
|
|
export type DatasourceFormButtonTypes = Record<string, string[]>;
|
|
|
|
|
|
|
|
|
|
enum AuthorizationStatus {
|
|
|
|
|
SUCCESS = "success",
|
|
|
|
|
APPSMITH_ERROR = "appsmith_error",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export enum DatasourceButtonTypeEnum {
|
|
|
|
|
DELETE = "DELETE",
|
|
|
|
|
SAVE = "SAVE",
|
|
|
|
|
TEST = "TEST",
|
|
|
|
|
SAVE_AND_AUTHORIZE = "SAVE_AND_AUTHORIZE",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const DatasourceButtonType: Record<
|
|
|
|
|
keyof typeof DatasourceButtonTypeEnum,
|
|
|
|
|
string
|
|
|
|
|
> = {
|
|
|
|
|
DELETE: "DELETE",
|
|
|
|
|
SAVE: "SAVE",
|
|
|
|
|
TEST: "TEST",
|
|
|
|
|
SAVE_AND_AUTHORIZE: "SAVE_AND_AUTHORIZE",
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-30 05:59:45 +00:00
|
|
|
const StyledButton = styled(ActionButton)<{ fluidWidth?: boolean }>`
|
2022-03-30 13:11:25 +00:00
|
|
|
&&&& {
|
|
|
|
|
height: 32px;
|
|
|
|
|
width: ${(props) => (props.fluidWidth ? "" : "87px")};
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2022-12-20 15:10:18 +00:00
|
|
|
const SaveButtonContainer = styled.div`
|
|
|
|
|
margin-top: 24px;
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
`;
|
|
|
|
|
|
2022-03-30 13:11:25 +00:00
|
|
|
const StyledAuthMessage = styled.div`
|
|
|
|
|
color: ${(props) => props.theme.colors.error};
|
|
|
|
|
margin-top: 15px;
|
|
|
|
|
&:after {
|
|
|
|
|
content: " *";
|
|
|
|
|
color: inherit;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2022-01-14 06:31:54 +00:00
|
|
|
function DatasourceAuth({
|
|
|
|
|
datasource,
|
2022-03-30 13:11:25 +00:00
|
|
|
datasourceButtonConfiguration = ["DELETE", "SAVE"],
|
2022-11-30 05:59:45 +00:00
|
|
|
datasourceDeleteTrigger,
|
2022-01-14 06:31:54 +00:00
|
|
|
formData,
|
|
|
|
|
getSanitizedFormData,
|
|
|
|
|
isInvalid,
|
2022-03-30 13:11:25 +00:00
|
|
|
pageId: pageIdProp,
|
2022-01-14 06:31:54 +00:00
|
|
|
shouldRender,
|
2022-12-20 15:10:18 +00:00
|
|
|
shouldDisplayAuthMessage = true,
|
2022-11-30 05:59:45 +00:00
|
|
|
triggerSave,
|
|
|
|
|
isFormDirty,
|
2022-01-14 06:31:54 +00:00
|
|
|
}: Props) {
|
|
|
|
|
const authType =
|
2022-12-20 15:10:18 +00:00
|
|
|
formData && "authType" in formData
|
|
|
|
|
? formData?.authType
|
|
|
|
|
: formData?.datasourceConfiguration?.authentication?.authenticationType;
|
2022-03-30 13:11:25 +00:00
|
|
|
|
2022-11-04 05:55:25 +00:00
|
|
|
const { id: datasourceId, isDeleting } = datasource;
|
2022-03-30 13:11:25 +00:00
|
|
|
const applicationId = useSelector(getCurrentApplicationId);
|
|
|
|
|
|
2022-12-01 06:30:50 +00:00
|
|
|
const datasourcePermissions = datasource.userPermissions || [];
|
|
|
|
|
|
|
|
|
|
const canManageDatasource = hasManageDatasourcePermission(
|
|
|
|
|
datasourcePermissions,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const canDeleteDatasource = hasDeleteDatasourcePermission(
|
|
|
|
|
datasourcePermissions,
|
|
|
|
|
);
|
|
|
|
|
|
2022-03-30 13:11:25 +00:00
|
|
|
// hooks
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const location = useLocation();
|
|
|
|
|
const { pageId: pageIdQuery } = useParams<ExplorerURLParams>();
|
|
|
|
|
|
|
|
|
|
const pageId = (pageIdQuery || pageIdProp) as string;
|
|
|
|
|
const [confirmDelete, setConfirmDelete] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (confirmDelete) {
|
|
|
|
|
delayConfirmDeleteToFalse();
|
|
|
|
|
}
|
|
|
|
|
}, [confirmDelete]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (authType === AuthType.OAUTH2) {
|
|
|
|
|
// When the authorization server redirects a user to the datasource form page, the url contains the "response_status" query parameter .
|
|
|
|
|
// Get the access token if response_status is successful else show a toast error
|
|
|
|
|
|
|
|
|
|
const search = new URLSearchParams(location.search);
|
|
|
|
|
const status = search.get("response_status");
|
|
|
|
|
const queryIsImport = search.get("importForGit");
|
|
|
|
|
const queryDatasourceId = search.get("datasourceId");
|
|
|
|
|
const shouldNotify =
|
|
|
|
|
!queryIsImport || (queryIsImport && queryDatasourceId === datasourceId);
|
|
|
|
|
if (status && shouldNotify) {
|
|
|
|
|
const display_message = search.get("display_message");
|
|
|
|
|
const variant = Variant.danger;
|
|
|
|
|
|
|
|
|
|
if (status !== AuthorizationStatus.SUCCESS) {
|
|
|
|
|
const message =
|
|
|
|
|
status === AuthorizationStatus.APPSMITH_ERROR
|
|
|
|
|
? OAUTH_AUTHORIZATION_APPSMITH_ERROR
|
|
|
|
|
: OAUTH_AUTHORIZATION_FAILED;
|
|
|
|
|
Toaster.show({ text: display_message || message, variant });
|
|
|
|
|
} else {
|
|
|
|
|
dispatch(getOAuthAccessToken(datasourceId));
|
|
|
|
|
}
|
|
|
|
|
AnalyticsUtil.logEvent("DATASOURCE_AUTH_COMPLETE", {
|
|
|
|
|
applicationId,
|
|
|
|
|
datasourceId,
|
|
|
|
|
pageId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [authType]);
|
|
|
|
|
|
|
|
|
|
// selectors
|
|
|
|
|
const {
|
2022-11-04 05:55:25 +00:00
|
|
|
datasources: { isTesting, loading: isSaving },
|
2022-03-30 13:11:25 +00:00
|
|
|
} = useSelector(getEntities);
|
|
|
|
|
|
2022-10-19 10:34:12 +00:00
|
|
|
const delayConfirmDeleteToFalse = debounce(
|
|
|
|
|
() => setConfirmDelete(false),
|
|
|
|
|
2200,
|
|
|
|
|
);
|
|
|
|
|
|
2022-03-30 13:11:25 +00:00
|
|
|
const pluginType = useSelector((state: AppState) =>
|
|
|
|
|
getPluginTypeFromDatasourceId(state, datasourceId),
|
|
|
|
|
);
|
|
|
|
|
|
2022-11-30 05:59:45 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (triggerSave) {
|
|
|
|
|
if (pluginType === "SAAS") {
|
|
|
|
|
handleOauthDatasourceSave();
|
|
|
|
|
} else {
|
|
|
|
|
handleDefaultAuthDatasourceSave();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [triggerSave]);
|
2022-03-30 13:11:25 +00:00
|
|
|
|
|
|
|
|
const isAuthorized =
|
|
|
|
|
datasource?.datasourceConfiguration?.authentication
|
|
|
|
|
?.authenticationStatus === AuthenticationStatus.SUCCESS;
|
|
|
|
|
|
|
|
|
|
// Button Operations for respective buttons.
|
|
|
|
|
|
|
|
|
|
// Handles datasource deletion
|
|
|
|
|
const handleDatasourceDelete = () => {
|
|
|
|
|
dispatch(deleteDatasource({ id: datasourceId }));
|
2022-11-30 05:59:45 +00:00
|
|
|
datasourceDeleteTrigger();
|
2022-03-30 13:11:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Handles datasource testing
|
|
|
|
|
const handleDatasourceTest = () => {
|
|
|
|
|
AnalyticsUtil.logEvent("TEST_DATA_SOURCE_CLICK", {
|
|
|
|
|
pageId: pageId,
|
|
|
|
|
appId: applicationId,
|
|
|
|
|
});
|
|
|
|
|
dispatch(testDatasource(getSanitizedFormData()));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Handles default auth datasource saving
|
|
|
|
|
const handleDefaultAuthDatasourceSave = () => {
|
2022-11-30 05:59:45 +00:00
|
|
|
dispatch(toggleSaveActionFlag(true));
|
2022-03-30 13:11:25 +00:00
|
|
|
AnalyticsUtil.logEvent("SAVE_DATA_SOURCE_CLICK", {
|
|
|
|
|
pageId: pageId,
|
|
|
|
|
appId: applicationId,
|
|
|
|
|
});
|
|
|
|
|
// After saving datasource, only redirect to the 'new integrations' page
|
|
|
|
|
// if datasource is not used to generate a page
|
2022-11-30 05:59:45 +00:00
|
|
|
if (datasource.id === TEMP_DATASOURCE_ID) {
|
|
|
|
|
dispatch(createDatasourceFromForm(getSanitizedFormData()));
|
|
|
|
|
} else {
|
2022-12-02 03:06:22 +00:00
|
|
|
dispatch(setDatasourceViewMode(true));
|
2022-11-30 05:59:45 +00:00
|
|
|
// we dont need to redirect it to active ds list instead ds would be shown in view only mode
|
|
|
|
|
dispatch(updateDatasource(getSanitizedFormData()));
|
|
|
|
|
}
|
2022-03-30 13:11:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Handles Oauth datasource saving
|
|
|
|
|
const handleOauthDatasourceSave = () => {
|
2022-11-30 05:59:45 +00:00
|
|
|
dispatch(toggleSaveActionFlag(true));
|
|
|
|
|
if (datasource.id === TEMP_DATASOURCE_ID) {
|
|
|
|
|
dispatch(
|
|
|
|
|
createDatasourceFromForm(
|
|
|
|
|
getSanitizedFormData(),
|
|
|
|
|
pluginType
|
|
|
|
|
? redirectAuthorizationCode(pageId, datasourceId, pluginType)
|
|
|
|
|
: undefined,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2022-12-02 03:06:22 +00:00
|
|
|
dispatch(setDatasourceViewMode(true));
|
2022-11-30 05:59:45 +00:00
|
|
|
dispatch(
|
|
|
|
|
updateDatasource(
|
|
|
|
|
getSanitizedFormData(),
|
|
|
|
|
pluginType
|
|
|
|
|
? redirectAuthorizationCode(pageId, datasourceId, pluginType)
|
|
|
|
|
: undefined,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-03-30 13:11:25 +00:00
|
|
|
};
|
|
|
|
|
|
2022-12-02 12:35:18 +00:00
|
|
|
const createMode = datasourceId === TEMP_DATASOURCE_ID;
|
|
|
|
|
|
2022-03-30 13:11:25 +00:00
|
|
|
const datasourceButtonsComponentMap = (buttonType: string): JSX.Element => {
|
|
|
|
|
return {
|
|
|
|
|
[DatasourceButtonType.DELETE]: (
|
|
|
|
|
<ActionButton
|
2022-11-30 05:59:45 +00:00
|
|
|
category={Category.primary}
|
2022-03-30 13:11:25 +00:00
|
|
|
className="t--delete-datasource"
|
2022-12-02 12:35:18 +00:00
|
|
|
disabled={createMode || !canDeleteDatasource}
|
2022-12-11 12:51:52 +00:00
|
|
|
isLoading={isDeleting}
|
2022-11-30 05:59:45 +00:00
|
|
|
key={buttonType}
|
2022-03-30 13:11:25 +00:00
|
|
|
onClick={() => {
|
2022-12-20 15:10:18 +00:00
|
|
|
if (!isDeleting) {
|
|
|
|
|
confirmDelete ? handleDatasourceDelete() : setConfirmDelete(true);
|
|
|
|
|
}
|
2022-03-30 13:11:25 +00:00
|
|
|
}}
|
2022-11-30 05:59:45 +00:00
|
|
|
size="medium"
|
|
|
|
|
tag="button"
|
2022-03-30 13:11:25 +00:00
|
|
|
text={
|
2022-12-20 15:10:18 +00:00
|
|
|
isDeleting
|
|
|
|
|
? createMessage(CONFIRM_CONTEXT_DELETING)
|
|
|
|
|
: confirmDelete
|
2022-03-30 13:11:25 +00:00
|
|
|
? createMessage(CONFIRM_CONTEXT_DELETE)
|
|
|
|
|
: createMessage(CONTEXT_DELETE)
|
|
|
|
|
}
|
2022-11-30 05:59:45 +00:00
|
|
|
variant={Variant.danger}
|
2022-01-14 06:31:54 +00:00
|
|
|
/>
|
2022-03-30 13:11:25 +00:00
|
|
|
),
|
|
|
|
|
[DatasourceButtonType.TEST]: (
|
|
|
|
|
<ActionButton
|
2022-11-30 05:59:45 +00:00
|
|
|
category={Category.secondary}
|
2022-03-30 13:11:25 +00:00
|
|
|
className="t--test-datasource"
|
2022-11-30 05:59:45 +00:00
|
|
|
isLoading={isTesting}
|
|
|
|
|
key={buttonType}
|
2022-03-30 13:11:25 +00:00
|
|
|
onClick={handleDatasourceTest}
|
2022-11-30 05:59:45 +00:00
|
|
|
size="medium"
|
2022-12-01 06:30:50 +00:00
|
|
|
tag="button"
|
2022-03-30 13:11:25 +00:00
|
|
|
text="Test"
|
2022-11-30 05:59:45 +00:00
|
|
|
variant={Variant.success}
|
2022-01-14 06:31:54 +00:00
|
|
|
/>
|
2022-03-30 13:11:25 +00:00
|
|
|
),
|
|
|
|
|
[DatasourceButtonType.SAVE]: (
|
2022-11-30 05:59:45 +00:00
|
|
|
<ActionButton
|
|
|
|
|
category={Category.primary}
|
2022-03-30 13:11:25 +00:00
|
|
|
className="t--save-datasource"
|
2022-12-02 12:35:18 +00:00
|
|
|
disabled={
|
|
|
|
|
isInvalid || !isFormDirty || (!createMode && !canManageDatasource)
|
|
|
|
|
}
|
2022-12-01 06:30:50 +00:00
|
|
|
filled
|
2022-12-11 12:51:52 +00:00
|
|
|
isLoading={isSaving}
|
2022-11-30 05:59:45 +00:00
|
|
|
key={buttonType}
|
2022-03-30 13:11:25 +00:00
|
|
|
onClick={handleDefaultAuthDatasourceSave}
|
2022-11-30 05:59:45 +00:00
|
|
|
size="medium"
|
|
|
|
|
tag="button"
|
2022-03-30 13:11:25 +00:00
|
|
|
text="Save"
|
2022-11-30 05:59:45 +00:00
|
|
|
variant={Variant.success}
|
2022-01-14 06:31:54 +00:00
|
|
|
/>
|
2022-03-30 13:11:25 +00:00
|
|
|
),
|
|
|
|
|
[DatasourceButtonType.SAVE_AND_AUTHORIZE]: (
|
|
|
|
|
<StyledButton
|
2022-11-30 05:59:45 +00:00
|
|
|
category={Category.primary}
|
2022-03-30 13:11:25 +00:00
|
|
|
className="t--save-datasource"
|
2022-12-02 12:35:18 +00:00
|
|
|
disabled={isInvalid || (!createMode && !canManageDatasource)}
|
2022-12-01 06:30:50 +00:00
|
|
|
filled
|
2022-03-30 13:11:25 +00:00
|
|
|
fluidWidth
|
2022-11-30 05:59:45 +00:00
|
|
|
isLoading={isSaving}
|
|
|
|
|
key={buttonType}
|
2022-03-30 13:11:25 +00:00
|
|
|
onClick={handleOauthDatasourceSave}
|
2022-11-30 05:59:45 +00:00
|
|
|
size="medium"
|
|
|
|
|
tag="button"
|
2022-03-30 13:11:25 +00:00
|
|
|
text={isAuthorized ? "Save and Re-authorize" : "Save and Authorize"}
|
2022-11-30 05:59:45 +00:00
|
|
|
variant={Variant.success}
|
2022-03-30 13:11:25 +00:00
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
}[buttonType];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2022-12-20 15:10:18 +00:00
|
|
|
{authType === AuthType.OAUTH2 &&
|
|
|
|
|
!isAuthorized &&
|
|
|
|
|
shouldDisplayAuthMessage && (
|
|
|
|
|
<StyledAuthMessage>Datasource not authorized</StyledAuthMessage>
|
|
|
|
|
)}
|
2022-03-30 13:11:25 +00:00
|
|
|
{shouldRender && (
|
|
|
|
|
<SaveButtonContainer>
|
|
|
|
|
{datasourceButtonConfiguration?.map((btnConfig) =>
|
|
|
|
|
datasourceButtonsComponentMap(btnConfig),
|
|
|
|
|
)}
|
|
|
|
|
</SaveButtonContainer>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2022-01-14 06:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default DatasourceAuth;
|