Skip to content

Commit

Permalink
Improve secret hostname hint
Browse files Browse the repository at this point in the history
  • Loading branch information
jerolimov committed Nov 8, 2020
1 parent eac92a3 commit 17ba507
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 58 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import * as _ from 'lodash';
import { FormikValues, useFormikContext } from 'formik';
import { FormikProps } from 'formik';
import { TextInputTypes } from '@patternfly/react-core';
import { ButtonBar } from '@console/internal/components/utils';
import {
Expand All @@ -10,15 +10,9 @@ import {
CreateConfigSubform,
} from '@console/internal/components/secrets/create-secret';
import { DropdownField, InputField, ActionGroupWithIcons } from '@console/shared';
import SecretAnnotation from './SecretAnnotation';
import { SecretAnnotationId, SecretAnnotationType } from '../../const';
import './SecretForm.scss';

const authTypes = {
[SecretType.dockerconfigjson]: 'Image Registry Credentials',
[SecretType.basicAuth]: 'Basic Authentication',
[SecretType.sshAuth]: 'SSH Key',
};

const renderSecretForm = (
type: SecretType,
stringData: {
Expand Down Expand Up @@ -47,19 +41,64 @@ const renderSecretForm = (
}
};

const SecretForm: React.FC<FormikValues> = ({
interface SecretFormValues {
secretName: string;
type: SecretType;
annotations: {
key: SecretAnnotationId;
value: string; // url
};
formData: any;
}

const SecretForm: React.FC<FormikProps<SecretFormValues>> = ({
values,
setFieldValue,
handleSubmit,
handleReset,
status,
isSubmitting,
}) => {
const { values, setFieldValue } = useFormikContext<FormikValues>();
const [stringData, setStringData] = React.useState({
[SecretType.basicAuth]: {},
[SecretType.sshAuth]: {},
[SecretType.dockerconfigjson]: {},
});

const authTypes = React.useMemo<Record<string, string>>(() => {
switch (values.annotations.key) {
case SecretAnnotationId.Git:
return {
[SecretType.basicAuth]: 'Basic Authentication',
[SecretType.sshAuth]: 'SSH Key',
};
case SecretAnnotationId.Image:
return {
[SecretType.basicAuth]: 'Basic Authentication',
[SecretType.dockerconfigjson]: 'Image Registry Credentials',
};
default:
return {};
}
}, [values.annotations.key]);

React.useEffect(() => {
const availableAuthTypes = Object.keys(authTypes);
if (!availableAuthTypes.includes(values.type)) {
setFieldValue('type', SecretType.basicAuth);
}
}, [authTypes, values.type, setFieldValue]);

// Uses a memo instead of const outside of the function so that we can add i18n right here
const helpText = React.useMemo(
() => ({
[SecretType.dockerconfigjson]: 'The base server url (e.g. https://quay.io/)',
[SecretType.basicAuth]: 'The base server url (e.g. https://github.com)',
[SecretType.sshAuth]: 'Server hostname without schema or path (e.g. github.com)',
}),
[],
);

const setValues = (type: SecretType) => {
if (type === SecretType.dockerconfigjson) {
setFieldValue(
Expand Down Expand Up @@ -89,7 +128,13 @@ const SecretForm: React.FC<FormikValues> = ({
/>
</div>
<div className="form-group">
<SecretAnnotation fieldName="annotations" />
<DropdownField
name="annotations.key"
items={SecretAnnotationType}
label="Access to"
fullWidth
required
/>
</div>
<div className="form-group">
<DropdownField
Expand All @@ -102,6 +147,15 @@ const SecretForm: React.FC<FormikValues> = ({
required
/>
</div>
<div className="form-group">
<InputField
name="annotations.value"
helpText={helpText[values.type]}
type={TextInputTypes.text}
label="Server URL"
required
/>
</div>
{renderSecretForm(values.type, stringData, onDataChanged)}
<ButtonBar errorMessage={status?.submitError} inProgress={isSubmitting}>
<ActionGroupWithIcons onSubmit={handleSubmit} onClose={handleReset} />
Expand Down

0 comments on commit 17ba507

Please sign in to comment.