-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Remove EditPolicy-related GraphQL implementation
And also clean up some tests like hooks.test.js.
- Loading branch information
Showing
22 changed files
with
678 additions
and
1,912 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './graphql/mutations'; | ||
export { default as usePolicy } from './hooks/usePolicy'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.