Skip to content

Commit

Permalink
add isFetchedPackagePolicies state to wait for policies to load befor…
Browse files Browse the repository at this point in the history
…e enabling save button
  • Loading branch information
alexreal1314 committed Feb 18, 2025
1 parent 148d47c commit 6c3a9e4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
);

const [withSysMonitoring, setWithSysMonitoring] = useState<boolean>(true);
const [isFetchedPackagePolicies, setIsFetchedPackagePolicies] = useState<boolean | undefined>(
undefined
);

const validation = agentPolicyFormValidation(newAgentPolicy, {
allowedNamespacePrefixes: spaceSettings.allowedNamespacePrefixes,
});
Expand Down Expand Up @@ -266,8 +270,13 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
const handleExtensionViewOnChange = useCallback<
PackagePolicyEditExtensionComponentProps['onChange']
>(
({ isValid, updatedPolicy }) => {
({ isValid, updatedPolicy, fetchedPackagePolicies }) => {
// console.log("isValid1 ", isValid);
// console.log("updatedPolicy2 ", updatedPolicy);
// console.log("fetchedPackagePolicies2 ", fetchedPackagePolicies);

updatePackagePolicy(updatedPolicy);
setIsFetchedPackagePolicies(fetchedPackagePolicies);
setFormState((prevState) => {
if (prevState === 'VALID' && !isValid) {
return 'INVALID';
Expand Down Expand Up @@ -652,7 +661,10 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
onClick={() => onSubmit()}
isLoading={formState === 'LOADING'}
disabled={
formState !== 'VALID' || hasAgentPolicyError || !validationResults
formState !== 'VALID' ||
hasAgentPolicyError ||
!validationResults ||
isFetchedPackagePolicies === false
}
iconType="save"
color="primary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface PackagePolicyEditExtensionComponentProps {
isValid: boolean;
/** The updated Integration Policy to be merged back and included in the API call */
updatedPolicy: Partial<NewPackagePolicy>;
fetchedPackagePolicies?: boolean;
}) => void;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const AwsAccountTypeSelect = ({
}: {
input: Extract<NewPackagePolicyPostureInput, { type: 'cloudbeat/cis_aws' }>;
newPolicy: NewPackagePolicy;
updatePolicy: (updatedPolicy: NewPackagePolicy) => void;
updatePolicy: (updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
packageInfo: PackageInfo;
disabled: boolean;
}) => {
Expand Down Expand Up @@ -303,7 +303,7 @@ const GcpAccountTypeSelect = ({
}: {
input: Extract<NewPackagePolicyPostureInput, { type: 'cloudbeat/cis_gcp' }>;
newPolicy: NewPackagePolicy;
updatePolicy: (updatedPolicy: NewPackagePolicy) => void;
updatePolicy: (updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
packageInfo: PackageInfo;
disabled: boolean;
}) => {
Expand Down Expand Up @@ -443,7 +443,7 @@ const AzureAccountTypeSelect = ({
}: {
input: Extract<NewPackagePolicyPostureInput, { type: 'cloudbeat/cis_azure' }>;
newPolicy: NewPackagePolicy;
updatePolicy: (updatedPolicy: NewPackagePolicy) => void;
updatePolicy: (updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
disabled: boolean;
packageInfo: PackageInfo;
setupTechnology: SetupTechnology;
Expand Down Expand Up @@ -548,7 +548,7 @@ const useEnsureDefaultNamespace = ({
}: {
newPolicy: NewPackagePolicy;
input: NewPackagePolicyPostureInput;
updatePolicy: (policy: NewPackagePolicy) => void;
updatePolicy: (policy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
}) => {
useEffect(() => {
if (newPolicy.namespace === POSTURE_NAMESPACE) return;
Expand All @@ -572,7 +572,7 @@ const usePolicyTemplateInitialName = ({
integration: CloudSecurityPolicyTemplate | undefined;
newPolicy: NewPackagePolicy;
packagePolicyList: PackagePolicy[] | undefined;
updatePolicy: (policy: NewPackagePolicy) => void;
updatePolicy: (policy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
setCanFetchIntegration: (canFetch: boolean) => void;
}) => {
useEffect(() => {
Expand All @@ -583,17 +583,35 @@ const usePolicyTemplateInitialName = ({
const packagePolicyListByIntegration = packagePolicyList?.filter(
(policy) => policy?.vars?.posture?.value === integration
);
// console.log("keta123");
// console.log("integration123 ", integration);
// console.log("packagePolicyListByIntegration123 ", packagePolicyListByIntegration);

const currentIntegrationName = getMaxPackageName(integration, packagePolicyListByIntegration);

if (newPolicy.name === currentIntegrationName) {
return;
}

updatePolicy({
...newPolicy,
name: currentIntegrationName,
});
// if (newPolicy.name === currentIntegrationName) {
// // console.log("newPolicy.name ", newPolicy.name);
// // console.log("currentIntegrationName ", currentIntegrationName);
// updatePolicy(
// {
// ...newPolicy,
// name: currentIntegrationName,
// },
// Array.isArray(packagePolicyListByIntegration)
// );
// return;
// }
// console.log(
// 'Array.isArray(packagePolicyListByIntegration ',
// Array.isArray(packagePolicyListByIntegration)
// );
updatePolicy(
{
...newPolicy,
name: currentIntegrationName,
},
Array.isArray(packagePolicyListByIntegration)
);
setCanFetchIntegration(false);
// since this useEffect should only run on initial mount updatePolicy and newPolicy shouldn't re-trigger it
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -629,7 +647,7 @@ const useCloudFormationTemplate = ({
}: {
packageInfo: PackageInfo;
newPolicy: NewPackagePolicy;
updatePolicy: (policy: NewPackagePolicy) => void;
updatePolicy: (policy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
}) => {
useEffect(() => {
const templateUrl = getVulnMgmtCloudFormationDefaultValue(packageInfo);
Expand Down Expand Up @@ -744,8 +762,12 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
};

const updatePolicy = useCallback(
(updatedPolicy: NewPackagePolicy) => {
onChange({ isValid, updatedPolicy });
(updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => {
// console.log("updatedPolicyy ", updatedPolicy);
// console.log("isValid ", isValid);
// console.log("fetchedPackagePolicies1 ", fetchedPackagePolicies);

onChange({ isValid, updatedPolicy, fetchedPackagePolicies });
},
[onChange, isValid]
);
Expand Down

0 comments on commit 6c3a9e4

Please sign in to comment.