Hide constructed and smart inventories in Inventory Lookup

This commit is contained in:
Marliana Lara 2023-02-22 15:30:30 -05:00 committed by Rick Elrod
parent ba9533f0e2
commit e3d167dfd1
3 changed files with 20 additions and 11 deletions

View File

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

View File

@ -1,5 +1,5 @@
import React, { useCallback, useEffect } from 'react';
import { func, bool, string } from 'prop-types';
import { func, bool, string, oneOfType, arrayOf } from 'prop-types';
import { withRouter } from 'react-router-dom';
import { t } from '@lingui/macro';
import { InventoriesAPI } from 'api';
@ -23,7 +23,7 @@ function InventoryLookup({
autoPopulate,
fieldId,
fieldName,
hideSmartInventories,
hideAdvancedInventories,
history,
isDisabled,
isPromptableField,
@ -34,6 +34,7 @@ function InventoryLookup({
required,
validate,
value,
multiple,
}) {
const autoPopulateLookup = useAutoPopulateLookup(onChange);
@ -45,8 +46,8 @@ function InventoryLookup({
} = useRequest(
useCallback(async () => {
const params = parseQueryString(QS_CONFIG, history.location.search);
const inventoryKindParams = hideSmartInventories
? { not__kind: 'smart' }
const inventoryKindParams = hideAdvancedInventories
? { not__kind: ['smart', 'constructed'] }
: {};
const [{ data }, actionsResponse] = await Promise.all([
InventoriesAPI.read(
@ -69,7 +70,10 @@ function InventoryLookup({
).map((val) => val.slice(0, -8)),
searchableKeys: Object.keys(actionsResponse.data.actions?.GET || {})
.filter((key) => {
if (['kind', 'host_filter'].includes(key) && hideSmartInventories) {
if (
['kind', 'host_filter'].includes(key) &&
hideAdvancedInventories
) {
return false;
}
return actionsResponse.data.actions?.GET[key].filterable;
@ -187,6 +191,7 @@ function InventoryLookup({
onDebounce={checkInventoryName}
fieldName={fieldName}
validate={validate}
multiple={multiple}
onBlur={onBlur}
required={required}
isLoading={isLoading}
@ -227,6 +232,10 @@ function InventoryLookup({
readOnly={!canDelete}
selectItem={(item) => dispatch({ type: 'SELECT_ITEM', item })}
deselectItem={(item) => dispatch({ type: 'DESELECT_ITEM', item })}
sortSelectedItems={(selectedItems) =>
dispatch({ type: 'SET_SELECTED_ITEMS', selectedItems })
}
isSelectedDraggable
/>
)}
/>
@ -239,19 +248,19 @@ InventoryLookup.propTypes = {
autoPopulate: bool,
fieldId: string,
fieldName: string,
hideSmartInventories: bool,
hideAdvancedInventories: bool,
isDisabled: bool,
onChange: func.isRequired,
required: bool,
validate: func,
value: Inventory,
value: oneOfType([Inventory, arrayOf(Inventory)]),
};
InventoryLookup.defaultProps = {
autoPopulate: false,
fieldId: 'inventory',
fieldName: 'inventory',
hideSmartInventories: false,
hideAdvancedInventories: false,
isDisabled: false,
required: false,
validate: () => {},

View File

@ -70,14 +70,14 @@ describe('InventoryLookup', () => {
await act(async () => {
wrapper = mountWithContexts(
<Formik>
<InventoryLookup onChange={() => {}} hideSmartInventories />
<InventoryLookup onChange={() => {}} hideAdvancedInventories />
</Formik>
);
});
wrapper.update();
expect(InventoriesAPI.read).toHaveBeenCalledTimes(1);
expect(InventoriesAPI.read).toHaveBeenCalledWith({
not__kind: 'smart',
not__kind: ['smart', 'constructed'],
order_by: 'name',
page: 1,
page_size: 5,