diff --git a/src/components/InventoryTable/InventoryTable.js b/src/components/InventoryTable/InventoryTable.js index 440a0a73f..3c01c31b1 100644 --- a/src/components/InventoryTable/InventoryTable.js +++ b/src/components/InventoryTable/InventoryTable.js @@ -17,8 +17,10 @@ import Pagination from './Pagination'; import AccessDenied from '../../Utilities/AccessDenied'; import { loadSystems } from '../../Utilities/sharedFunctions'; import isEqual from 'lodash/isEqual'; -import { entitiesLoading } from '../../store/actions'; +import { clearErrors, entitiesLoading } from '../../store/actions'; import cloneDeep from 'lodash/cloneDeep'; +import { useSearchParams } from 'react-router-dom'; +import { ACTION_TYPES } from '../../store/action-types'; /** * A helper function to store props and to always return the latest state. @@ -123,6 +125,8 @@ const InventoryTable = forwardRef( : entities?.loaded ); + const [searchParams] = useSearchParams(); + const controller = useRef(new AbortController()); /** @@ -146,6 +150,18 @@ const InventoryTable = forwardRef( abortOnUnmount && controller.current.abort(); }; }, []); + const hasLoadEntitiesError = + error?.status === 404 && + error?.type === ACTION_TYPES.LOAD_ENTITIES && + parseInt(searchParams.get('page')) !== 1; + useEffect(() => { + if (error) { + if (hasLoadEntitiesError) { + onRefreshData({ page: 1 }); + dispatch(clearErrors()); + } + } + }, [error]); const cache = useRef(inventoryCache()); cache.current.updateProps({ @@ -235,7 +251,7 @@ const InventoryTable = forwardRef( } /> - ) : !error ? ( + ) : !error || hasLoadEntitiesError ? ( ({ type: CLEAR_ENTITIES, payload: [], }); + +export const clearErrors = () => ({ + type: CLEAR_ERRORS, + payload: [], +}); diff --git a/src/store/entities.js b/src/store/entities.js index bf5fd83e0..82f820c64 100644 --- a/src/store/entities.js +++ b/src/store/entities.js @@ -3,6 +3,7 @@ import { ACTION_TYPES, CHANGE_SORT, CLEAR_ENTITIES, + CLEAR_ERRORS, CLEAR_FILTERS, CONFIG_CHANGED, ENTITIES_LOADING, @@ -434,4 +435,5 @@ export default { }), [ACTION_TYPES.GROUPS_FOR_ENTITIES_FULFILLED]: (state, action) => groupsLoaded(state, { payload: { ...action.payload } }), + [CLEAR_ERRORS]: (state) => ({ ...state, error: null }), }; diff --git a/src/store/inventory-actions.js b/src/store/inventory-actions.js index 47f203f09..cad64dbcc 100644 --- a/src/store/inventory-actions.js +++ b/src/store/inventory-actions.js @@ -83,23 +83,27 @@ export const loadEntities = ( }, showTags, defaultGetEntities - ).then(({ results, ...data }) => ({ - ...data, - filters, - sortBy: { key: orderBy, direction: orderDirection }, - results: - items.length > 0 - ? items.map((item) => ({ - ...(item.id ? item : { id: item }), - ...(results.find(({ id }) => id === item || id === item.id) || - {}), - })) - : results, - page: config.page || data?.page, - // eslint-disable-next-line camelcase - per_page: config.per_page || data?.per_page, - hideFilters: config.hideFilters, - })), + ) + .then(({ results, ...data }) => ({ + ...data, + filters, + sortBy: { key: orderBy, direction: orderDirection }, + results: + items.length > 0 + ? items.map((item) => ({ + ...(item.id ? item : { id: item }), + ...(results.find(({ id }) => id === item || id === item.id) || + {}), + })) + : results, + page: config.page || data?.page, + // eslint-disable-next-line camelcase + per_page: config.per_page || data?.per_page, + hideFilters: config.hideFilters, + })) + .catch((error) => { + throw { ...error, type: 'LOAD_ENTITIES' }; + }), meta: { showTags, lastDateRequest,