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

feat: add authentication tab in api editor #8245

Merged
merged 17 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/client/src/assets/icons/ads/shield-error.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/client/src/assets/icons/ads/shield-success.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ export const EditorWrapper = styled.div<{
border-radius: 2px;
margin-right: 2px;
}
.datasource-highlight-error {
background: #FFF0F0;
border: 1px solid #F22B2B;
}
.datasource-highlight-success {
background: #E3FFF3;
border: 1px solid #03B365;
}
.CodeMirror {
flex: 1;
line-height: 21px;
Expand Down Expand Up @@ -215,7 +223,7 @@ export const EditorWrapper = styled.div<{
border-right: 1px dotted #ccc;
}
`}

.bp3-popover-target {
padding-right: 10px;
padding-top: 5px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import history from "utils/history";
import { getDatasourceInfo } from "pages/Editor/APIEditor/ApiRightPane";
import * as FontFamilies from "constants/Fonts";
import { getQueryParams } from "../../../../utils/AppsmithUtils";
import { AuthType } from "entities/Datasource/RestAPIForm";

type ReduxStateProps = {
orgId: string;
Expand Down Expand Up @@ -199,6 +200,22 @@ class EmbeddedDatasourcePathComponent extends React.Component<Props> {

handleDatasourceHighlight = () => {
const { datasource } = this.props;
const authType = get(
datasource,
"datasourceConfiguration.authentication.authenticationType",
"",
);

const hasError = !!get(datasource, "structure.error");
hetunandu marked this conversation as resolved.
Show resolved Hide resolved

let className = "datasource-highlight";

if (authType === AuthType.OAuth2) {
className = `${className} ${
hasError ? "datasource-highlight-error" : "datasource-highlight-success"
}`;
}

return (editorInstance: CodeMirror.Doc) => {
if (
editorInstance.lineCount() === 1 &&
Expand All @@ -211,7 +228,7 @@ class EmbeddedDatasourcePathComponent extends React.Component<Props> {
{ ch: 0, line: 0 },
{ ch: end, line: 0 },
{
className: "datasource-highlight",
className,
atomic: true,
inclusiveRight: false,
},
Expand Down
140 changes: 140 additions & 0 deletions app/client/src/pages/Editor/APIEditor/ApiAuthentication.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import React from "react";
import { Datasource, EmbeddedRestDatasource } from "entities/Datasource";
import { get, merge } from "lodash";
import styled from "styled-components";
import { connect, useDispatch } from "react-redux";
import Button, { Category, Size } from "components/ads/Button";
import { storeAsDatasource } from "actions/datasourceActions";
import history from "utils/history";
import { DATA_SOURCES_EDITOR_ID_URL } from "constants/routes";
import { getQueryParams } from "utils/AppsmithUtils";
import Text, { TextType } from "components/ads/Text";
import { AuthType } from "entities/Datasource/RestAPIForm";
import { API_EDITOR_FORM_NAME } from "constants/forms";
import { formValueSelector } from "redux-form";
import { AppState } from "reducers";
import { ReactComponent as SheildSuccess } from "assets/icons/ads/shield-success.svg";
import { ReactComponent as SheildError } from "assets/icons/ads/shield-error.svg";

interface ApiAuthenticationProps {
datasource: EmbeddedRestDatasource | Datasource;
applicationId?: string;
currentPageId?: string;
}

const AuthContainer = styled.div`
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
`;

const OAuthContainer = styled.div`
background: #ffffff;
display: flex;
flex-direction: row;
padding: 12px 5px;
`;

interface ErrorProps {
hasError: boolean;
}

const OAuthText = styled.span<ErrorProps>`
color: ${(props) => (props.hasError ? "#F22B2B" : "#03B365")};
`;

const DescriptionText = styled(Text)`
margin: 12px auto;
`;

const apiFormValueSelector = formValueSelector(API_EDITOR_FORM_NAME);

function OAuthLabel(props: ErrorProps) {
return (
<OAuthContainer>
{props.hasError ? <SheildError /> : <SheildSuccess />}
<OAuthText hasError={props.hasError}>
{props.hasError ? "OAuth Error" : "OAuth 2.0"}
</OAuthText>
</OAuthContainer>
);
}

function ApiAuthentication(props: ApiAuthenticationProps): JSX.Element {
const dispatch = useDispatch();
const { applicationId, currentPageId, datasource } = props;
const authType = get(
datasource,
"datasourceConfiguration.authentication.authenticationType",
"",
);

const hasError = !!get(datasource, "structure.error");
hetunandu marked this conversation as resolved.
Show resolved Hide resolved

const shouldSave = datasource && !("id" in datasource);

const onClick = () => {
if (shouldSave) {
dispatch(storeAsDatasource());
} else {
history.push(
DATA_SOURCES_EDITOR_ID_URL(
applicationId,
currentPageId,
get(datasource, "id"),
getQueryParams(),
),
);
}
};

return (
<AuthContainer>
{authType === AuthType.OAuth2 && <OAuthLabel hasError={hasError} />}
<DescriptionText type={TextType.P1}>
{shouldSave
? "Save API as datasource to setup authentication"
: "Edit Datasource to access authentication settings"}
</DescriptionText>
<Button
category={Category.tertiary}
onClick={onClick}
size={Size.medium}
tag="button"
text={shouldSave ? "Save Datasource" : "Edit Datasource"}
mohanarpit marked this conversation as resolved.
Show resolved Hide resolved
/>
</AuthContainer>
);
}

const mapStateToProps = (state: AppState): ApiAuthenticationProps => {
const datasourceFromAction = apiFormValueSelector(state, "datasource");
let datasourceMerged = datasourceFromAction;
if (datasourceFromAction && "id" in datasourceFromAction) {
const datasourceFromDataSourceList = state.entities.datasources.list.find(
(d) => d.id === datasourceFromAction.id,
);
if (datasourceFromDataSourceList) {
datasourceMerged = merge(
{},
datasourceFromAction,
datasourceFromDataSourceList,
);
}
}

return {
datasource: datasourceMerged,
currentPageId: state.entities.pageList.currentPageId,
applicationId: state.entities.pageList.applicationId,
};
};

const ApiAuthenticationConnectedComponent = connect(mapStateToProps)(
ApiAuthentication,
);

export default ApiAuthenticationConnectedComponent;
6 changes: 6 additions & 0 deletions app/client/src/pages/Editor/APIEditor/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { getActionResponses } from "../../../selectors/entitiesSelector";
import { isEmpty } from "lodash";
import SearchSnippets from "components/ads/SnippetButton";
import { ENTITY_TYPE } from "entities/DataTree/dataTreeFactory";
import ApiAuthentication from "./ApiAuthentication";

const Form = styled.form`
display: flex;
Expand Down Expand Up @@ -592,6 +593,11 @@ function ApiEditorForm(props: Props) {
/>
),
},
{
key: "authentication",
title: "Authentication",
panelComponent: <ApiAuthentication />,
},
{
key: "settings",
title: "Settings",
Expand Down