-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CM layout and validate input fields
Signed-off-by: Louis Chu <[email protected]>
- Loading branch information
Showing
25 changed files
with
526 additions
and
243 deletions.
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
261 changes: 261 additions & 0 deletions
261
...ent/public/components/common/components/create_credential_form/create_credential_form.tsx
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 |
---|---|---|
@@ -0,0 +1,261 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { | ||
EuiForm, | ||
EuiFormRow, | ||
EuiFieldText, | ||
EuiButton, | ||
EuiFieldPassword, | ||
EuiPageContent, | ||
EuiHorizontalRule, | ||
EuiSpacer, | ||
EuiText, | ||
} from '@elastic/eui'; | ||
import { FormattedMessage } from '@osd/i18n/react'; | ||
import { DocLinksStart } from 'src/core/public'; | ||
import { context as contextType } from '../../../../../../opensearch_dashboards_react/public'; | ||
|
||
import { CredentialManagmentContextValue } from '../../../../types'; | ||
import { CreateCredentialItem } from '../../../types'; | ||
import { Header } from '../header'; | ||
|
||
export interface CredentialFormProps { | ||
docLinks: DocLinksStart; | ||
handleSubmit: (formValues: CreateCredentialItem) => void; | ||
} | ||
|
||
export interface CredentialFormState { | ||
formErrors: string[]; | ||
formErrorsByField: CredentialFormValidation; | ||
title: string; | ||
description: string; | ||
username: string; | ||
password: string; | ||
dual: boolean; | ||
} | ||
|
||
interface CredentialFormValidation { | ||
title: string[]; | ||
description: string[]; | ||
username: string[]; | ||
password: string[]; | ||
} | ||
|
||
const defaultValidation: CredentialFormValidation = { | ||
title: [], | ||
description: [], | ||
username: [], | ||
password: [], | ||
}; | ||
|
||
export class CredentialForm extends React.Component<CredentialFormProps, CredentialFormState> { | ||
static contextType = contextType; | ||
public readonly context!: CredentialManagmentContextValue; | ||
|
||
constructor(props: CredentialFormProps, context: CredentialManagmentContextValue) { | ||
super(props, context); | ||
|
||
this.state = { | ||
formErrors: [], | ||
formErrorsByField: { ...defaultValidation }, | ||
title: '', | ||
description: '', | ||
username: '', | ||
password: '', | ||
dual: true, | ||
}; | ||
} | ||
|
||
/* Validations */ | ||
|
||
isFormValid = () => { | ||
const validationByField: CredentialFormValidation = { | ||
title: [], | ||
description: [], | ||
username: [], | ||
password: [], | ||
}; | ||
const formErrorMessages: string[] = []; | ||
/* Title validation */ | ||
if (!this.state.title) { | ||
validationByField.title.push('Title should not be empty'); | ||
formErrorMessages.push('Title should not be empty'); | ||
} | ||
/* Username Validation */ | ||
if (!this.state.username) { | ||
validationByField.username.push('Username should not be empty'); | ||
formErrorMessages.push('Username should not be empty'); | ||
} | ||
/* Password Validation */ | ||
if (!this.state.password) { | ||
validationByField.password.push('Password should not be empty'); | ||
formErrorMessages.push('Password should not be empty'); | ||
} | ||
|
||
this.setState({ | ||
formErrors: formErrorMessages, | ||
formErrorsByField: { ...validationByField }, | ||
}); | ||
return formErrorMessages.length === 0; | ||
}; | ||
|
||
/* Events */ | ||
|
||
onChangeTitle = (e: { target: { value: any } }) => { | ||
this.setState({ title: e.target.value }, () => { | ||
if (this.state.formErrorsByField.title.length) { | ||
this.isFormValid(); | ||
} | ||
}); | ||
}; | ||
|
||
onChangeDescription = (e: { target: { value: any } }) => { | ||
this.setState({ description: e.target.value }, () => { | ||
if (this.state.formErrorsByField.description.length) { | ||
this.isFormValid(); | ||
} | ||
}); | ||
}; | ||
|
||
onChangeUsername = (e: { target: { value: any } }) => { | ||
this.setState({ username: e.target.value }, () => { | ||
if (this.state.formErrorsByField.username.length) { | ||
this.isFormValid(); | ||
} | ||
}); | ||
}; | ||
|
||
onChangePassword = (e: { target: { value: any } }) => { | ||
this.setState({ password: e.target.value }, () => { | ||
if (this.state.formErrorsByField.password.length) { | ||
this.isFormValid(); | ||
} | ||
}); | ||
}; | ||
|
||
onClickSubmitForm = () => { | ||
if (this.isFormValid()) { | ||
const formValues: CreateCredentialItem = { | ||
title: this.state.title, | ||
description: this.state.description, | ||
username: this.state.username, | ||
password: this.state.password, | ||
}; | ||
this.props.handleSubmit(formValues); | ||
} | ||
}; | ||
|
||
/* Render methods */ | ||
|
||
renderHeader() { | ||
const { docLinks } = this.props; | ||
return <Header docLinks={docLinks} />; | ||
} | ||
|
||
renderContent = () => { | ||
const header = this.renderHeader(); | ||
|
||
return ( | ||
<EuiPageContent> | ||
{header} | ||
<EuiHorizontalRule /> | ||
<EuiForm | ||
data-test-subj="credential-creation" | ||
isInvalid={!!this.state.formErrors.length} | ||
error={this.state.formErrors} | ||
> | ||
{/* Title */} | ||
<EuiFormRow | ||
label="Title" | ||
isInvalid={!!this.state.formErrorsByField.title.length} | ||
error={this.state.formErrorsByField.title} | ||
helpText="Tip: Title your stored credential with an employee or team name" | ||
> | ||
<EuiFieldText | ||
name="credentialTitle" | ||
value={this.state.title || ''} | ||
placeholder="Text field (placeholder)" | ||
isInvalid={!!this.state.formErrorsByField.title.length} | ||
onChange={this.onChangeTitle} | ||
/> | ||
</EuiFormRow> | ||
|
||
{/* Description */} | ||
<EuiFormRow | ||
label="Description" | ||
isInvalid={!!this.state.formErrorsByField.description.length} | ||
error={this.state.formErrorsByField.description} | ||
helpText="Describe what this credential is used for" | ||
> | ||
<EuiFieldText | ||
name="description" | ||
value={this.state.description || ''} | ||
placeholder="Text field (placeholder)" | ||
isInvalid={!!this.state.formErrorsByField.description.length} | ||
onChange={this.onChangeDescription} | ||
/> | ||
</EuiFormRow> | ||
|
||
{/* Authentication */} | ||
<EuiSpacer size="xl" /> | ||
<EuiText grow={false}> | ||
<h4> | ||
<FormattedMessage | ||
id="createCredentialForm.authenticationHeader" | ||
defaultMessage="Authentication" | ||
/> | ||
</h4> | ||
</EuiText> | ||
<EuiSpacer size="s" /> | ||
|
||
{/* Username */} | ||
<EuiFormRow | ||
label="Username" | ||
isInvalid={!!this.state.formErrorsByField.username.length} | ||
error={this.state.formErrorsByField.username} | ||
> | ||
<EuiFieldText | ||
name="username" | ||
value={this.state.username || ''} | ||
placeholder="Text field (placeholder)" | ||
isInvalid={!!this.state.formErrorsByField.username.length} | ||
onChange={this.onChangeUsername} | ||
/> | ||
</EuiFormRow> | ||
|
||
{/* Password */} | ||
<EuiFormRow | ||
label="Password" | ||
isInvalid={!!this.state.formErrorsByField.password.length} | ||
error={this.state.formErrorsByField.password} | ||
> | ||
<EuiFieldPassword | ||
name="password" | ||
type={this.state.dual ? 'dual' : undefined} | ||
value={this.state.password || ''} | ||
placeholder="Password field (placeholder)" | ||
isInvalid={!!this.state.formErrorsByField.password.length} | ||
onChange={this.onChangePassword} | ||
/> | ||
</EuiFormRow> | ||
|
||
<EuiSpacer size="xl" /> | ||
|
||
{/* Create Credential button*/} | ||
<EuiButton type="submit" fill onClick={this.onClickSubmitForm}> | ||
Create stored credential | ||
</EuiButton> | ||
</EuiForm> | ||
</EuiPageContent> | ||
); | ||
}; | ||
|
||
render() { | ||
return <>{this.renderContent()}</>; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...redential_management/public/components/common/components/create_credential_form/index.tsx
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { CredentialForm } from './create_credential_form'; |
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
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
src/plugins/credential_management/public/components/common/components/index.tsx
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { CredentialForm } from './create_credential_form'; |
7 changes: 7 additions & 0 deletions
7
src/plugins/credential_management/public/components/common/index.tsx
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { CredentialForm } from './components'; | ||
export { LocalizedContent } from './text_content'; |
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
File renamed without changes.
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
Oops, something went wrong.