Merge pull request #10692 from nixocio/ui_issue_10663_again

Add auto-populate inventory host form
This commit is contained in:
Tiago Góes
2021-07-28 10:32:08 -03:00
committed by GitHub
2 changed files with 32 additions and 21 deletions

View File

@@ -39,6 +39,7 @@ const InventoryLookupField = ({ isDisabled }) => {
validate={required(t`Select a value for this field`)} validate={required(t`Select a value for this field`)}
isDisabled={isDisabled} isDisabled={isDisabled}
hideSmartInventories hideSmartInventories
autoPopulate={!inventoryField.value?.id}
/> />
); );

View File

@@ -5,6 +5,7 @@ import { t } from '@lingui/macro';
import { InventoriesAPI } from 'api'; import { InventoriesAPI } from 'api';
import { Inventory } from 'types'; import { Inventory } from 'types';
import useRequest from 'hooks/useRequest'; import useRequest from 'hooks/useRequest';
import useAutoPopulateLookup from 'hooks/useAutoPopulateLookup';
import { getQSConfig, parseQueryString, mergeParams } from 'util/qs'; import { getQSConfig, parseQueryString, mergeParams } from 'util/qs';
import Lookup from './Lookup'; import Lookup from './Lookup';
import OptionsList from '../OptionsList'; import OptionsList from '../OptionsList';
@@ -19,21 +20,24 @@ const QS_CONFIG = getQSConfig('inventory', {
}); });
function InventoryLookup({ function InventoryLookup({
value, autoPopulate,
onChange,
onBlur,
history,
required,
isPromptableField,
fieldId, fieldId,
fieldName,
hideSmartInventories,
history,
isDisabled,
isOverrideDisabled,
isPromptableField,
onBlur,
onChange,
promptId, promptId,
promptName, promptName,
isOverrideDisabled, required,
validate, validate,
fieldName, value,
isDisabled,
hideSmartInventories,
}) { }) {
const autoPopulateLookup = useAutoPopulateLookup(onChange);
const { const {
result: { result: {
inventories, inventories,
@@ -60,6 +64,10 @@ function InventoryLookup({
InventoriesAPI.readOptions(), InventoriesAPI.readOptions(),
]); ]);
if (autoPopulate) {
autoPopulateLookup(data.results);
}
return { return {
inventories: data.results, inventories: data.results,
count: data.count, count: data.count,
@@ -78,7 +86,7 @@ function InventoryLookup({
Boolean(actionsResponse.data.actions.POST) || isOverrideDisabled, Boolean(actionsResponse.data.actions.POST) || isOverrideDisabled,
}; };
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [history.location]), }, [autoPopulate, autoPopulateLookup, history.location]),
{ {
inventories: [], inventories: [],
count: 0, count: 0,
@@ -236,26 +244,28 @@ function InventoryLookup({
} }
InventoryLookup.propTypes = { InventoryLookup.propTypes = {
autoPopulate: bool,
fieldId: string, fieldId: string,
value: Inventory, fieldName: string,
hideSmartInventories: bool,
isDisabled: bool,
isOverrideDisabled: bool,
onChange: func.isRequired, onChange: func.isRequired,
required: bool, required: bool,
isOverrideDisabled: bool,
validate: func, validate: func,
fieldName: string, value: Inventory,
isDisabled: bool,
hideSmartInventories: bool,
}; };
InventoryLookup.defaultProps = { InventoryLookup.defaultProps = {
autoPopulate: false,
fieldId: 'inventory', fieldId: 'inventory',
value: null,
required: false,
isOverrideDisabled: false,
validate: () => {},
fieldName: 'inventory', fieldName: 'inventory',
isDisabled: false,
hideSmartInventories: false, hideSmartInventories: false,
isDisabled: false,
isOverrideDisabled: false,
required: false,
validate: () => {},
value: null,
}; };
export default withRouter(InventoryLookup); export default withRouter(InventoryLookup);