Compliance: refactor clearPackage duplicate

This commit is contained in:
Anna Vítová 2025-06-05 14:50:32 +02:00 committed by Gianluca Zuccarelli
parent d5877b256c
commit c026102dd3
3 changed files with 26 additions and 17 deletions

View file

@ -9,6 +9,8 @@ import {
} from '@patternfly/react-core'; } from '@patternfly/react-core';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { useSelectorHandlers } from './useSelectorHandlers';
import { import {
PolicyRead, PolicyRead,
usePoliciesQuery, usePoliciesQuery,
@ -91,6 +93,7 @@ const PolicySelector = () => {
const hasWslTargetOnly = useHasSpecificTargetOnly('wsl'); const hasWslTargetOnly = useHasSpecificTargetOnly('wsl');
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const { clearCompliancePackages } = useSelectorHandlers();
const { const {
data: policies, data: policies,
@ -143,7 +146,7 @@ const PolicySelector = () => {
policyTitle: undefined, policyTitle: undefined,
}) })
); );
clearOscapPackages(currentProfileData?.packages || []); clearCompliancePackages(currentProfileData?.packages || []);
dispatch(changeFileSystemConfigurationType('automatic')); dispatch(changeFileSystemConfigurationType('automatic'));
handleServices(undefined); handleServices(undefined);
dispatch(clearKernelAppend()); dispatch(clearKernelAppend());
@ -153,7 +156,7 @@ const PolicySelector = () => {
oldOscapPackages: string[], oldOscapPackages: string[],
newOscapPackages: string[] newOscapPackages: string[]
) => { ) => {
clearOscapPackages(oldOscapPackages); clearCompliancePackages(oldOscapPackages);
for (const pkg of newOscapPackages) { for (const pkg of newOscapPackages) {
dispatch( dispatch(
@ -166,12 +169,6 @@ const PolicySelector = () => {
} }
}; };
const clearOscapPackages = (oscapPackages: string[]) => {
for (const pkg of oscapPackages) {
dispatch(removePackage(pkg));
}
};
const handlePartitions = (oscapPartitions: Filesystem[]) => { const handlePartitions = (oscapPartitions: Filesystem[]) => {
dispatch(clearPartitions()); dispatch(clearPartitions());

View file

@ -17,6 +17,8 @@ import {
import { TimesIcon } from '@patternfly/react-icons'; import { TimesIcon } from '@patternfly/react-icons';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { useSelectorHandlers } from './useSelectorHandlers';
import { import {
useBackendPrefetch, useBackendPrefetch,
useGetOscapCustomizationsQuery, useGetOscapCustomizationsQuery,
@ -42,7 +44,6 @@ import {
changeMaskedServices, changeMaskedServices,
clearKernelAppend, clearKernelAppend,
clearPartitions, clearPartitions,
removePackage,
selectComplianceProfileID, selectComplianceProfileID,
selectComplianceType, selectComplianceType,
selectDistribution, selectDistribution,
@ -81,6 +82,7 @@ const ProfileSelector = () => {
>([]); >([]);
const complianceType = useAppSelector(selectComplianceType); const complianceType = useAppSelector(selectComplianceType);
const prefetchProfile = useBackendPrefetch('getOscapCustomizations'); const prefetchProfile = useBackendPrefetch('getOscapCustomizations');
const { clearCompliancePackages } = useSelectorHandlers();
const { const {
data: profiles, data: profiles,
@ -181,7 +183,7 @@ const ProfileSelector = () => {
policyTitle: undefined, policyTitle: undefined,
}) })
); );
clearOscapPackages(currentProfileData?.packages || []); clearCompliancePackages(currentProfileData?.packages || []);
dispatch(changeFileSystemConfigurationType('automatic')); dispatch(changeFileSystemConfigurationType('automatic'));
handleServices(undefined); handleServices(undefined);
dispatch(clearKernelAppend()); dispatch(clearKernelAppend());
@ -193,7 +195,7 @@ const ProfileSelector = () => {
oldOscapPackages: string[], oldOscapPackages: string[],
newOscapPackages: string[] newOscapPackages: string[]
) => { ) => {
clearOscapPackages(oldOscapPackages); clearCompliancePackages(oldOscapPackages);
for (const pkg of newOscapPackages) { for (const pkg of newOscapPackages) {
dispatch( dispatch(
@ -206,12 +208,6 @@ const ProfileSelector = () => {
} }
}; };
const clearOscapPackages = (oscapPackages: string[]) => {
for (const pkg of oscapPackages) {
dispatch(removePackage(pkg));
}
};
const handlePartitions = (oscapPartitions: Filesystem[]) => { const handlePartitions = (oscapPartitions: Filesystem[]) => {
dispatch(clearPartitions()); dispatch(clearPartitions());

View file

@ -0,0 +1,16 @@
import { useAppDispatch } from '../../../../../store/hooks';
import { removePackage } from '../../../../../store/wizardSlice';
export const useSelectorHandlers = () => {
const dispatch = useAppDispatch();
const clearCompliancePackages = (oscapPackages: string[]) => {
for (const pkg of oscapPackages) {
dispatch(removePackage(pkg));
}
};
return {
clearCompliancePackages,
};
};