fix: edge cases

This commit is contained in:
Piyush Gupta
2025-12-05 23:46:37 +05:30
parent 1f0daf447e
commit 32ecbd2d6d
2 changed files with 20 additions and 43 deletions
+1 -4
View File
@@ -186,10 +186,7 @@ export const AddExternalKmsGcpFormSchema = z.discriminatedUnion("formType", [
.merge(AddExternalKmsGcpFormSchemaStandardInputs) .merge(AddExternalKmsGcpFormSchemaStandardInputs)
.merge(AddExternalKmsSchema.pick({ name: true, description: true })), .merge(AddExternalKmsSchema.pick({ name: true, description: true })),
z z
.object({ formType: z.literal("updateGcpKmsDetails") }) .object({ formType: z.literal("updateGcpKms") })
.merge(AddExternalKmsSchema.pick({ name: true, description: true })),
z
.object({ formType: z.literal("updateGcpKmsCredentials") })
.merge(AddExternalKmsGcpFormSchemaStandardInputs) .merge(AddExternalKmsGcpFormSchemaStandardInputs)
.merge(AddExternalKmsSchema.pick({ name: true, description: true })) .merge(AddExternalKmsSchema.pick({ name: true, description: true }))
]); ]);
@@ -81,13 +81,6 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props)
const [isCredentialValid, setIsCredentialValid] = useState<boolean>(false); const [isCredentialValid, setIsCredentialValid] = useState<boolean>(false);
const [keys, setKeys] = useState<{ value: string; label: string }[]>([]); const [keys, setKeys] = useState<{ value: string; label: string }[]>([]);
const getFormType = () => {
if (kms) {
return mode === "details" ? "updateGcpKmsDetails" : "updateGcpKmsCredentials";
}
return "newGcpKms";
};
const { const {
control, control,
handleSubmit, handleSubmit,
@@ -96,11 +89,11 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props)
getValues, getValues,
resetField, resetField,
setValue, setValue,
formState: { isSubmitting } formState: { isSubmitting, isDirty, isValid }
} = useForm<AddExternalKmsGcpFormSchemaType>({ } = useForm<AddExternalKmsGcpFormSchemaType>({
resolver: zodResolver(AddExternalKmsGcpFormSchema), resolver: zodResolver(AddExternalKmsGcpFormSchema),
defaultValues: { defaultValues: {
formType: getFormType(), formType: kms ? "updateGcpKms" : "newGcpKms",
name: kms?.name ?? "", name: kms?.name ?? "",
description: kms?.description ?? "", description: kms?.description ?? "",
gcpRegion: kms gcpRegion: kms
@@ -152,24 +145,11 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props)
// handles the form submission // handles the form submission
const handleGcpKmsFormSubmit = async (data: AddExternalKmsGcpFormSchemaType) => { const handleGcpKmsFormSubmit = async (data: AddExternalKmsGcpFormSchemaType) => {
const { name, description, formType } = data; const { name, description, formType, gcpRegion: gcpRegionObject, keyObject } = data;
try { try {
if (kms) { if (kms) {
if (formType === "updateGcpKmsDetails") { if (formType === "updateGcpKms") {
await updateGcpExternalKms({
kmsId: kms.id,
name,
description
});
createNotification({
text: "Successfully updated GCP External KMS Details",
type: "success"
});
} else if (formType === "updateGcpKmsCredentials") {
const { gcpRegion: gcpRegionObject, keyObject } = data;
const gcpRegion = gcpRegionObject?.value; const gcpRegion = gcpRegionObject?.value;
if (!gcpRegion) { if (!gcpRegion) {
setError("gcpRegion", { setError("gcpRegion", {
@@ -188,6 +168,8 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props)
await updateGcpExternalKms({ await updateGcpExternalKms({
kmsId: kms.id, kmsId: kms.id,
name,
description,
configuration: { configuration: {
type: ExternalKmsProvider.Gcp, type: ExternalKmsProvider.Gcp,
inputs: { inputs: {
@@ -198,13 +180,11 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props)
}); });
createNotification({ createNotification({
text: "Successfully updated GCP External KMS configuration", text: "Successfully updated GCP External KMS Details",
type: "success" type: "success"
}); });
} }
} else if (formType === "newGcpKms") { } else if (formType === "newGcpKms") {
const { gcpRegion: gcpRegionObject, keyObject } = data;
const gcpRegion = gcpRegionObject?.value; const gcpRegion = gcpRegionObject?.value;
if (!gcpRegion) { if (!gcpRegion) {
setError("gcpRegion", { setError("gcpRegion", {
@@ -334,10 +314,6 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props)
</FormControl> </FormControl>
)} )}
/> />
</>
)}
{(mode === "full" || mode === "credentials") && (
<>
<Controller <Controller
control={control} control={control}
name="gcpRegion" name="gcpRegion"
@@ -402,17 +378,21 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props)
</FormControl> </FormControl>
)} )}
/> />
{kms && ( </>
)}
{kms && mode === "credentials" && (
<span className="text-xs text-mineshaft-300"> <span className="text-xs text-mineshaft-300">
To change your GCP credentials, create a new external KMS and assign it to project you To change your GCP credentials, create a new external KMS and assign it to project you
want to use it with. want to use it with.
</span> </span>
)} )}
</>
)}
<div className="mt-6 flex items-center space-x-4"> <div className="mt-6 flex items-center space-x-4">
<Button type="submit" isLoading={isSubmitting}> <Button
{mode === "credentials" ? "Update Configuration" : "Save"} type="submit"
isLoading={isSubmitting}
isDisabled={!isDirty || !isValid || mode === "credentials"}
>
Save
</Button> </Button>
<Button variant="outline_bg" onClick={onCancel}> <Button variant="outline_bg" onClick={onCancel}>
Cancel Cancel