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.
This commit is contained in:
kialam
2018-12-11 16:20:55 -05:00
parent d047bc876a
commit 9c7d449a4d
4 changed files with 33 additions and 34 deletions

View File

@@ -1,15 +1,16 @@
import React from 'react'; import React from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import AnsibleEnvironmentSelect from '../../src/components/AnsibleEnvironmentSelect'; import AnsibleSelect from '../../src/components/AnsibleEnvironmentSelect';
describe('<AnsibleEnvironmentSelect />', () => { const mockData = ['foo', 'bar'];
describe('<AnsibleSelect />', () => {
test('initially renders succesfully', async() => { test('initially renders succesfully', async() => {
const wrapper = mount(<AnsibleEnvironmentSelect selected="foo" selectChange={() => {}} />); const wrapper = mount(<AnsibleSelect selected="foo" data={mockData} selectChange={() => {}} />);
wrapper.setState({ isHidden: false }); wrapper.setState({ isHidden: false });
}); });
test('calls "onSelectChange" on dropdown select change', () => { test('calls "onSelectChange" on dropdown select change', () => {
const spy = jest.spyOn(AnsibleEnvironmentSelect.prototype, 'onSelectChange'); const spy = jest.spyOn(AnsibleSelect.prototype, 'onSelectChange');
const wrapper = mount(<AnsibleEnvironmentSelect selected="foo" selectChange={() => {}} />); const wrapper = mount(<AnsibleSelect selected="foo" data={mockData} selectChange={() => {}} />);
wrapper.setState({ isHidden: false }); wrapper.setState({ isHidden: false });
expect(spy).not.toHaveBeenCalled(); expect(spy).not.toHaveBeenCalled();
wrapper.find('select').simulate('change'); wrapper.find('select').simulate('change');

View File

@@ -4,43 +4,26 @@ import {
Select, Select,
SelectOption, SelectOption,
} from '@patternfly/react-core'; } 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) { constructor(props) {
super(props); super(props);
this.onSelectChange = this.onSelectChange.bind(this); 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, _) { onSelectChange(val, _) {
this.props.selectChange(val); this.props.selectChange(val);
} }
render() { render() {
const { isHidden } = this.state; const hide = this.props.hidden;
if (isHidden) { if (hide) {
return null; return null;
} else { } else {
return ( return (
<FormGroup label="Ansible Environment" fieldId="simple-form-instance-groups"> <FormGroup label={this.props.labelName} fieldId="ansible-select">
<Select value={this.props.selected} onChange={this.onSelectChange} aria-label="Select Input"> <Select value={this.props.selected} onChange={this.onSelectChange} aria-label="Select Input">
<SelectOption isDisabled value="Select Ansible Environment" label="Select Ansible Environment" /> {this.props.data.map((env, index) => (
{this.state.custom_virtualenvs.map((env, index) => (
<SelectOption isDisabled={env.disabled} key={index} value={env} label={env} /> <SelectOption isDisabled={env.disabled} key={index} value={env} label={env} />
))} ))}
</Select> </Select>
@@ -50,4 +33,4 @@ class AnsibleEnvironmentSelect extends React.Component {
} }
} }
export default AnsibleEnvironmentSelect; export default AnsibleSelect;

View File

@@ -1,3 +1,3 @@
import AnsibleEnvironmentSelect from './AnsibleEnvironmentSelect'; import AnsibleSelect from './AnsibleEnvironmentSelect';
export default AnsibleEnvironmentSelect; export default AnsibleSelect;

View File

@@ -15,9 +15,9 @@ import {
CardBody, CardBody,
} from '@patternfly/react-core'; } from '@patternfly/react-core';
import { API_ORGANIZATIONS } from '../../../endpoints'; import { API_ORGANIZATIONS, API_CONFIG } from '../../../endpoints';
import api from '../../../api'; import api from '../../../api';
import AnsibleEnvironmentSelect from '../../../components/AnsibleEnvironmentSelect' import AnsibleSelect from '../../../components/AnsibleEnvironmentSelect'
const { light } = PageSectionVariants; const { light } = PageSectionVariants;
class OrganizationAdd extends React.Component { class OrganizationAdd extends React.Component {
@@ -35,7 +35,8 @@ class OrganizationAdd extends React.Component {
description: '', description: '',
instanceGroups: '', instanceGroups: '',
custom_virtualenv: '', custom_virtualenv: '',
post_endpoint: API_ORGANIZATIONS, custom_virtualenvs: [],
hideAnsibleSelect: true,
}; };
onSelectChange(value, _) { onSelectChange(value, _) {
@@ -60,6 +61,14 @@ class OrganizationAdd extends React.Component {
this.resetForm(); 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() { render() {
const { name } = this.state; const { name } = this.state;
const enabled = name.length > 0; // TODO: add better form validation const enabled = name.length > 0; // TODO: add better form validation
@@ -104,7 +113,13 @@ class OrganizationAdd extends React.Component {
onChange={this.handleChange} onChange={this.handleChange}
/> />
</FormGroup> </FormGroup>
<AnsibleEnvironmentSelect selected={this.state.custom_virtualenv} selectChange={this.onSelectChange} /> <AnsibleSelect
labelName="Ansible Environment"
selected={this.state.custom_virtualenv}
selectChange={this.onSelectChange}
data={this.state.custom_virtualenvs}
hidden={this.state.hideAnsibleSelect}
/>
</Gallery> </Gallery>
<ActionGroup className="at-align-right"> <ActionGroup className="at-align-right">
<Toolbar> <Toolbar>