Skip to content

Commit

Permalink
PoC for passing state variable to child form
Browse files Browse the repository at this point in the history
Signed-off-by: Bandini Bhopi <bandinib@amazon.com>
  • Loading branch information
bandinib-amzn committed Feb 29, 2024
1 parent 03e4897 commit 70d584e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EuiSuperSelectOption } from '@elastic/eui';
export interface AuthenticationMethod {
name: string;
credentialSourceOption: EuiSuperSelectOption<string>;
credentialForm?: React.JSX.Element;
credentialForm?: (state?: any, setState?: any) => React.JSX.Element;
crendentialFormField?: { [key: string]: string };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@osd/i18n';
import { FormattedMessage } from '@osd/i18n/react';
import { AuthenticationMethodRegistery } from 'src/plugins/data_source_management/public/auth_registry';
import { SigV4Content, SigV4ServiceName } from '../../../../../../data_source/common/data_sources';
import {
AuthType,
Expand Down Expand Up @@ -55,7 +56,11 @@ export interface CreateDataSourceState {
endpoint: string;
auth: {
type: AuthType;
credentials: UsernamePasswordTypedContent | SigV4Content | undefined;
credentials:
| UsernamePasswordTypedContent
| SigV4Content
| undefined
| { [key: string]: string };
};
}

Expand All @@ -68,16 +73,17 @@ export class CreateDataSourceForm extends React.Component<

authOptions: Array<EuiSuperSelectOption<string>> = [];
isNoAuthOptionEnabled: boolean;
authenticationMethodRegistery: AuthenticationMethodRegistery;

constructor(props: CreateDataSourceProps, context: DataSourceManagementContextValue) {
super(props, context);

const authenticationMethodRegistery = context.services.authenticationMethodRegistery;
const registeredAuthMethods = authenticationMethodRegistery.getAllAuthenticationMethods();
const initialSelectedAuthMethod = getDefaultAuthMethod(authenticationMethodRegistery);
this.authenticationMethodRegistery = context.services.authenticationMethodRegistery;
const registeredAuthMethods = this.authenticationMethodRegistery.getAllAuthenticationMethods();
const initialSelectedAuthMethod = getDefaultAuthMethod(this.authenticationMethodRegistery);

this.isNoAuthOptionEnabled =
authenticationMethodRegistery.getAuthenticationMethod(AuthType.NoAuth) !== undefined;
this.authenticationMethodRegistery.getAuthenticationMethod(AuthType.NoAuth) !== undefined;

this.authOptions = registeredAuthMethods.map((authMethod) => {
return authMethod.credentialSourceOption;
Expand Down Expand Up @@ -152,6 +158,20 @@ export class CreateDataSourceForm extends React.Component<
});
};

// Function to update state
handleStateChange = (newState: any) => {
this.setState(newState);
};

getAuthUIFromRegistry = (authType: string) => {
const authMethodUI = this.authenticationMethodRegistery.getAuthenticationMethod(authType);
const form = authMethodUI?.credentialForm;
if (form !== undefined) {
return form(this.state, this.handleStateChange);
}
return null;
};

onChangeSigV4ServiceName = (service: SigV4ServiceName) => {
this.setState({
auth: {
Expand Down Expand Up @@ -296,6 +316,7 @@ export class CreateDataSourceForm extends React.Component<

getFormValues = (): DataSourceAttributes => {
let credentials = this.state.auth.credentials;
console.log(`Inside getFormValues credentials = ${JSON.stringify(credentials)}`);
if (this.state.auth.type === AuthType.UsernamePasswordType) {
credentials = {
username: this.state.auth.credentials.username,
Expand Down Expand Up @@ -631,6 +652,7 @@ export class CreateDataSourceForm extends React.Component<
/>
</EuiFormRow>

{this.getAuthUIFromRegistry(this.state.auth.type)}
{/* Create New credentials */}
{this.state.auth.type === AuthType.UsernamePasswordType
? this.renderCreateNewCredentialsForm(this.state.auth.type)
Expand Down

0 comments on commit 70d584e

Please sign in to comment.