From 9c7d449a4d334782e2f19369b85c1a9cfa95437c Mon Sep 17 00:00:00 2001 From: kialam Date: Tue, 11 Dec 2018 16:20:55 -0500 Subject: [PATCH] Abstract out API get request to Add Org component. - This makes it so we now have a generic select dropdown where we can pass data down as props. --- .../AnsibleEnvironmentSelect.test.jsx | 11 +++---- .../AnsibleEnvironmentSelect.jsx | 29 ++++--------------- .../AnsibleEnvironmentSelect/index.js | 4 +-- .../Organizations/views/Organization.add.jsx | 23 ++++++++++++--- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/__tests__/components/AnsibleEnvironmentSelect.test.jsx b/__tests__/components/AnsibleEnvironmentSelect.test.jsx index 312b033547..0bcf9d3ded 100644 --- a/__tests__/components/AnsibleEnvironmentSelect.test.jsx +++ b/__tests__/components/AnsibleEnvironmentSelect.test.jsx @@ -1,15 +1,16 @@ import React from 'react'; import { mount } from 'enzyme'; -import AnsibleEnvironmentSelect from '../../src/components/AnsibleEnvironmentSelect'; +import AnsibleSelect from '../../src/components/AnsibleEnvironmentSelect'; -describe('', () => { +const mockData = ['foo', 'bar']; +describe('', () => { test('initially renders succesfully', async() => { - const wrapper = mount( {}} />); + const wrapper = mount( {}} />); wrapper.setState({ isHidden: false }); }); test('calls "onSelectChange" on dropdown select change', () => { - const spy = jest.spyOn(AnsibleEnvironmentSelect.prototype, 'onSelectChange'); - const wrapper = mount( {}} />); + const spy = jest.spyOn(AnsibleSelect.prototype, 'onSelectChange'); + const wrapper = mount( {}} />); wrapper.setState({ isHidden: false }); expect(spy).not.toHaveBeenCalled(); wrapper.find('select').simulate('change'); diff --git a/src/components/AnsibleEnvironmentSelect/AnsibleEnvironmentSelect.jsx b/src/components/AnsibleEnvironmentSelect/AnsibleEnvironmentSelect.jsx index d07bfdaac6..632a5b544f 100644 --- a/src/components/AnsibleEnvironmentSelect/AnsibleEnvironmentSelect.jsx +++ b/src/components/AnsibleEnvironmentSelect/AnsibleEnvironmentSelect.jsx @@ -4,43 +4,26 @@ import { Select, SelectOption, } from '@patternfly/react-core'; -import { API_CONFIG } from '../../endpoints'; -import api from '../../api'; -class AnsibleEnvironmentSelect extends React.Component { +class AnsibleSelect extends React.Component { constructor(props) { super(props); this.onSelectChange = this.onSelectChange.bind(this); } - state = { - custom_virtualenvs: [], - isHidden: true, - } - - async componentDidMount() { - const { data } = await api.get(API_CONFIG); - this.setState({ custom_virtualenvs: [...data.custom_virtualenvs] }); - if (this.state.custom_virtualenvs.length > 1) { - // Show dropdown if we have more than one ansible environment - this.setState({ isHidden: !this.state.isHidden }); - } - } - onSelectChange(val, _) { this.props.selectChange(val); } render() { - const { isHidden } = this.state; - if (isHidden) { + const hide = this.props.hidden; + if (hide) { return null; } else { return ( - + @@ -50,4 +33,4 @@ class AnsibleEnvironmentSelect extends React.Component { } } -export default AnsibleEnvironmentSelect; +export default AnsibleSelect; diff --git a/src/components/AnsibleEnvironmentSelect/index.js b/src/components/AnsibleEnvironmentSelect/index.js index 2e7a7f44a3..1f2327e390 100644 --- a/src/components/AnsibleEnvironmentSelect/index.js +++ b/src/components/AnsibleEnvironmentSelect/index.js @@ -1,3 +1,3 @@ -import AnsibleEnvironmentSelect from './AnsibleEnvironmentSelect'; +import AnsibleSelect from './AnsibleEnvironmentSelect'; -export default AnsibleEnvironmentSelect; +export default AnsibleSelect; diff --git a/src/pages/Organizations/views/Organization.add.jsx b/src/pages/Organizations/views/Organization.add.jsx index 0c8fc6c57d..3e03511a77 100644 --- a/src/pages/Organizations/views/Organization.add.jsx +++ b/src/pages/Organizations/views/Organization.add.jsx @@ -15,9 +15,9 @@ import { CardBody, } from '@patternfly/react-core'; -import { API_ORGANIZATIONS } from '../../../endpoints'; +import { API_ORGANIZATIONS, API_CONFIG } from '../../../endpoints'; import api from '../../../api'; -import AnsibleEnvironmentSelect from '../../../components/AnsibleEnvironmentSelect' +import AnsibleSelect from '../../../components/AnsibleEnvironmentSelect' const { light } = PageSectionVariants; class OrganizationAdd extends React.Component { @@ -35,7 +35,8 @@ class OrganizationAdd extends React.Component { description: '', instanceGroups: '', custom_virtualenv: '', - post_endpoint: API_ORGANIZATIONS, + custom_virtualenvs: [], + hideAnsibleSelect: true, }; onSelectChange(value, _) { @@ -60,6 +61,14 @@ class OrganizationAdd extends React.Component { this.resetForm(); } + async componentDidMount() { + const { data } = await api.get(API_CONFIG); + this.setState({ custom_virtualenvs: [...data.custom_virtualenvs] }); + if (this.state.custom_virtualenvs.length > 1) { + // Show dropdown if we have more than one ansible environment + this.setState({ hideAnsibleSelect: !this.state.hideAnsibleSelect }); + } + } render() { const { name } = this.state; const enabled = name.length > 0; // TODO: add better form validation @@ -104,7 +113,13 @@ class OrganizationAdd extends React.Component { onChange={this.handleChange} /> - +