Skip to content

Commit

Permalink
fix(ESSNTL-4731): Detect 404 from Hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
AsToNlele committed Nov 1, 2023
1 parent 1356ed9 commit 13d68ed
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
20 changes: 18 additions & 2 deletions src/components/InventoryTable/InventoryTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -123,6 +125,8 @@ const InventoryTable = forwardRef(
: entities?.loaded
);

const [searchParams] = useSearchParams();

const controller = useRef(new AbortController());

/**
Expand All @@ -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({
Expand Down Expand Up @@ -235,7 +251,7 @@ const InventoryTable = forwardRef(
</div>
}
/>
) : !error ? (
) : !error || hasLoadEntitiesError ? (
<Fragment>
<EntityTableToolbar
{...props}
Expand Down
1 change: 1 addition & 0 deletions src/store/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ export const TOGGLE_TAG_MODAL = 'TOGGLE_TAG_MODAL';
export const CONFIG_CHANGED = 'CONFIG_CHANGED';
export const TOGGLE_DRAWER = 'TOGGLE_INVENTORY_DRAWER';
export const CLEAR_ENTITIES = 'CLEAR_ENTITIES';
export const CLEAR_ERRORS = 'CLEAR_ERRORS';
6 changes: 6 additions & 0 deletions src/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ACTION_TYPES,
CLEAR_ENTITIES,
CLEAR_ERRORS,
CLEAR_NOTIFICATIONS,
SET_INVENTORY_FILTER,
SET_PAGINATION,
Expand Down Expand Up @@ -90,3 +91,8 @@ export const clearEntitiesAction = () => ({
type: CLEAR_ENTITIES,
payload: [],
});

export const clearErrors = () => ({
type: CLEAR_ERRORS,
payload: [],
});
2 changes: 2 additions & 0 deletions src/store/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ACTION_TYPES,
CHANGE_SORT,
CLEAR_ENTITIES,
CLEAR_ERRORS,
CLEAR_FILTERS,
CONFIG_CHANGED,
ENTITIES_LOADING,
Expand Down Expand Up @@ -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 }),
};
38 changes: 21 additions & 17 deletions src/store/inventory-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 13d68ed

Please sign in to comment.