Remove smart inventories from host form inv lookup

This commit is contained in:
mabashian
2021-06-21 13:03:31 -04:00
committed by Shane McDonald
parent cd83030668
commit 8246d4a298
3 changed files with 55 additions and 3 deletions

View File

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

View File

@@ -7,7 +7,7 @@ import { Inventory } from '../../types';
import Lookup from './Lookup'; import Lookup from './Lookup';
import OptionsList from '../OptionsList'; import OptionsList from '../OptionsList';
import useRequest from '../../util/useRequest'; import useRequest from '../../util/useRequest';
import { getQSConfig, parseQueryString } from '../../util/qs'; import { getQSConfig, parseQueryString, mergeParams } from '../../util/qs';
import LookupErrorMessage from './shared/LookupErrorMessage'; import LookupErrorMessage from './shared/LookupErrorMessage';
import FieldWithPrompt from '../FieldWithPrompt'; import FieldWithPrompt from '../FieldWithPrompt';
@@ -32,6 +32,7 @@ function InventoryLookup({
validate, validate,
fieldName, fieldName,
isDisabled, isDisabled,
hideSmartInventories,
}) { }) {
const { const {
result: { result: {
@@ -47,8 +48,15 @@ function InventoryLookup({
} = useRequest( } = useRequest(
useCallback(async () => { useCallback(async () => {
const params = parseQueryString(QS_CONFIG, history.location.search); const params = parseQueryString(QS_CONFIG, history.location.search);
const inventoryKindParams = hideSmartInventories
? { not__kind: 'smart' }
: {};
const [{ data }, actionsResponse] = await Promise.all([ const [{ data }, actionsResponse] = await Promise.all([
InventoriesAPI.read(params), InventoriesAPI.read(
mergeParams(params, {
...inventoryKindParams,
})
),
InventoriesAPI.readOptions(), InventoriesAPI.readOptions(),
]); ]);
@@ -60,7 +68,12 @@ function InventoryLookup({
).map(val => val.slice(0, -8)), ).map(val => val.slice(0, -8)),
searchableKeys: Object.keys( searchableKeys: Object.keys(
actionsResponse.data.actions?.GET || {} actionsResponse.data.actions?.GET || {}
).filter(key => actionsResponse.data.actions?.GET[key].filterable), ).filter(key => {
if (key === 'kind' && hideSmartInventories) {
return false;
}
return actionsResponse.data.actions?.GET[key].filterable;
}),
canEdit: canEdit:
Boolean(actionsResponse.data.actions.POST) || isOverrideDisabled, Boolean(actionsResponse.data.actions.POST) || isOverrideDisabled,
}; };
@@ -230,6 +243,7 @@ InventoryLookup.propTypes = {
validate: func, validate: func,
fieldName: string, fieldName: string,
isDisabled: bool, isDisabled: bool,
hideSmartInventories: bool,
}; };
InventoryLookup.defaultProps = { InventoryLookup.defaultProps = {
@@ -239,6 +253,7 @@ InventoryLookup.defaultProps = {
validate: () => {}, validate: () => {},
fieldName: 'inventory', fieldName: 'inventory',
isDisabled: false, isDisabled: false,
hideSmartInventories: false,
}; };
export default withRouter(InventoryLookup); export default withRouter(InventoryLookup);

View File

@@ -48,6 +48,42 @@ describe('InventoryLookup', () => {
}); });
wrapper.update(); wrapper.update();
expect(InventoriesAPI.read).toHaveBeenCalledTimes(1); expect(InventoriesAPI.read).toHaveBeenCalledTimes(1);
expect(InventoriesAPI.read).toHaveBeenCalledWith({
order_by: 'name',
page: 1,
page_size: 5,
role_level: 'use_role',
});
expect(wrapper.find('InventoryLookup')).toHaveLength(1);
expect(wrapper.find('Lookup').prop('isDisabled')).toBe(false);
});
test('should fetch only regular inventories when hideSmartInventories is true', async () => {
InventoriesAPI.readOptions.mockReturnValue({
data: {
actions: {
GET: {},
POST: {},
},
related_search_fields: [],
},
});
await act(async () => {
wrapper = mountWithContexts(
<Formik>
<InventoryLookup onChange={() => {}} hideSmartInventories />
</Formik>
);
});
wrapper.update();
expect(InventoriesAPI.read).toHaveBeenCalledTimes(1);
expect(InventoriesAPI.read).toHaveBeenCalledWith({
not__kind: 'smart',
order_by: 'name',
page: 1,
page_size: 5,
role_level: 'use_role',
});
expect(wrapper.find('InventoryLookup')).toHaveLength(1); expect(wrapper.find('InventoryLookup')).toHaveLength(1);
expect(wrapper.find('Lookup').prop('isDisabled')).toBe(false); expect(wrapper.find('Lookup').prop('isDisabled')).toBe(false);
}); });