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} 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 hideAdvancedInventories
autoPopulate={!inventoryField.value?.id} autoPopulate={!inventoryField.value?.id}
/> />
); );

View File

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

View File

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