Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Auto save for approval and governance #264

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import type * as SCHEMA from "@ctrlplane/db/schema";
import React from "react";
import { z } from "zod";

import { Button } from "@ctrlplane/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
useForm,
} from "@ctrlplane/ui/form";
import { Input } from "@ctrlplane/ui/input";
import { Label } from "@ctrlplane/ui/label";
import { RadioGroup, RadioGroupItem } from "@ctrlplane/ui/radio-group";
import {
Select,
Expand All @@ -24,141 +14,116 @@ import {

import { api } from "~/trpc/react";

const schema = z.object({
approvalRequirement: z.enum(["automatic", "manual"]),
successType: z.enum(["all", "some", "optional"]),
successMinimum: z.number().min(0, "Must be a positive number"),
});

export const ApprovalAndGovernance: React.FC<{
environmentPolicy: SCHEMA.EnvironmentPolicy;
}> = ({ environmentPolicy }) => {
const form = useForm({ schema, defaultValues: { ...environmentPolicy } });
const { successMinimum } = form.watch();

const updatePolicy = api.environment.policy.update.useMutation();
const utils = api.useUtils();

const { id, systemId } = environmentPolicy;
const onSubmit = form.handleSubmit((data) =>
updatePolicy
.mutateAsync({ id, data })
.then(() => form.reset(data))
.then(() => utils.environment.policy.byId.invalidate(id))
.then(() => utils.environment.policy.bySystemId.invalidate(systemId)),
);
const invalidatePolicy = () => {
utils.environment.policy.byId.invalidate(id);
utils.environment.policy.bySystemId.invalidate(systemId);
};
Comment on lines +24 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add loading state handling for mutations

The cache invalidation logic is good, but the component should handle loading states to prevent multiple simultaneous updates and provide better UX.

 const updatePolicy = api.environment.policy.update.useMutation();
+const [isUpdating, setIsUpdating] = useState(false);
 
 const invalidatePolicy = () => {
   utils.environment.policy.byId.invalidate(id);
   utils.environment.policy.bySystemId.invalidate(systemId);
 };

Committable suggestion skipped: line range outside the PR's diff.


return (
<Form {...form}>
<form onSubmit={onSubmit} className="space-y-10 p-2">
<div className="space-y-10 p-2">
<div className="flex flex-col gap-1">
<h1 className="text-lg font-medium">Approval & Governance</h1>
<span className="text-sm text-muted-foreground">
This category defines policies that govern the oversight and approval
process for deployments. These policies ensure that deployments meet
specific criteria or gain necessary approvals before proceeding,
contributing to compliance, quality assurance, and overall governance
of the deployment process.
</span>
</div>

<div className="space-y-4">
<div className="flex flex-col gap-1">
<h1 className="text-lg font-medium">Approval & Governance</h1>
<h1 className="text-lg font-medium">Approval gates</h1>
<span className="text-sm text-muted-foreground">
This category defines policies that govern the oversight and
approval process for deployments. These policies ensure that
deployments meet specific criteria or gain necessary approvals
before proceeding, contributing to compliance, quality assurance,
and overall governance of the deployment process.
If enabled, a release will require approval from an authorized user
before it can be deployed to any environment with this policy.
</span>
</div>

<FormField
control={form.control}
name="approvalRequirement"
render={({ field: { value, onChange } }) => (
<FormItem className="space-y-4">
<div className="flex flex-col gap-1">
<FormLabel>Approval gates</FormLabel>
<FormDescription>
If enabled, a release will require approval from an authorized
user before it can be deployed to any environment with this
policy.
</FormDescription>
</div>
<FormControl>
<div className="w-32">
<Select value={value} onValueChange={onChange}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="manual">Manual</SelectItem>
<SelectItem value="automatic">Automatic</SelectItem>
</SelectContent>
</Select>
</div>
</FormControl>
</FormItem>
)}
/>
<div className="w-32">
<Select
value={environmentPolicy.approvalRequirement}
onValueChange={(value: "manual" | "automatic") =>
updatePolicy
.mutateAsync({ id, data: { approvalRequirement: value } })
.then(invalidatePolicy)
}
Comment on lines +54 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for the approval requirement update

The mutation lacks error handling and could fail silently. Consider adding error handling and loading states.

 onValueChange={(value: "manual" | "automatic") =>
   updatePolicy
     .mutateAsync({ id, data: { approvalRequirement: value } })
-    .then(invalidatePolicy)
+    .then(invalidatePolicy)
+    .catch((error) => {
+      console.error('Failed to update approval requirement:', error);
+      // TODO: Add toast or other user notification
+    })
 }

Committable suggestion skipped: line range outside the PR's diff.

>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="manual">Manual</SelectItem>
<SelectItem value="automatic">Automatic</SelectItem>
</SelectContent>
</Select>
</div>
</div>

<FormField
control={form.control}
name="successType"
render={({ field: { value, onChange } }) => (
<FormItem className="space-y-4">
<div className="flex flex-col gap-1">
<FormLabel>Previous Deploy Status</FormLabel>
<FormDescription>
Specify a minimum number of resources in dependent
environments to successfully be deployed to before triggering
a release. For example, specifying that all resources in QA
must be deployed to before releasing to PROD.
</FormDescription>
</div>
<FormControl>
<RadioGroup value={value} onValueChange={onChange}>
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="all" />
</FormControl>
<FormLabel className="flex items-center gap-2 font-normal">
All resources in dependent environments must complete
successfully
</FormLabel>
</FormItem>
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="some" />
</FormControl>
<FormLabel className="flex items-center gap-2 font-normal">
A minimum of{" "}
<Input
disabled={value !== "some"}
type="number"
value={successMinimum}
onChange={(e) =>
form.setValue(
"successMinimum",
e.target.valueAsNumber,
)
}
className="border-b-1 h-6 w-16 text-xs"
/>
resources must be successfully deployed to
</FormLabel>
</FormItem>
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="optional" />
</FormControl>
<FormLabel className="flex items-center gap-2 font-normal">
No validation required
</FormLabel>
</FormItem>
</RadioGroup>
</FormControl>
</FormItem>
)}
/>
<div className="space-y-4">
<div className="flex flex-col gap-1">
<h1 className="text-lg font-medium">Previous Deploy Status</h1>
<span className="text-sm text-muted-foreground">
Specify a minimum number of resources in dependent environments to
successfully be deployed to before triggering a release. For
example, specifying that all resources in QA must be deployed to
before releasing to PROD.
</span>
</div>

<Button
type="submit"
disabled={form.formState.isSubmitting || !form.formState.isDirty}
>
Save
</Button>
</form>
</Form>
<div>
<RadioGroup
value={environmentPolicy.successType}
onValueChange={(value: "all" | "some" | "optional") =>
updatePolicy
.mutateAsync({ id, data: { successType: value } })
.then(invalidatePolicy)
}
>
<div className="flex items-center space-x-3 space-y-0">
<RadioGroupItem value="all" />
<Label>
All resources in dependent environments must complete
successfully
</Label>
</div>

<div className="flex items-center space-x-3 space-y-0">
<RadioGroupItem value="some" />
<Label className="flex items-center gap-1">
A minimum of{" "}
<Input
disabled={environmentPolicy.successType !== "some"}
type="number"
value={environmentPolicy.successMinimum}
onChange={(e) => {
const value = e.target.valueAsNumber;
const successMinimum = Number.isNaN(value) ? 0 : value;
updatePolicy
.mutateAsync({ id, data: { successMinimum } })
.then(invalidatePolicy);
}}
className="h-6 w-16 text-xs"
/>
resources must be successfully deployed to
</Label>
</div>

<div className="flex items-center space-x-3 space-y-0">
<RadioGroupItem value="optional" />
<Label>No validation required</Label>
</div>
</RadioGroup>
</div>
</div>
</div>
);
};
Loading