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: Added Steps and centralized Headers #15041

Merged
merged 10 commits into from
Jun 10, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { FormEvent, useEvent } from 'react';
import React, { FormEvent, useState } from 'react';
import { SupersetTheme, JsonObject, t } from '@superset-ui/core';
import { InputProps } from 'antd/lib/input';
import { Switch, Select, Button } from 'src/common/components';
import InfoTooltip from 'src/components/InfoTooltip';
import ValidatedInput from 'src/components/Form/LabeledErrorBoundInput';
import { DeleteFilled } from '@ant-design/icons';
import { SelectValue } from 'src/filters/components/Select/types';
import {
formScrollableStyles,
validatedFormStyles,
CredentialInfoForm,
StyledFormHeader,
toggleStyle,
infoTooltip,
} from './styles';
Expand All @@ -49,8 +49,12 @@ export const FormFieldOrder = [
'credentials_info',
];

const selectedFile = document.getElementById('selectedFile');

interface FieldPropTypes {
required: boolean;
onParametersChange: (value: any) => string;
onParametersUploadFileChange: (value: any) => string;
changeMethods: { onParametersChange: (value: any) => string } & {
onChange: (value: any) => string;
} & { onParametersUploadFileChange: (value: any) => string };
Expand All @@ -59,25 +63,26 @@ interface FieldPropTypes {
db?: DatabaseObject;
isEditMode?: boolean;
sslForced?: boolean;
uploadOption?: string | null;
setUploadOption: (obj: any) => void;
fileToUpload?: string;
setFileToUpload: (obj: any) => void;
AAfghahi marked this conversation as resolved.
Show resolved Hide resolved
}

const credentialsInfo = ({
required,
changeMethods,
getValidation,
validationErrors,
}: FieldPropTypes) => {
const [uploadOption, setUploadOption] = useState<string>('upload');
const [fileToUpload, setFileToUpload] = useState<string>(null);
const CredentialsInfo = ({ changeMethods }: FieldPropTypes) => {
const [uploadOption, setUploadOption] = useState<SelectValue | number>(0);
const [fileToUpload, setFileToUpload] = useState<string | null | undefined>(
null,
);
return (
<CredentialInfoForm>
<label className="label-select">
<span className="label-select">
How do you want to enter service account credentials?
</label>
</span>
<Select
defaultValue={CredentialInfoOptions.jsonUpload}
style={{ width: '100%' }}
onChange={setUploadOption}
onChange={option => setUploadOption(option)}
>
<Select.Option value={CredentialInfoOptions.jsonUpload}>
Upload JSON file
Expand All @@ -86,7 +91,7 @@ const credentialsInfo = ({
Copy and Paste JSON credentials
</Select.Option>
</Select>
{uploadOption === 'paste' ? (
{uploadOption === CredentialInfoOptions.copyPaste ? (
<div className="input-container" onChange={changeMethods.onChange}>
<span className="label-select">Service Account</span>
<textarea className="input-form" name="encrypted_extra" />
Expand All @@ -100,7 +105,7 @@ const credentialsInfo = ({
{!fileToUpload && (
<Button
className="input-upload-btn"
onClick={() => document.getElementById('selectedFile').click()}
onClick={() => document?.getElementById('selectedFile').click()}
>
Choose File
</Button>
Expand All @@ -127,17 +132,20 @@ const credentialsInfo = ({
className="input-upload"
type="file"
onChange={async event => {
const file = event?.target?.files[0];
setFileToUpload(file.name);
let file;
if (event.target.files) {
file = event.target.files[0];
}
AAfghahi marked this conversation as resolved.
Show resolved Hide resolved
setFileToUpload(file?.name);
changeMethods.onParametersChange({
target: {
type: null,
name: 'encrypted_extra',
value: await file.text(),
value: await file?.text(),
checked: false,
},
});
document.getElementById('selectedFile').value = null;
(selectedFile as HTMLInputElement).value = null as any;
}}
/>
</div>
Expand Down Expand Up @@ -303,11 +311,11 @@ const FORM_FIELD_MAP = {
password: passwordField,
database_name: displayField,
encryption: forceSSLField,
credentials_info: credentialsInfo,
credentials_info: CredentialsInfo,
};

const DatabaseConnectionForm = ({
dbModel: { name, parameters },
dbModel: { parameters },
onParametersChange,
onChange,
onParametersUploadFileChange,
Expand All @@ -327,21 +335,13 @@ const DatabaseConnectionForm = ({
onChange: (
event: FormEvent<InputProps> | { target: HTMLInputElement },
) => void;
onParametersUploadFileChange: (
onParametersUploadFileChange?: (
event: FormEvent<InputProps> | { target: HTMLInputElement },
) => void;
validationErrors: JsonObject | null;
getValidation: () => void;
}) => (
<>
{!isEditMode && (
<StyledFormHeader>
<h4>Enter the required {name} credentials</h4>
<p className="helper">
Need help? Learn more about connecting to {name}.
</p>
</StyledFormHeader>
)}
<div
// @ts-ignore
css={(theme: SupersetTheme) => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React from 'react';
import {
EditHeaderTitle,
EditHeaderSubtitle,
CreateHeaderTitle,
CreateHeaderSubtitle,
StyledFormHeader,
} from './styles';
import { DatabaseForm, DatabaseObject } from '../types';

export const DOCUMENTATION_LINK =
'https://superset.apache.org/docs/databases/installing-database-drivers';

const ModalHeader = ({
isLoading,
isEditMode,
useSqlAlchemyForm,
hasConnectedDb,
db,
dbName,
dbModel,
}: {
isLoading: boolean;
isEditMode: boolean;
useSqlAlchemyForm: boolean;
hasConnectedDb: boolean;
db: Partial<DatabaseObject> | null;
dbName: string;
dbModel: DatabaseForm;
}) => {
const isEditHeader = (
<>
<EditHeaderTitle>{db?.backend}</EditHeaderTitle>
<EditHeaderSubtitle>{dbName}</EditHeaderSubtitle>
</>
);
const useSqlAlchemyFormHeader = (
<>
<p className="helper"> Step 2 of 2 </p>
<CreateHeaderTitle>Enter Primary Credentials</CreateHeaderTitle>
<CreateHeaderSubtitle>
Need help? Learn how to connect your database{' '}
<a href={DOCUMENTATION_LINK} target="_blank" rel="noopener noreferrer">
here
</a>
.
</CreateHeaderSubtitle>
</>
);
const hasConnectedDbHeader = (
<StyledFormHeader>
<p className="helper"> Step 3 of 3 </p>
AAfghahi marked this conversation as resolved.
Show resolved Hide resolved
</StyledFormHeader>
);
const hasDbHeader = (
<StyledFormHeader>
<p className="helper"> Step 2 of 3 </p>
<h4>Enter the required {dbModel.name} credentials</h4>
<p className="helper">
Need help? Learn more about connecting to {dbModel.name}.
</p>
</StyledFormHeader>
);
const noDbHeader = (
<StyledFormHeader>
<div className="select-db">
<p className="helper"> Step 1 of 3 </p>
<h4>Select a database to connect</h4>
</div>
</StyledFormHeader>
);

if (isLoading) return <></>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: prefer a switch statement here

if (isEditMode) {
return isEditHeader;
}
if (useSqlAlchemyForm) {
return useSqlAlchemyFormHeader;
}
if (hasConnectedDb) {
return hasConnectedDbHeader;
}
if (db) {
return hasDbHeader;
}
return noDbHeader;
};

export default ModalHeader;
Loading