Skip to content

Commit

Permalink
Add connection modal
Browse files Browse the repository at this point in the history
  • Loading branch information
emilys314 committed Sep 18, 2024
1 parent b5351a7 commit 71d923a
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from 'react';
import {
Button,
DescriptionList,
DescriptionListDescription,
DescriptionListGroup,
DescriptionListTerm,
HelperText,
HelperTextItem,
LabelGroup,
Popover,
} from '@patternfly/react-core';
import { getDescriptionFromK8sResource } from '~/concepts/k8s/utils';
import { ConnectionTypeConfigMapObj } from './types';
import UnspecifiedValue from './fields/UnspecifiedValue';
import CategoryLabel from './CategoryLabel';

type Props = {
connectionType?: ConnectionTypeConfigMapObj;
};

export const ConnectionTypeDetailsHelperText: React.FC<Props> = ({ connectionType }) => {
const displayName =
connectionType && connectionType.metadata.annotations?.['openshift.io/display-name'];
const description = connectionType && getDescriptionFromK8sResource(connectionType);

Check warning on line 25 in frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx#L24-L25

Added lines #L24 - L25 were not covered by tests

return (

Check warning on line 27 in frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx#L27

Added line #L27 was not covered by tests
<HelperText>
<HelperTextItem>
<Popover
headerContent="Connection type details"
bodyContent={
<DescriptionList>
<DescriptionListGroup>
<DescriptionListTerm>Connection type name</DescriptionListTerm>
<DescriptionListDescription>
{displayName || <UnspecifiedValue />}

Check warning on line 37 in frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx#L37

Added line #L37 was not covered by tests
</DescriptionListDescription>
</DescriptionListGroup>
{description ? (
<DescriptionListGroup>

Check warning on line 41 in frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx#L41

Added line #L41 was not covered by tests
<DescriptionListTerm>Connection type description</DescriptionListTerm>
<DescriptionListDescription>
{description || <UnspecifiedValue />}

Check warning on line 44 in frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx#L44

Added line #L44 was not covered by tests
</DescriptionListDescription>
</DescriptionListGroup>
) : undefined}

Check warning on line 47 in frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx#L47

Added line #L47 was not covered by tests
<DescriptionListGroup>
<DescriptionListTerm>Category</DescriptionListTerm>
<DescriptionListDescription>
{connectionType?.data?.category?.length ? (
<LabelGroup>
{connectionType.data.category.map((category) => (
<CategoryLabel key={category} category={category} />

Check warning on line 54 in frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx#L51-L54

Added lines #L51 - L54 were not covered by tests
))}
</LabelGroup>
) : (
<UnspecifiedValue />

Check warning on line 58 in frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypeDetailsHelperText.tsx#L58

Added line #L58 was not covered by tests
)}
</DescriptionListDescription>
</DescriptionListGroup>
</DescriptionList>
}
>
<Button variant="link" isInline>
View connection type details
</Button>
</Popover>
</HelperTextItem>
</HelperText>
);
};
205 changes: 111 additions & 94 deletions frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx
Original file line number Diff line number Diff line change
@@ -1,111 +1,128 @@
import * as React from 'react';
import {
Button,
DescriptionList,
DescriptionListDescription,
DescriptionListGroup,
DescriptionListTerm,
Form,
FormGroup,
FormSection,
HelperText,
HelperTextItem,
LabelGroup,
MenuToggleStatus,
Popover,
Title,
} from '@patternfly/react-core';
import { Form, FormGroup, FormSection, MenuToggleStatus, Title } from '@patternfly/react-core';
import ConnectionTypeFormFields from '~/concepts/connectionTypes/fields/ConnectionTypeFormFields';
import { ConnectionTypeConfigMapObj } from '~/concepts/connectionTypes/types';
import NameDescriptionField from '~/concepts/k8s/NameDescriptionField';
import { getDescriptionFromK8sResource } from '~/concepts/k8s/utils';
import UnspecifiedValue from '~/concepts/connectionTypes/fields/UnspecifiedValue';
import SimpleSelect from '~/components/SimpleSelect';
import CategoryLabel from '~/concepts/connectionTypes/CategoryLabel';
import TruncatedText from '~/components/TruncatedText';
import {
ConnectionTypeConfigMapObj,
ConnectionTypeValueType,
} from '~/concepts/connectionTypes/types';
import {
getDisplayNameFromK8sResource,
getResourceNameFromK8sResource,
} from '~/concepts/k8s/utils';
import TypeaheadSelect, { TypeaheadSelectOption } from '~/components/TypeaheadSelect';
import K8sNameDescriptionField from '~/concepts/k8s/K8sNameDescriptionField/K8sNameDescriptionField';
import {
K8sNameDescriptionFieldData,
K8sNameDescriptionFieldUpdateFunction,
} from '~/concepts/k8s/K8sNameDescriptionField/types';
import { ConnectionTypeDetailsHelperText } from './ConnectionTypeDetailsHelperText';

type Props = {
obj?: ConnectionTypeConfigMapObj;
setObj?: (obj?: ConnectionTypeConfigMapObj) => void;
connectionTypes?: ConnectionTypeConfigMapObj[];
isPreview?: boolean;
connectionNameDesc?: K8sNameDescriptionFieldData;
setConnectionNameDesc?: K8sNameDescriptionFieldUpdateFunction;
connectionValues?: {
[key: string]: ConnectionTypeValueType;
};
setConnectionValues?: (values: { [key: string]: ConnectionTypeValueType }) => void;
};

// TODO consider refactoring this form for reuse when creating connection type instances
const ConnectionTypePreview: React.FC<Props> = ({ obj }) => {
const ConnectionTypePreview: React.FC<Props> = ({
obj,
setObj,
connectionTypes,
isPreview,
connectionNameDesc,
setConnectionNameDesc,
connectionValues,
setConnectionValues,
}) => {

Check warning on line 42 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L34-L42

Added lines #L34 - L42 were not covered by tests
const connectionTypeName = obj && obj.metadata.annotations?.['openshift.io/display-name'];
const connectionTypeDescription = (obj && getDescriptionFromK8sResource(obj)) ?? undefined;

const options: TypeaheadSelectOption[] = React.useMemo(() => {
if (isPreview && connectionTypeName) {
return [

Check warning on line 47 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L45-L47

Added lines #L45 - L47 were not covered by tests
{
value: connectionTypeName,
content: connectionTypeName,
isSelected: true,
},
];
}
if (!isPreview && connectionTypes) {
return connectionTypes.map((t) => ({

Check warning on line 56 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L55-L56

Added lines #L55 - L56 were not covered by tests
value: getResourceNameFromK8sResource(t),
content: getDisplayNameFromK8sResource(t),
isSelected: t.metadata.name === obj?.metadata.name,

Check warning on line 59 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L59

Added line #L59 was not covered by tests
}));
}
return [];
}, [isPreview, obj?.metadata.name, connectionTypes, connectionTypeName]);

Check warning on line 63 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L62-L63

Added lines #L62 - L63 were not covered by tests

return (
<Form>
<Title headingLevel="h1">Add connection</Title>
{isPreview && <Title headingLevel="h1">Add connection</Title>}

Check warning on line 67 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L67

Added line #L67 was not covered by tests
<FormGroup label="Connection type" fieldId="connection-type" isRequired>
<SimpleSelect
<TypeaheadSelect
id="connection-type"
isFullWidth
toggleLabel={connectionTypeName || 'Unspecified'}
isDisabled={!!connectionTypeName}
toggleProps={connectionTypeName ? undefined : { status: MenuToggleStatus.danger }}
onChange={() => undefined}
selectOptions={options}
onSelect={(_, selection) =>
setObj?.(connectionTypes?.find((c) => c.metadata.name === selection))

Check warning on line 73 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L72-L73

Added lines #L72 - L73 were not covered by tests
}
isDisabled={isPreview}
placeholder={
isPreview && !connectionTypeName
? 'Unspecified'
: 'Select a type, or search by keyword or category'

Check warning on line 79 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L77-L79

Added lines #L77 - L79 were not covered by tests
}
toggleProps={
isPreview && !connectionTypeName ? { status: MenuToggleStatus.danger } : undefined

Check warning on line 82 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L82

Added line #L82 was not covered by tests
}
/>
<HelperText>
{connectionTypeDescription ? (
<HelperTextItem>
<TruncatedText maxLines={2} content={connectionTypeDescription} />
</HelperTextItem>
) : undefined}
<HelperTextItem>
<Popover
headerContent="Connection type details"
bodyContent={
<DescriptionList>
<DescriptionListGroup>
<DescriptionListTerm>Connection type name</DescriptionListTerm>
<DescriptionListDescription>
{connectionTypeName || <UnspecifiedValue />}
</DescriptionListDescription>
</DescriptionListGroup>
{connectionTypeDescription ? (
<DescriptionListGroup>
<DescriptionListTerm>Connection type description</DescriptionListTerm>
<DescriptionListDescription>
{connectionTypeDescription || <UnspecifiedValue />}
</DescriptionListDescription>
</DescriptionListGroup>
) : undefined}
<DescriptionListGroup>
<DescriptionListTerm>Category</DescriptionListTerm>
<DescriptionListDescription>
{obj?.data?.category?.length ? (
<LabelGroup>
{obj.data.category.map((category) => (
<CategoryLabel key={category} category={category} />
))}
</LabelGroup>
) : (
<UnspecifiedValue />
)}
</DescriptionListDescription>
</DescriptionListGroup>
</DescriptionList>
}
>
<Button variant="link" isInline>
View connection type details
</Button>
</Popover>
</HelperTextItem>
</HelperText>
{(isPreview || obj?.metadata.name) && (
<ConnectionTypeDetailsHelperText connectionType={obj} />

Check warning on line 86 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L85-L86

Added lines #L85 - L86 were not covered by tests
)}
</FormGroup>
<FormSection title="Connection details" style={{ marginTop: 0 }}>
<NameDescriptionField
nameFieldId="connection-details-name"
descriptionFieldId="connection-details-description"
nameFieldLabel="Connection name"
descriptionFieldLabel="Connection description"
data={{
name: '',
description: '',
}}
/>
<ConnectionTypeFormFields fields={obj?.data?.fields} isPreview />
</FormSection>
{(isPreview || obj?.metadata.name) && (
<FormSection title="Connection details" style={{ marginTop: 0 }}>

Check warning on line 90 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L89-L90

Added lines #L89 - L90 were not covered by tests
<K8sNameDescriptionField
dataTestId="connection-name-desc"
nameLabel="Connection name"
descriptionLabel="Connection description"
data={
connectionNameDesc ?? {

Check warning on line 96 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L96

Added line #L96 was not covered by tests
name: '',
description: '',
k8sName: {
value: '',
state: {
immutable: true,
invalidCharacters: false,
invalidLength: false,
maxLength: 0,
touched: false,
},
},
}
}
onDataChange={setConnectionNameDesc}
/>
<ConnectionTypeFormFields
fields={obj?.data?.fields}

Check warning on line 114 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L114

Added line #L114 was not covered by tests
isPreview={isPreview}
connectionValues={connectionValues}
onChange={(field, value) => {
setConnectionValues?.({

Check warning on line 118 in frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/ConnectionTypePreview.tsx#L117-L118

Added lines #L117 - L118 were not covered by tests
...connectionValues,
[field.envVar]: value,
});
}}
/>
</FormSection>
)}
</Form>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const ConnectionTypePreviewDrawer: React.FC<Props> = ({ children, isExpanded, on
>
<Card isFlat isRounded>
<CardBody>
<ConnectionTypePreview obj={obj} />
<ConnectionTypePreview isPreview obj={obj} />
</CardBody>
</Card>
</DrawerContentBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@ import {
ConnectionTypeDataField,
ConnectionTypeField,
ConnectionTypeFieldType,
ConnectionTypeValueType,
SectionField,
} from '~/concepts/connectionTypes/types';

type Props = {
fields?: ConnectionTypeField[];
isPreview?: boolean;
onChange?: (field: ConnectionTypeDataField, value: unknown) => void;
onChange?: (field: ConnectionTypeDataField, value: ConnectionTypeValueType) => void;
connectionValues?: {
[key: string]: ConnectionTypeValueType;
};
};

type FieldGroup = { section: SectionField | undefined; fields: ConnectionTypeDataField[] };
type FieldGroup = {
section: SectionField | undefined;
fields: ConnectionTypeDataField[];
};

const ConnectionTypeFormFields: React.FC<Props> = ({ fields, isPreview, onChange }) => {
const ConnectionTypeFormFields: React.FC<Props> = ({
fields,
isPreview,
onChange,
connectionValues,

Check warning on line 31 in frontend/src/concepts/connectionTypes/fields/ConnectionTypeFormFields.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/fields/ConnectionTypeFormFields.tsx#L28-L31

Added lines #L28 - L31 were not covered by tests
}) => {
const fieldGroups = React.useMemo(
() =>
fields?.reduce<FieldGroup[]>((acc, field) => {
Expand All @@ -42,6 +54,7 @@ const ConnectionTypeFormFields: React.FC<Props> = ({ fields, isPreview, onChange
field={field}
mode={isPreview ? 'preview' : 'instance'}
onChange={onChange ? (v) => onChange(field, v) : undefined}
value={connectionValues?.[field.envVar]}

Check warning on line 57 in frontend/src/concepts/connectionTypes/fields/ConnectionTypeFormFields.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/concepts/connectionTypes/fields/ConnectionTypeFormFields.tsx#L57

Added line #L57 was not covered by tests
/>
)}
</DataFormFieldGroup>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/concepts/connectionTypes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export type ConnectionTypeConfigMapObj = Omit<ConnectionTypeConfigMap, 'data'> &
};
};

export type ConnectionTypeValueType = ConnectionTypeDataField['properties']['defaultValue'];

export type Connection = SecretKind & {
metadata: {
labels: DashboardLabels & {
Expand All @@ -145,7 +147,7 @@ export type Connection = SecretKind & {
'opendatahub.io/connection-type': string;
};
};
data: {
data?: {
[key: string]: string;
};
};
Expand Down
Loading

0 comments on commit 71d923a

Please sign in to comment.