Skip to content

Commit

Permalink
Trigger Modal Support
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewballantyne committed Apr 16, 2020
1 parent 1c4f910 commit 9ee3124
Show file tree
Hide file tree
Showing 49 changed files with 1,276 additions and 865 deletions.

This file was deleted.

17 changes: 12 additions & 5 deletions frontend/packages/dev-console/src/components/pipelines/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ export enum StartedByLabel {
}

export enum PipelineResourceType {
'' = 'Select resource type',
git = 'Git',
image = 'Image',
cluster = 'Cluster',
storage = 'Storage',
git = 'git',
image = 'image',
cluster = 'cluster',
storage = 'storage',
}

export const pipelineResourceTypeSelections = {
'': 'Select resource type',
[PipelineResourceType.git]: 'Git',
[PipelineResourceType.image]: 'Image',
[PipelineResourceType.cluster]: 'Cluster',
[PipelineResourceType.storage]: 'Storage',
};

export enum VolumeTypes {
EmptyDirectory = 'Empty Directory',
ConfigMap = 'Config Map',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { TextInputTypes } from '@patternfly/react-core';
import { MultiColumnField, InputField, DropdownField } from '@console/shared';
import { PipelineResourceType } from '../const';
import { pipelineResourceTypeSelections } from '../const';

type PipelineResourcesParam = {
addLabel?: string;
Expand All @@ -27,7 +27,12 @@ const PipelineResources: React.FC<PipelineResourcesParam> = (props) => {
placeholder="Name"
isReadOnly={isReadOnly}
/>
<DropdownField name="type" items={PipelineResourceType} fullWidth disabled={isReadOnly} />
<DropdownField
name="type"
items={pipelineResourceTypeSelections}
fullWidth
disabled={isReadOnly}
/>
</MultiColumnField>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as yup from 'yup';
import { VolumeTypes } from '../const';

export const resourcesValidationSchema = yup.object().shape({
resources: yup.array().of(
Expand All @@ -19,83 +18,3 @@ export const parametersValidationSchema = yup.object().shape({
}),
),
});

const volumeTypeSchema = yup
.object()
.when('type', {
is: (type) => VolumeTypes[type] === VolumeTypes.Secret,
then: yup.object().shape({
secret: yup.object().shape({
secretName: yup.string().required('Required'),
items: yup.array().of(
yup.object().shape({
key: yup.string(),
path: yup.string().when('key', {
is: (key) => !!key,
then: yup.string().required('Required'),
}),
}),
),
}),
}),
})
.when('type', {
is: (type) => VolumeTypes[type] === VolumeTypes.ConfigMap,
then: yup.object().shape({
configMap: yup.object().shape({
name: yup.string().required('Required'),
items: yup.array().of(
yup.object().shape({
key: yup.string(),
path: yup.string().when('key', {
is: (key) => !!key,
then: yup.string().required('Required'),
}),
}),
),
}),
}),
})
.when('type', {
is: (type) => VolumeTypes[type] === VolumeTypes.PVC,
then: yup.object().shape({
persistentVolumeClaim: yup.object().shape({
claimName: yup.string().required('Required'),
}),
}),
});

export const startPipelineSchema = yup.object().shape({
resources: yup.array().of(
yup.object().shape({
name: yup.string().required('Required'),
type: yup.string().required('Required'),
resourceRef: yup.object().shape({
name: yup.string().required('Required'),
}),
}),
),
parameters: yup.array().of(
yup.object().shape({
name: yup.string().required('Required'),
description: yup.string(),
default: yup.string().required('Required'),
}),
),
workspaces: yup.array().of(
yup.object().shape({
type: yup.string().required('Required'),
data: volumeTypeSchema,
}),
),
secretOpen: yup.boolean().equals([false]),
});

export const advancedSectionValidationSchema = yup.object().shape({
secretName: yup.string().required('Required'),
type: yup.string().required('Required'),
annotations: yup.object().shape({
key: yup.string().required('Required'),
value: yup.string().required('Required'),
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as React from 'react';
import * as _ from 'lodash';
import { FormikProps, FormikValues } from 'formik';
import { Form } from '@patternfly/react-core';
import { ModalBody, ModalSubmitFooter, ModalTitle } from '@console/internal/components/factory';

type ModalStructureProps = FormikProps<FormikValues> & {
children: React.ReactNode;
close: () => void;
submitBtnText: string;
title: string;
};

const ModalStructure: React.FC<ModalStructureProps> = (props) => {
const {
children,
close,
errors,
isSubmitting,
handleSubmit,
status,
submitBtnText,
title,
} = props;

return (
<Form onSubmit={handleSubmit}>
<div className="modal-content">
<ModalTitle>{title}</ModalTitle>
<ModalBody>
<div className="pf-c-form">{children}</div>
</ModalBody>
<ModalSubmitFooter
errorMessage={status?.submitError}
inProgress={isSubmitting}
submitText={submitBtnText}
submitDisabled={!_.isEmpty(errors)}
cancel={close}
/>
</div>
</Form>
);
};

export default ModalStructure;
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,30 @@ import * as React from 'react';
import { FieldArray } from 'formik';
import { TextInputTypes } from '@patternfly/react-core';
import { InputField } from '@console/shared';
import { PipelineParam } from '../../../../utils/pipeline-augment';
import FormSection from '../../../import/section/FormSection';

export interface ParamertersSectionProps {
parameters: {
name: string;
description?: string;
default: string;
}[];
}
type ParametersSectionProps = {
parameters: PipelineParam[];
};

const PipelineParameterSection: React.FC<ParamertersSectionProps> = ({ parameters }) => (
const PipelineParameterSection: React.FC<ParametersSectionProps> = ({ parameters }) => (
<FieldArray
name="parameters"
key="parameters-row"
render={() =>
parameters.length > 0 && (
<FormSection title="Parameters" fullWidth>
{parameters.map((parameter, index) => (
<div
className="form-group"
// eslint-disable-next-line react/no-array-index-key
key={`${parameter.name}-${index}`}
>
<InputField
name={`parameters.${index}.default`}
type={TextInputTypes.text}
label={parameter.name}
helpText={parameter.description}
placeholder="Name"
required
/>
</div>
<InputField
key={parameter.name}
name={`parameters.${index}.default`}
type={TextInputTypes.text}
label={parameter.name}
helpText={parameter.description}
placeholder="Name"
required
/>
))}
</FormSection>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.odc-pipeline-resource-dropdown {
li[role=presentation] {
list-style: none;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as React from 'react';
import * as _ from 'lodash';
import { FormikValues, useField, useFormikContext } from 'formik';
import { Select, SelectOption } from '@patternfly/react-core';
import { referenceForModel } from '@console/internal/module/k8s';
import {
useK8sWatchResource,
WatchK8sResource,
} from '@console/internal/components/utils/k8s-watch-hook';
import { PipelineResourceModel } from '../../../../models';
import { PipelineResourceKind } from '../../../../utils/pipeline-augment';

import './PipelineResourceDropdown.scss';
import { LoadingInline } from '@console/internal/components/utils';
import { CREATE_PIPELINE_RESOURCE } from './const';

type PipelineResourceDropdownProps = {
autoSelect?: boolean;
filterType: string;
name: string;
namespace: string;
selectedKey: string;
};

const PipelineResourceDropdown: React.FC<PipelineResourceDropdownProps> = (props) => {
const { autoSelect, filterType, name, namespace, selectedKey } = props;

const [isExpanded, setExpanded] = React.useState(false);
const { setFieldValue, setFieldTouched } = useFormikContext<FormikValues>();
const [, { touched }] = useField(name);

const resourceDefinition: WatchK8sResource = React.useMemo(
() => ({
isList: true,
namespace,
kind: referenceForModel(PipelineResourceModel),
}),
[namespace],
);
const [resources, loaded, error] = useK8sWatchResource<PipelineResourceKind[]>(
resourceDefinition,
);

const availableResources: PipelineResourceKind[] = resources.filter(
(resource) => resource.spec.type === filterType,
);

const canAutoSelect = autoSelect && !touched && loaded && !error;
React.useEffect(() => {
if (canAutoSelect) {
if (availableResources.length === 0) {
setFieldValue(name, CREATE_PIPELINE_RESOURCE);
} else {
setFieldValue(name, availableResources[0].metadata.name);
}
setFieldTouched(name);
}
}, [canAutoSelect, name, availableResources, setFieldTouched, setFieldValue]);

const options = [
{ label: 'Create Pipeline Resource', value: CREATE_PIPELINE_RESOURCE },
...availableResources.map((resource) => {
const resourceName = resource.metadata.name;
const url = _.get(_.find(resource.spec.params, ['name', 'url']), 'value', '');
const label = url.trim().length > 0 ? `${url} (${resourceName})` : resourceName;

return { label, value: resourceName };
}),
];

return (
<Select
className="odc-pipeline-resource-dropdown"
selections={selectedKey}
isExpanded={isExpanded}
onToggle={() => setExpanded(!isExpanded)}
onSelect={(e, value) => {
setFieldValue(name, value);
setExpanded(false);
}}
placeholderText={!loaded ? <LoadingInline /> : 'Select Pipeline Resource'}
isDisabled={loaded && resources.length === 0}
>
{options.map(({ label, value }) => (
<SelectOption key={value} value={value}>
{label}
</SelectOption>
))}
</Select>
);
};

export default PipelineResourceDropdown;

0 comments on commit 9ee3124

Please sign in to comment.