Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8871465
commit 8c76f5f
Showing
15 changed files
with
299 additions
and
75 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
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
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
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
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
93 changes: 93 additions & 0 deletions
93
frontend/packages/knative-plugin/src/components/add/event-sources/ApiServerSection.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,93 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { useFormikContext, FormikValues } from 'formik'; | ||
import { FormGroup } from '@patternfly/react-core'; | ||
import { NameValueEditor } from '@console/internal/components/utils/name-value-editor'; | ||
import { ResourceDropdownField, DropdownField, getFieldId } from '@console/shared'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
|
||
interface ApiServerSectionProps { | ||
namespace: string; | ||
} | ||
|
||
const ApiServerSection: React.FC<ApiServerSectionProps> = ({ namespace }) => { | ||
const { values, setFieldValue, setFieldTouched, validateForm } = useFormikContext<FormikValues>(); | ||
const initVal = values?.data?.apiserversource?.resources || []; | ||
const initialValueResources = !_.isEmpty(initVal) | ||
? initVal.map((val) => _.values(val)) | ||
: [['', '']]; | ||
const [nameValue, setNameValue] = React.useState(initialValueResources); | ||
const handleNameValuePairs = React.useCallback( | ||
({ nameValuePairs }) => { | ||
const updatedNameValuePairs = _.compact( | ||
nameValuePairs.map(([name, value]) => { | ||
if (value.length) { | ||
return { apiVersion: name, kind: value }; | ||
} | ||
return null; | ||
}), | ||
); | ||
setNameValue(nameValuePairs); | ||
setFieldValue('data.apiserversource.resources', updatedNameValuePairs); | ||
}, | ||
[setFieldValue], | ||
); | ||
const onChange = React.useCallback( | ||
(selectedValue) => { | ||
if (selectedValue) { | ||
setFieldTouched('data.apiserversource.serviceAccountName', true); | ||
setFieldValue('data.apiserversource.serviceAccountName', selectedValue); | ||
validateForm(); | ||
} | ||
}, | ||
[setFieldValue, setFieldTouched, validateForm], | ||
); | ||
const resources = [ | ||
{ | ||
isList: true, | ||
kind: 'ServiceAccount', | ||
namespace, | ||
prop: 'serviceaccount', | ||
optional: true, | ||
}, | ||
]; | ||
const modeItems = { | ||
Ref: 'Ref', | ||
Resource: 'Resource', | ||
}; | ||
const fieldId = getFieldId(values.type, 'res-input'); | ||
return ( | ||
<FormSection title="ApiServerSource"> | ||
<FormGroup fieldId={fieldId} label="Resource" isRequired> | ||
<NameValueEditor | ||
nameValuePairs={nameValue} | ||
valueString="kind" | ||
nameString="apiVersion" | ||
addString="Add Resource" | ||
readOnly={false} | ||
allowSorting={false} | ||
updateParentData={handleNameValuePairs} | ||
/> | ||
</FormGroup> | ||
<DropdownField | ||
name="data.apiserversource.mode" | ||
label="Mode" | ||
items={modeItems} | ||
title={modeItems.Ref} | ||
fullWidth | ||
/> | ||
<ResourceDropdownField | ||
name="data.apiserversource.serviceAccountName" | ||
label="Service Account Name" | ||
resources={resources} | ||
dataSelector={['metadata', 'name']} | ||
fullWidth | ||
placeholder="Select Service Account Name" | ||
onChange={onChange} | ||
autoSelect | ||
/> | ||
</FormSection> | ||
); | ||
}; | ||
|
||
export default ApiServerSection; |
24 changes: 19 additions & 5 deletions
24
frontend/packages/knative-plugin/src/components/add/event-sources/EventSourcesSelector.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 |
---|---|---|
@@ -1,16 +1,30 @@ | ||
import * as React from 'react'; | ||
import { useFormikContext, FormikValues } from 'formik'; | ||
import { ItemSelectorField } from '@console/shared'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
import { NormalizedEventSources } from '../import-types'; | ||
import { getEventSourceData } from '../../../utils/create-eventsources-utils'; | ||
|
||
interface EventSourcesSelectorProps { | ||
eventSourceList: NormalizedEventSources; | ||
} | ||
|
||
const EventSourcesSelector: React.FC<EventSourcesSelectorProps> = ({ eventSourceList }) => ( | ||
<FormSection title="Type" fullWidth> | ||
<ItemSelectorField itemList={eventSourceList} name="type" /> | ||
</FormSection> | ||
); | ||
const EventSourcesSelector: React.FC<EventSourcesSelectorProps> = ({ eventSourceList }) => { | ||
const { setFieldValue, setFieldTouched, validateForm } = useFormikContext<FormikValues>(); | ||
const handleItemChange = React.useCallback( | ||
(item: string) => { | ||
const name = `data.${item.toLowerCase()}`; | ||
setFieldValue(name, getEventSourceData(item.toLowerCase())); | ||
setFieldTouched(name, true); | ||
validateForm(); | ||
}, | ||
[setFieldValue, setFieldTouched, validateForm], | ||
); | ||
return ( | ||
<FormSection title="Type" fullWidth> | ||
<ItemSelectorField itemList={eventSourceList} name="type" onSelect={handleItemChange} /> | ||
</FormSection> | ||
); | ||
}; | ||
|
||
export default EventSourcesSelector; |
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
38 changes: 38 additions & 0 deletions
38
...kages/knative-plugin/src/components/add/event-sources/__tests__/ApiServerSection.spec.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,38 @@ | ||
import * as React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { ResourceDropdownField } from '@console/shared'; | ||
import { NameValueEditor } from '@console/internal/components/utils/name-value-editor'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
import ApiServerSection from '../ApiServerSection'; | ||
|
||
jest.mock('formik', () => ({ | ||
useField: jest.fn(() => [{}, {}]), | ||
useFormikContext: jest.fn(() => ({ | ||
setFieldValue: jest.fn(), | ||
setFieldTouched: jest.fn(), | ||
validateForm: jest.fn(), | ||
values: { | ||
type: 'ApiServerSource', | ||
}, | ||
})), | ||
getFieldId: jest.fn(), | ||
})); | ||
describe('ApiServerSection', () => { | ||
it('should render FormSection', () => { | ||
const wrapper = shallow(<ApiServerSection namespace="test-project" />); | ||
expect(wrapper.find(FormSection)).toHaveLength(1); | ||
expect(wrapper.find(FormSection).props().title).toBe('ApiServerSource'); | ||
}); | ||
|
||
it('should render NameValueEditor', () => { | ||
const wrapper = shallow(<ApiServerSection namespace="test-project" />); | ||
const nameValueEditorField = wrapper.find(NameValueEditor); | ||
expect(nameValueEditorField).toHaveLength(1); | ||
expect(nameValueEditorField.props().nameString).toBe('apiVersion'); | ||
expect(nameValueEditorField.props().valueString).toBe('kind'); | ||
}); | ||
it('should render ResourceDropdownField', () => { | ||
const wrapper = shallow(<ApiServerSection namespace="test-project" />); | ||
expect(wrapper.find(ResourceDropdownField)).toHaveLength(1); | ||
}); | ||
}); |
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
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
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.