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 9590df3
Show file tree
Hide file tree
Showing 3 changed files with 26 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,9 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
const handleExtensionViewOnChange = useCallback<
PackagePolicyEditExtensionComponentProps['onChange']
>(
({ isValid, updatedPolicy }) => {
({ isValid, updatedPolicy, fetchedPackagePolicies }) => {
updatePackagePolicy(updatedPolicy);
setIsFetchedPackagePolicies(fetchedPackagePolicies);
setFormState((prevState) => {
if (prevState === 'VALID' && !isValid) {
return 'INVALID';
Expand Down Expand Up @@ -652,7 +657,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 @@ -586,14 +586,13 @@ const usePolicyTemplateInitialName = ({

const currentIntegrationName = getMaxPackageName(integration, packagePolicyListByIntegration);

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

updatePolicy({
...newPolicy,
name: currentIntegrationName,
});
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 +628,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 +743,8 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
};

const updatePolicy = useCallback(
(updatedPolicy: NewPackagePolicy) => {
onChange({ isValid, updatedPolicy });
(updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => {
onChange({ isValid, updatedPolicy, fetchedPackagePolicies });
},
[onChange, isValid]
);
Expand Down

0 comments on commit 9590df3

Please sign in to comment.