From 3c1de8d6835bf10e53c800477eef53e6f1b15158 Mon Sep 17 00:00:00 2001 From: nixocio Date: Tue, 20 Jul 2021 10:00:21 -0400 Subject: [PATCH] Add auto-populate inventory host form Add auto-populate inventory host form closes:https://github.com/ansible/awx/issues/10663 --- .../src/components/HostForm/HostForm.js | 1 + .../src/components/Lookup/InventoryLookup.js | 52 +++++++++++-------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/awx/ui_next/src/components/HostForm/HostForm.js b/awx/ui_next/src/components/HostForm/HostForm.js index dabccebbaa..8bbf6ac9f3 100644 --- a/awx/ui_next/src/components/HostForm/HostForm.js +++ b/awx/ui_next/src/components/HostForm/HostForm.js @@ -39,6 +39,7 @@ const InventoryLookupField = ({ isDisabled }) => { validate={required(t`Select a value for this field`)} isDisabled={isDisabled} hideSmartInventories + autoPopulate={!inventoryField.value?.id} /> ); diff --git a/awx/ui_next/src/components/Lookup/InventoryLookup.js b/awx/ui_next/src/components/Lookup/InventoryLookup.js index 105f62da4b..71a92108f4 100644 --- a/awx/ui_next/src/components/Lookup/InventoryLookup.js +++ b/awx/ui_next/src/components/Lookup/InventoryLookup.js @@ -5,6 +5,7 @@ import { t } from '@lingui/macro'; import { InventoriesAPI } from 'api'; import { Inventory } from 'types'; import useRequest from 'hooks/useRequest'; +import useAutoPopulateLookup from 'hooks/useAutoPopulateLookup'; import { getQSConfig, parseQueryString, mergeParams } from 'util/qs'; import Lookup from './Lookup'; import OptionsList from '../OptionsList'; @@ -19,21 +20,24 @@ const QS_CONFIG = getQSConfig('inventory', { }); function InventoryLookup({ - value, - onChange, - onBlur, - history, - required, - isPromptableField, + autoPopulate, fieldId, + fieldName, + hideSmartInventories, + history, + isDisabled, + isOverrideDisabled, + isPromptableField, + onBlur, + onChange, promptId, promptName, - isOverrideDisabled, + required, validate, - fieldName, - isDisabled, - hideSmartInventories, + value, }) { + const autoPopulateLookup = useAutoPopulateLookup(onChange); + const { result: { inventories, @@ -60,6 +64,10 @@ function InventoryLookup({ InventoriesAPI.readOptions(), ]); + if (autoPopulate) { + autoPopulateLookup(data.results); + } + return { inventories: data.results, count: data.count, @@ -78,7 +86,7 @@ function InventoryLookup({ Boolean(actionsResponse.data.actions.POST) || isOverrideDisabled, }; // eslint-disable-next-line react-hooks/exhaustive-deps - }, [history.location]), + }, [autoPopulate, autoPopulateLookup, history.location]), { inventories: [], count: 0, @@ -236,26 +244,28 @@ function InventoryLookup({ } InventoryLookup.propTypes = { + autoPopulate: bool, fieldId: string, - value: Inventory, + fieldName: string, + hideSmartInventories: bool, + isDisabled: bool, + isOverrideDisabled: bool, onChange: func.isRequired, required: bool, - isOverrideDisabled: bool, validate: func, - fieldName: string, - isDisabled: bool, - hideSmartInventories: bool, + value: Inventory, }; InventoryLookup.defaultProps = { + autoPopulate: false, fieldId: 'inventory', - value: null, - required: false, - isOverrideDisabled: false, - validate: () => {}, fieldName: 'inventory', - isDisabled: false, hideSmartInventories: false, + isDisabled: false, + isOverrideDisabled: false, + required: false, + validate: () => {}, + value: null, }; export default withRouter(InventoryLookup);