Skip to content

Commit

Permalink
chore: Remove EditPolicy-related GraphQL implementation
Browse files Browse the repository at this point in the history
And also clean up some tests like hooks.test.js.
  • Loading branch information
gkarat committed Jan 8, 2025
1 parent abdb28c commit 1a4e29e
Show file tree
Hide file tree
Showing 22 changed files with 678 additions and 1,912 deletions.
1 change: 0 additions & 1 deletion src/Mutations/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './graphql/mutations';
export { default as usePolicy } from './hooks/usePolicy';
174 changes: 157 additions & 17 deletions src/SmartComponents/EditPolicy/EditPolicy.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,161 @@
import React from 'react';
import { Bullseye, Spinner } from '@patternfly/react-core';
import useAPIV2FeatureFlag from '@/Utilities/hooks/useAPIV2FeatureFlag';
import { EditPolicyGraphQL } from './EditPolicyGraphQL';
import { EditPolicyRest } from './EditPolicyRest';

const EditPolicy = (props) => {
const apiV2Enabled = useAPIV2FeatureFlag();

return apiV2Enabled === undefined ? (
<Bullseye>
<Spinner />
</Bullseye>
) : apiV2Enabled === true ? (
<EditPolicyRest {...props} />
) : (
<EditPolicyGraphQL {...props} />
import React, { useState } from 'react';
import propTypes from 'prop-types';
import { Button, Spinner } from '@patternfly/react-core';
import { useLocation, useParams } from 'react-router-dom';
import useNavigate from '@redhat-cloud-services/frontend-components-utilities/useInsightsNavigate';
import { useTitleEntity } from 'Utilities/hooks/useDocumentTitle';
import {
ComplianceModal,
StateViewWithError,
StateViewPart,
} from 'PresentationalComponents';
import EditPolicyForm from './EditPolicyForm';
import { useOnSave } from './hooks';
import usePolicy from 'Utilities/hooks/api/usePolicy';
import useAssignedRules from './hooks/useAssignedRules';
import useAssignedSystems from './hooks/useAssignedSystems';
import useSupportedProfiles from 'Utilities/hooks/api/useSupportedProfiles';

export const EditPolicy = ({ route }) => {
const navigate = useNavigate();
const { policy_id: policyId } = useParams();
const location = useLocation();
const {
data: { data: policy } = {},
loading: policyLoading,
error: policyError,
} = usePolicy(policyId);

const {
data: { data: supportedProfiles } = {},
error: supportedProfilesError,
loading: supportedProfilesLoading,
} = useSupportedProfiles({
params: {
filter: `os_major_version=${policy?.os_major_version}`,
limit: 100,
},
skip: policyLoading || !policy?.os_major_version,
});

const securityGuide = supportedProfiles?.find(
(profile) => profile.ref_id === policy?.ref_id
);

const supportedOsVersions = securityGuide?.os_minor_versions || [];

const { assignedRuleIds, assignedRulesLoading } = useAssignedRules(policyId);
const { assignedSystems, assignedSystemsLoading } = useAssignedSystems(
policyId,
policy,
policyLoading
);

const [updatedPolicy, setUpdatedPolicy] = useState(null);

const saveEnabled = !updatedPolicy;

const onSaveCallback = (isClose) =>
navigate(
isClose ? `/scappolicies/${policyId}` : location.state?.returnTo || -1
);

const [isSaving, onSave] = useOnSave(policyId, updatedPolicy, {
onSave: onSaveCallback,
onError: onSaveCallback,
});

const setRuleValues = (
_policy,
tailoring,
valueDefinition,
newValue,
closeInlineEdit
) => {
setUpdatedPolicy((prev) => ({
...prev,
value: {
...prev.value,
[tailoring.id]: {
...tailoring.value_overrides,
[valueDefinition.id]: newValue,
},
},
}));

closeInlineEdit();
};
const actions = [
<Button
isDisabled={saveEnabled}
key="save"
ouiaId="EditPolicySaveButton"
variant="primary"
spinnerAriaValueText="Saving"
isLoading={isSaving}
onClick={onSave}
>
Save
</Button>,
<Button
key="cancel"
ouiaId="EditPolicyCancelButton"
variant="link"
onClick={() => onSaveCallback(true)}
>
Cancel
</Button>,
];

useTitleEntity(route, policy?.title);

const statusValues = {
data: policy && supportedProfiles && assignedRuleIds,
loading:
policyLoading ||
supportedProfilesLoading ||
assignedSystemsLoading ||
assignedRulesLoading,
error: policyError || supportedProfilesError,
};

return (
<ComplianceModal
isOpen
position={'top'}
style={{ minHeight: '350px' }}
width={1220}
variant={'large'}
ouiaId="EditPolicyModal"
title={`Edit ${policy?.title || ''}`}
onClose={() => onSaveCallback(true)}
actions={actions}
>
<StateViewWithError stateValues={statusValues}>
<StateViewPart stateKey="loading">
<Spinner />
</StateViewPart>
<StateViewPart stateKey="data">
<EditPolicyForm
{...{
policy,
updatedPolicy,
setUpdatedPolicy,
assignedRuleIds,
assignedSystems,
setRuleValues,
supportedOsVersions,
securityGuide,
}}
/>
</StateViewPart>
</StateViewWithError>
</ComplianceModal>
);
};

EditPolicy.propTypes = {
route: propTypes.object,
};

export default EditPolicy;
Loading

0 comments on commit 1a4e29e

Please sign in to comment.