refactor lookup components

This commit is contained in:
Keith Grant
2019-08-22 14:48:25 -07:00
parent 6fd86fed65
commit 8a31be6ffe
6 changed files with 137 additions and 114 deletions

View File

@@ -1 +1,3 @@
export { default } from './Lookup'; export { default } from './Lookup';
export { default as InstanceGroupsLookup } from './InstanceGroupsLookup';
export { default as InventoriesLookup } from './InventoriesLookup';

View File

@@ -15,10 +15,9 @@ import FormRow from '@components/FormRow';
import FormField from '@components/FormField'; import FormField from '@components/FormField';
import FormActionGroup from '@components/FormActionGroup/FormActionGroup'; import FormActionGroup from '@components/FormActionGroup/FormActionGroup';
import AnsibleSelect from '@components/AnsibleSelect'; import AnsibleSelect from '@components/AnsibleSelect';
import { InstanceGroupsLookup } from '@components/Lookup/';
import { required, minMaxValue } from '@util/validators'; import { required, minMaxValue } from '@util/validators';
import InstanceGroupsLookup from './InstanceGroupsLookup';
class OrganizationForm extends Component { class OrganizationForm extends Component {
constructor(props) { constructor(props) {
super(props); super(props);

View File

@@ -1,2 +1,2 @@
export { default as InstanceGroupsLookup } from './InstanceGroupsLookup'; /* eslint-disable-next-line import/prefer-default-export */
export { default as OrganizationForm } from './OrganizationForm'; export { default as OrganizationForm } from './OrganizationForm';

View File

@@ -17,18 +17,13 @@ import CollapsibleSection from '@components/CollapsibleSection';
import { required } from '@util/validators'; import { required } from '@util/validators';
import styled from 'styled-components'; import styled from 'styled-components';
import { JobTemplate } from '@types'; import { JobTemplate } from '@types';
import InventoriesLookup from './InventoriesLookup'; import { InventoriesLookup, InstanceGroupsLookup } from '@components/Lookup';
import ProjectLookup from './ProjectLookup'; import ProjectLookup from './ProjectLookup';
import { LabelsAPI, ProjectsAPI } from '@api'; import { JobTemplatesAPI, LabelsAPI, ProjectsAPI } from '@api';
const QuestionCircleIcon = styled(PFQuestionCircleIcon)` const QuestionCircleIcon = styled(PFQuestionCircleIcon)`
margin-left: 10px; margin-left: 10px;
`; `;
const QSConfig = {
page: 1,
page_size: 200,
order_by: 'name',
};
class JobTemplateForm extends Component { class JobTemplateForm extends Component {
static propTypes = { static propTypes = {
@@ -64,6 +59,7 @@ class JobTemplateForm extends Component {
project: props.template.summary_fields.project, project: props.template.summary_fields.project,
inventory: props.template.summary_fields.inventory, inventory: props.template.summary_fields.inventory,
relatedProjectPlaybooks: props.relatedProjectPlaybooks, relatedProjectPlaybooks: props.relatedProjectPlaybooks,
relatedInstanceGroups: [],
}; };
this.handleNewLabel = this.handleNewLabel.bind(this); this.handleNewLabel = this.handleNewLabel.bind(this);
this.loadLabels = this.loadLabels.bind(this); this.loadLabels = this.loadLabels.bind(this);
@@ -74,21 +70,29 @@ class JobTemplateForm extends Component {
); );
} }
async componentDidMount() { componentDidMount() {
const { validateField } = this.props; const { validateField } = this.props;
await this.loadLabels(QSConfig);
validateField('project'); validateField('project');
this.setState({ contentError: null, hasContentLoading: true });
Promise.all([this.loadLabels(), this.loadRelatedInstanceGroups()]).then(
() => {
this.setState({ hasContentLoading: false });
}
);
} }
async loadLabels(QueryConfig) { async loadLabels() {
// This function assumes that the user has no more than 400 // This function assumes that the user has no more than 400
// labels. For the vast majority of users this will be more thans // labels. For the vast majority of users this will be more thans
// enough. This can be updated to allow more than 400 labels if we // enough. This can be updated to allow more than 400 labels if we
// decide it is necessary. // decide it is necessary.
this.setState({ contentError: null, hasContentLoading: true });
let loadedLabels; let loadedLabels;
try { try {
const { data } = await LabelsAPI.read(QueryConfig); const { data } = await LabelsAPI.read({
page: 1,
page_size: 200,
order_by: 'name',
});
loadedLabels = [...data.results]; loadedLabels = [...data.results];
if (data.next && data.next.includes('page=2')) { if (data.next && data.next.includes('page=2')) {
const { const {
@@ -103,8 +107,19 @@ class JobTemplateForm extends Component {
this.setState({ loadedLabels }); this.setState({ loadedLabels });
} catch (err) { } catch (err) {
this.setState({ contentError: err }); this.setState({ contentError: err });
} finally { }
this.setState({ hasContentLoading: false }); }
async loadRelatedInstanceGroups() {
const { template } = this.props;
if (!template.id) {
return;
}
try {
const { data } = await JobTemplatesAPI.readInstanceGroups(template.id);
console.log(data.results);
} catch (err) {
this.setState({ contentError: err });
} }
} }
@@ -519,6 +534,13 @@ class JobTemplateForm extends Component {
)} )}
/> />
</FormRow> </FormRow>
{/* <InstanceGroupsLookup
value={instanceGroups}
onChange={this.handleInstanceGroupsChange}
tooltip={i18n._(
t`Select the Instance Groups for this Organization to run on.`
)}
/> */}
</CollapsibleSection> </CollapsibleSection>
<FormActionGroup onCancel={handleCancel} onSubmit={handleSubmit} /> <FormActionGroup onCancel={handleCancel} onSubmit={handleSubmit} />
</Form> </Form>
@@ -541,7 +563,7 @@ const FormikApp = withFormik({
verbosity, verbosity,
job_slicing, job_slicing,
timeout, timeout,
diff_mode diff_mode,
summary_fields = { labels: { results: [] } }, summary_fields = { labels: { results: [] } },
} = { ...template }; } = { ...template };