Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(RHINENG-6147): Paginated requests #1163

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import PatchSetWizard from '../../SmartComponents/PatchSetWizard/PatchSetWizard'
import UnassignSystemsModal from '../../SmartComponents/Modals/UnassignSystemsModal';
import AssignSystemsModal from '../../SmartComponents/Modals/AssignSystemsModal';

const PatchSetWrapper = ({ patchSetState, setPatchSetState }) => {
const PatchSetWrapper = ({ patchSetState, setPatchSetState, totalItems }) => {
return (<>
{(patchSetState.isUnassignSystemsModalOpen) && <UnassignSystemsModal
unassignSystemsModalState={patchSetState}
setUnassignSystemsModalOpen={setPatchSetState}
systemsIDs={patchSetState.systemsIDs}
totalItems={totalItems}
/>}
<AssignSystemsModal
patchSetState={patchSetState}
setPatchSetState={setPatchSetState}
systemsIDs={patchSetState.systemsIDs}
totalItems={totalItems}
/>
{(patchSetState.isPatchSetWizardOpen) &&
<PatchSetWizard systemsIDs={patchSetState.systemsIDs} setBaselineState={setPatchSetState} />}
Expand All @@ -24,6 +26,7 @@ const PatchSetWrapper = ({ patchSetState, setPatchSetState }) => {

PatchSetWrapper.propTypes = {
patchSetState: propTypes.object,
setPatchSetState: propTypes.func
setPatchSetState: propTypes.func,
totalItems: propTypes.number
};
export default PatchSetWrapper;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { mountWithRouterAndProviderAndIntl } from '../../../config/rtlwrapper';

jest.mock('../../SmartComponents/PatchSetWizard/PatchSetWizard', () => () => <div id={'test-patch-set-wizard'}></div>);
jest.mock('../../SmartComponents/Modals/UnassignSystemsModal', () => () => <div id={'test-unassign-systems-modal'}></div>);
jest.mock('../../SmartComponents/Modals/AssignSystemsModal', () => () => <div id={'test-assign-systems-modal'}></div>);

const mockState = {};

Expand All @@ -28,7 +29,8 @@ const testProps = {
isPatchSetWizardOpen: true,
systemsIDs: ['system-1', 'system-2']
},
setPatchSetState: jest.fn()
setPatchSetState: jest.fn(),
totalItems: 101
};
describe('PatchSetWrapper', () => {
it('should display PatchSetWizard when isPatchSetWizardOpen prop is true', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/Advisories/Advisories.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ const Advisories = () => {
{
endpoint: ID_API_ENDPOINTS.advisories,
queryParams,
selectionDispatcher: selectAdvisoryRow
selectionDispatcher: selectAdvisoryRow,
totalItems: metadata?.total_items
gkarat marked this conversation as resolved.
Show resolved Hide resolved
}
);

Expand Down
4 changes: 2 additions & 2 deletions src/SmartComponents/Advisories/Advisories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jest.mock('../../Utilities/api', () => ({
exportAdvisoriesCSV: jest.fn(() => Promise.resolve({ success: true }).catch((err) => console.log(err))),
fetchSystems: jest.fn(() => Promise.resolve({ data: { id: 'testId' } }).catch((err) => console.log(err))),
fetchViewAdvisoriesSystems: jest.fn(() => Promise.resolve({ success: true }).catch((err) => console.log(err))),
fetchIDs: jest.fn(() => Promise.resolve({ ids: [] }).catch((err) => console.log(err))),
fetchIDs: jest.fn(() => Promise.resolve({ data: [{ id: 'test_id-1' }] }).catch((err) => console.log(err))),
fetchApplicableAdvisoriesApi: jest.fn(() => Promise.resolve({ success: true }).catch((err) => console.log(err)))
}));

Expand Down Expand Up @@ -52,7 +52,7 @@ const mockState = { ...storeListDefaults,
metadata: {
limit: 25,
offset: 0,
total_items: 10
total_items: 101
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/SmartComponents/AdvisorySystems/AdvisorySystem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ initMocks();

jest.mock('../../Utilities/api', () => ({
...jest.requireActual('../../Utilities/api'),
fetchIDs: jest.fn(() => Promise.resolve({ ids: [] }).catch((err) => console.log(err)))
fetchIDs: jest.fn(() => Promise.resolve({ data: [{ id: 'test_id-1' }] }).catch((err) => console.log(err)))
}));

jest.mock(
Expand All @@ -30,7 +30,7 @@ const mockState = {
selectedRows: { 'test-system-1': true },
error: {},
status: 'resolved',
total: 2
total: 101
},
AdvisorySystemsStore: {
queryParams: {}
Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/AdvisorySystems/AdvisorySystems.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ const AdvisorySystems = ({ advisoryName }) => {
{
endpoint: ID_API_ENDPOINTS.advisorySystems(advisoryName),
queryParams,
selectionDispatcher: systemSelectAction
selectionDispatcher: systemSelectAction,
totalItems
}
);

Expand Down
17 changes: 12 additions & 5 deletions src/SmartComponents/Modals/AssignSystemsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import { addNotification } from '@redhat-cloud-services/frontend-components-noti
import { patchSetAssignSystemsNotifications } from '../PatchSet/PatchSetAssets';
import { filterSelectedActiveSystemIDs } from '../../Utilities/Helpers';
import { filterSatelliteManagedSystems } from './Helpers';
import { useFetchBatched } from '../../Utilities/hooks';
import isEmpty from 'lodash/isEmpty';

const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl }) => {
const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl, totalItems }) => {
const dispatch = useDispatch();

const { systemsIDs, isAssignSystemsModalOpen } = patchSetState;
Expand All @@ -20,6 +22,7 @@ const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl }) => {

const [systemsNotManagedBySatellite, setSystemsNotManagedBySatellite] = useState([]);
const [systemsLoading, setSystemsLoading] = useState(true);
const { fetchBatched } = useFetchBatched();

const closeModal = () => {
setPatchSetState({
Expand Down Expand Up @@ -66,10 +69,13 @@ const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl }) => {
};

useEffect(() => {
if (systemsIDs) {
if (systemsIDs && !isEmpty(systemsIDs)) {
setSystemsLoading(true);

filterSatelliteManagedSystems(Object.keys(systemsIDs)).then(result => {
filterSatelliteManagedSystems(
Object.keys(systemsIDs),
fetchBatched,
totalItems
).then(result => {
setSystemsNotManagedBySatellite(result);
setSystemsLoading(false);
});
Expand Down Expand Up @@ -144,7 +150,8 @@ const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl }) => {
AssignSystemsModal.propTypes = {
intl: propTypes.any,
setPatchSetState: propTypes.func,
patchSetState: propTypes.object
patchSetState: propTypes.object,
totalItems: propTypes.number
};

export default injectIntl(AssignSystemsModal);
43 changes: 26 additions & 17 deletions src/SmartComponents/Modals/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@ import React from 'react';
import { GridItem } from '@patternfly/react-core';

import messages from '../../Messages';
import { fetchIDs, fetchSystems } from '../../Utilities/api';
import { fetchIDs } from '../../Utilities/api';

export const filterSystemsWithoutSets = (systemsIDs) => {
return fetchSystems({
limit: -1, 'filter[baseline_name]': 'neq:',
filter: { stale: [true, false] }
}).then((allSystemsWithPatchSet) => {
return systemsIDs.filter(systemID =>
allSystemsWithPatchSet?.data?.some(system => system.id === systemID)
const filterChosenSystems = (urlFilter, systemsIDs, fetchBatched, totalItems) => {
return fetchBatched(
(filter) => fetchIDs(
'/ids/systems',
filter
),
{
...urlFilter,
filter: { stale: [true, false] }
},
totalItems,
100
).then((systemsNotManagedBySatellite) => {
const aggregatedResult = systemsNotManagedBySatellite.flatMap(({ data }) => data);
return systemsIDs.filter(systemID =>{
return aggregatedResult?.some(system => system.id === systemID);
}
);
});
};

export const filterSatelliteManagedSystems = (systemsIDs) => {
return fetchIDs('/ids/systems', {
limit: -1, 'filter[satellite_managed]': 'false',
filter: { stale: [true, false] }
}).then((systemsNotManagedBySatellite) => {
return systemsIDs.filter(systemID =>
systemsNotManagedBySatellite?.data?.some(system => system.id === systemID)
);
});
export const filterSystemsWithoutSets = (systemsIDs, fetchBatched, totalItems) => {
const urlFilter = { 'filter[baseline_name]': 'neq:' };
return filterChosenSystems(urlFilter, systemsIDs, fetchBatched, totalItems);
};

export const filterSatelliteManagedSystems = (systemsIDs, fetchBatched, totalItems) => {
const urlFilter = { 'filter[satellite_managed]': 'false' };
return filterChosenSystems(urlFilter, systemsIDs, fetchBatched, totalItems);
};

export const renderUnassignModalMessages = (bodyMessage, systemsCount, intl) => (<GridItem>
Expand Down
14 changes: 11 additions & 3 deletions src/SmartComponents/Modals/UnassignSystemsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { injectIntl } from 'react-intl';
import messages from '../../Messages';
import { useUnassignSystemsHook } from './useUnassignSystemsHook';
import { renderUnassignModalMessages, filterSystemsWithoutSets } from './Helpers';
import { useFetchBatched } from '../../Utilities/hooks';

const UnassignSystemsModal = ({ unassignSystemsModalState = {}, setUnassignSystemsModalOpen, intl }) => {
const UnassignSystemsModal = ({ unassignSystemsModalState = {}, setUnassignSystemsModalOpen, intl, totalItems }) => {
const { systemsIDs, isUnassignSystemsModalOpen } = unassignSystemsModalState;
const [systemsWithPatchSet, setSystemWithPatchSet] = useState([]);
const [systemsLoading, setSystemsLoading] = useState(true);
const { fetchBatched } = useFetchBatched();

const handleModalToggle = (shouldRefresh) => {
setUnassignSystemsModalOpen({
Expand All @@ -29,7 +31,12 @@ const UnassignSystemsModal = ({ unassignSystemsModalState = {}, setUnassignSyste
useEffect(() => {
setSystemsLoading(true);

filterSystemsWithoutSets(systemsIDs).then(result => {
filterSystemsWithoutSets(
systemsIDs,
fetchBatched,
totalItems
)
.then(result => {
setSystemWithPatchSet(result);
setSystemsLoading(false);
});
Expand Down Expand Up @@ -82,6 +89,7 @@ const UnassignSystemsModal = ({ unassignSystemsModalState = {}, setUnassignSyste
UnassignSystemsModal.propTypes = {
intl: propTypes.any,
setUnassignSystemsModalOpen: propTypes.func,
unassignSystemsModalState: propTypes.object
unassignSystemsModalState: propTypes.object,
totalItems: propTypes.number
};
export default injectIntl(UnassignSystemsModal);
6 changes: 2 additions & 4 deletions src/SmartComponents/Modals/UnassignSystemsModal.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addNotification } from '@redhat-cloud-services/frontend-components-notifications/redux';
import UnassignSystemsModal from './UnassignSystemsModal';
import { unassignSystemFromPatchSet, fetchSystems } from '../../Utilities/api';
import { unassignSystemFromPatchSet } from '../../Utilities/api';
import { initMocks } from '../../Utilities/unitTestingUtilities';
import { patchSetUnassignSystemsNotifications } from '../PatchSet/PatchSetAssets';
import { render, waitFor, screen } from '@testing-library/react';
Expand All @@ -12,7 +12,7 @@ initMocks();
jest.mock('../../Utilities/api', () => ({
...jest.requireActual('../../Utilities/api'),
unassignSystemFromPatchSet: jest.fn(),
fetchSystems: jest.fn()
fetchIDs: jest.fn(() => Promise.resolve({ data: [{ id: 'test_1' }] }))
}));

jest.mock('react-redux', () => ({
Expand All @@ -23,8 +23,6 @@ jest.mock('@redhat-cloud-services/frontend-components-notifications/redux', () =
addNotification: jest.fn(() => {})
}));

fetchSystems.mockResolvedValue({ data: [{ id: 'test_1' }] });

let unassignSystemsModalState = {
isUnassignSystemsModalOpen: true,
systemsIDs: ['test_1', 'test_2', 'test_3']
Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/PackageSystems/PackageSystems.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ const PackageSystems = ({ packageName }) => {
queryParams,
selectionDispatcher: systemSelectAction,
constructFilename,
apiResponseTransformer: filterRemediatablePackageSystems
apiResponseTransformer: filterRemediatablePackageSystems,
totalItems
}
);

Expand Down
13 changes: 6 additions & 7 deletions src/SmartComponents/PackageSystems/PackageSystems.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ jest.mock('../../Utilities/api', () => ({
fetchPackageVersions: jest.fn(() => Promise.resolve({ success: true }).catch((err) => console.log(err))),
fetchIDs: jest.fn(() => Promise.resolve({
data: [{
attributes: {
advisory_type: 2,
description: 'The tzdata penhancements.',
public_date: '2020-10-19T15:02:38Z',
synopsis: 'tzdata enhancement update'
},
advisory_type: 2,
description: 'The tzdata penhancements.',
public_date: '2020-10-19T15:02:38Z',
synopsis: 'tzdata enhancement update',
updatable: true,
id: 'RHBA-2020:4282',
type: 'advisory'
}]
Expand All @@ -47,7 +46,7 @@ const mockState = {
selectedRows: { 'test-system-1': 'packageEvra' },
error: {},
status: 'resolved',
total: 2
total: 101
},
PackageSystemsStore: {
queryParams: {}
Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/PatchSet/PatchSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ const PatchSet = () => {
{
endpoint: ID_API_ENDPOINTS.templates,
queryParams,
selectionDispatcher: selectPatchSetRow
selectionDispatcher: selectPatchSetRow,
totalItems: metadata.total_items
}
);

Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/PatchSetDetail/PatchSetDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ const PatchSetDetail = () => {
{
endpoint: ID_API_ENDPOINTS.templateSystems(patchSetId),
queryParams,
selectionDispatcher: systemSelectAction
selectionDispatcher: systemSelectAction,
totalItems
}
);

Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/PatchSetWizard/steps/ReviewSystems.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export const ReviewSystems = ({ systemsIDs = [], ...props }) => {
satellite_managed: false
}
},
customSelector: selectRows
customSelector: selectRows,
totalItems: metadata.total_items
}
);
return (
Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/SystemAdvisories/SystemAdvisories.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ const SystemAdvisories = ({ handleNoSystemData, inventoryId, shouldRefresh }) =>
endpoint: ID_API_ENDPOINTS.systemAdvisories(inventoryId),
queryParams,
selectionDispatcher: selectSystemAdvisoryRow,
constructFilename
constructFilename,
totalItems: metadata?.total_items
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const mockState = {
metadata: {
limit: 25,
offset: 0,
total_items: 10
total_items: 101
},
expandedRows: {},
selectedRows: {},
Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/SystemPackages/SystemPackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ const SystemPackages = ({ handleNoSystemData, inventoryId, shouldRefresh }) => {
queryParams,
selectionDispatcher: selectSystemPackagesRow,
constructFilename,
transformKey
transformKey,
totalItems: metadata?.total_items
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const mockState = {
metadata: {
limit: 25,
offset: 0,
total_items: 10
total_items: 101
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/SmartComponents/Systems/SystemsListAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ export const useActivateRemediationModal = (setRemediationIssues, setRemediation
);

fetchBatched(
(__, pagination) => fetchApplicableSystemAdvisoriesApi({ ...filter, ...pagination }),
totalCount,
filter
(filterWithPagination) => fetchApplicableSystemAdvisoriesApi(filterWithPagination),
filter,
totalCount
).then(response => {
const advisories = response.flatMap(({ data }) => data);
const remediationIssues = remediationProvider(
Expand Down
Loading