-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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: validation db modal #14832
Merged
hughhhh
merged 3 commits into
apache:master
from
preset-io:elizabeth/validation-db-modal
Jun 1, 2021
Merged
feat: validation db modal #14832
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -17,11 +17,14 @@ | |
* under the License. | ||
*/ | ||
import React, { FormEvent } from 'react'; | ||
import cx from 'classnames'; | ||
import { SupersetTheme, JsonObject } from '@superset-ui/core'; | ||
import { InputProps } from 'antd/lib/input'; | ||
import { FormLabel, FormItem } from 'src/components/Form'; | ||
import { Input } from 'src/common/components'; | ||
import { StyledFormHeader, formScrollableStyles } from './styles'; | ||
import ValidatedInput from 'src/components/Form/LabeledErrorBoundInput'; | ||
import { | ||
StyledFormHeader, | ||
formScrollableStyles, | ||
validatedFormStyles, | ||
} from './styles'; | ||
import { DatabaseForm } from '../types'; | ||
|
||
export const FormFieldOrder = [ | ||
|
@@ -33,64 +36,137 @@ export const FormFieldOrder = [ | |
'database_name', | ||
]; | ||
|
||
const CHANGE_METHOD = { | ||
onChange: 'onChange', | ||
onPropertiesChange: 'onPropertiesChange', | ||
}; | ||
interface FieldPropTypes { | ||
required: boolean; | ||
changeMethods: { onParametersChange: (value: any) => string } & { | ||
onChange: (value: any) => string; | ||
}; | ||
validationErrors: JsonObject | null; | ||
getValidation: () => void; | ||
} | ||
|
||
const hostField = ({ | ||
required, | ||
changeMethods, | ||
getValidation, | ||
validationErrors, | ||
}: FieldPropTypes) => ( | ||
<ValidatedInput | ||
id="host" | ||
name="host" | ||
required={required} | ||
validationMethods={{ onBlur: getValidation }} | ||
errorMessage={validationErrors?.host} | ||
placeholder="e.g. 127.0.0.1" | ||
className="form-group-w-50" | ||
label="Host" | ||
onChange={changeMethods.onParametersChange} | ||
/> | ||
); | ||
const portField = ({ | ||
required, | ||
changeMethods, | ||
getValidation, | ||
validationErrors, | ||
}: FieldPropTypes) => ( | ||
<ValidatedInput | ||
id="port" | ||
name="port" | ||
required={required} | ||
validationMethods={{ onBlur: getValidation }} | ||
errorMessage={validationErrors?.port} | ||
placeholder="e.g. 5432" | ||
className="form-group-w-50" | ||
label="Port" | ||
onChange={changeMethods.onParametersChange} | ||
/> | ||
); | ||
const databaseField = ({ | ||
required, | ||
changeMethods, | ||
getValidation, | ||
validationErrors, | ||
}: FieldPropTypes) => ( | ||
<ValidatedInput | ||
id="database" | ||
name="database" | ||
required={required} | ||
validationMethods={{ onBlur: getValidation }} | ||
errorMessage={validationErrors?.database} | ||
placeholder="e.g. world_population" | ||
label="Database name" | ||
onChange={changeMethods.onParametersChange} | ||
helpText="Copy the name of the PostgreSQL database you are trying to connect to." | ||
/> | ||
); | ||
const usernameField = ({ | ||
required, | ||
changeMethods, | ||
getValidation, | ||
validationErrors, | ||
}: FieldPropTypes) => ( | ||
<ValidatedInput | ||
id="username" | ||
name="username" | ||
required={required} | ||
validationMethods={{ onBlur: getValidation }} | ||
errorMessage={validationErrors?.username} | ||
placeholder="e.g. Analytics" | ||
label="Username" | ||
onChange={changeMethods.onParametersChange} | ||
/> | ||
); | ||
const passwordField = ({ | ||
required, | ||
changeMethods, | ||
getValidation, | ||
validationErrors, | ||
}: FieldPropTypes) => ( | ||
<ValidatedInput | ||
id="password" | ||
name="password" | ||
required={required} | ||
validationMethods={{ onBlur: getValidation }} | ||
errorMessage={validationErrors?.password} | ||
placeholder="e.g. ********" | ||
label="Password" | ||
onChange={changeMethods.onParametersChange} | ||
/> | ||
); | ||
const displayField = ({ | ||
required, | ||
changeMethods, | ||
getValidation, | ||
validationErrors, | ||
}: FieldPropTypes) => ( | ||
<ValidatedInput | ||
id="database_name" | ||
name="database_name" | ||
required={required} | ||
validationMethods={{ onBlur: getValidation }} | ||
errorMessage={validationErrors?.database_name} | ||
placeholder="" | ||
label="Display Name" | ||
onChange={changeMethods.onChange} | ||
helpText="Pick a nickname for this database to display as in Superset." | ||
/> | ||
); | ||
|
||
const FORM_FIELD_MAP = { | ||
host: { | ||
description: 'Host', | ||
type: 'text', | ||
className: 'w-50', | ||
placeholder: 'e.g. 127.0.0.1', | ||
changeMethod: CHANGE_METHOD.onPropertiesChange, | ||
}, | ||
port: { | ||
description: 'Port', | ||
type: 'text', | ||
className: 'w-50', | ||
placeholder: 'e.g. 5432', | ||
changeMethod: CHANGE_METHOD.onPropertiesChange, | ||
}, | ||
database: { | ||
description: 'Database name', | ||
type: 'text', | ||
label: | ||
'Copy the name of the PostgreSQL database you are trying to connect to.', | ||
placeholder: 'e.g. world_population', | ||
changeMethod: CHANGE_METHOD.onPropertiesChange, | ||
}, | ||
username: { | ||
description: 'Username', | ||
type: 'text', | ||
placeholder: 'e.g. Analytics', | ||
changeMethod: CHANGE_METHOD.onPropertiesChange, | ||
}, | ||
password: { | ||
description: 'Password', | ||
type: 'text', | ||
placeholder: 'e.g. ********', | ||
changeMethod: CHANGE_METHOD.onPropertiesChange, | ||
}, | ||
database_name: { | ||
description: 'Display Name', | ||
type: 'text', | ||
label: 'Pick a nickname for this database to display as in Superset.', | ||
changeMethod: CHANGE_METHOD.onChange, | ||
}, | ||
query: { | ||
additionalProperties: {}, | ||
description: 'Additional parameters', | ||
type: 'object', | ||
changeMethod: CHANGE_METHOD.onPropertiesChange, | ||
}, | ||
host: hostField, | ||
port: portField, | ||
database: databaseField, | ||
username: usernameField, | ||
password: passwordField, | ||
database_name: displayField, | ||
}; | ||
|
||
const DatabaseConnectionForm = ({ | ||
dbModel: { name, parameters }, | ||
onParametersChange, | ||
onChange, | ||
validationErrors, | ||
getValidation, | ||
}: { | ||
dbModel: DatabaseForm; | ||
onParametersChange: ( | ||
|
@@ -99,6 +175,8 @@ const DatabaseConnectionForm = ({ | |
onChange: ( | ||
event: FormEvent<InputProps> | { target: HTMLInputElement }, | ||
) => void; | ||
validationErrors: JsonObject | null; | ||
getValidation: () => void; | ||
}) => ( | ||
<> | ||
<StyledFormHeader> | ||
|
@@ -107,52 +185,30 @@ const DatabaseConnectionForm = ({ | |
Need help? Learn more about connecting to {name}. | ||
</p> | ||
</StyledFormHeader> | ||
<div css={formScrollableStyles}> | ||
<div | ||
// @ts-ignore | ||
css={(theme: SupersetTheme) => [ | ||
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. we have to look into why this isn't using |
||
formScrollableStyles, | ||
validatedFormStyles(theme), | ||
]} | ||
> | ||
{parameters && | ||
FormFieldOrder.filter( | ||
(key: string) => | ||
Object.keys(parameters.properties).includes(key) || | ||
key === 'database_name', | ||
).map(field => { | ||
const { | ||
className, | ||
description, | ||
type, | ||
placeholder, | ||
label, | ||
changeMethod, | ||
} = FORM_FIELD_MAP[field]; | ||
const onEdit = | ||
changeMethod === CHANGE_METHOD.onChange | ||
? onChange | ||
: onParametersChange; | ||
return ( | ||
<FormItem | ||
className={cx(className, `form-group-${className}`)} | ||
key={field} | ||
> | ||
<FormLabel | ||
htmlFor={field} | ||
required={parameters.required.includes(field)} | ||
> | ||
{description} | ||
</FormLabel> | ||
<Input | ||
name={field} | ||
type={type} | ||
id={field} | ||
autoComplete="off" | ||
placeholder={placeholder} | ||
onChange={onEdit} | ||
/> | ||
<p className="helper">{label}</p> | ||
</FormItem> | ||
); | ||
})} | ||
).map(field => | ||
FORM_FIELD_MAP[field]({ | ||
required: parameters.required.includes(field), | ||
changeMethods: { onParametersChange, onChange }, | ||
validationErrors, | ||
getValidation, | ||
key: field, | ||
}), | ||
)} | ||
</div> | ||
</> | ||
); | ||
|
||
export const FormFieldMap = FORM_FIELD_MAP; | ||
|
||
export default DatabaseConnectionForm; |
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.
nit: can we move this to a separate files in the next iteration
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.
yes for sure