refactor: streamline modal close handlers and completion callbacks across various components

This commit is contained in:
Victor Santos
2025-10-31 18:43:34 -03:00
parent 7d8b28367e
commit b26fb73053
116 changed files with 1865 additions and 2949 deletions
+8 -15
View File
@@ -35,23 +35,16 @@ export const WishForm = () => {
const [isOpen, setIsOpen] = useToggle(false);
const createWish = async (data: TFormData) => {
try {
await mutateAsync({
text: data.text
});
await mutateAsync({
text: data.text
});
createNotification({
text: "Your wish has been sent to the Infisical team!",
type: "success"
});
createNotification({
text: "Your wish has been sent to the Infisical team!",
type: "success"
});
setIsOpen.off();
} catch {
createNotification({
text: "An error occured while sending your wish to the Infisical team.",
type: "error"
});
}
setIsOpen.off();
};
return (
@@ -43,35 +43,27 @@ export const CreateOrgModal: FC<CreateOrgModalProps> = ({ isOpen, onClose }) =>
const { mutateAsync: selectOrg } = useSelectOrganization();
const onFormSubmit = async ({ name }: FormData) => {
try {
const organization = await createOrg({
name
});
const organization = await createOrg({
name
});
await selectOrg({
organizationId: organization.id
});
await selectOrg({
organizationId: organization.id
});
createNotification({
text: "Successfully created organization",
type: "success"
});
createNotification({
text: "Successfully created organization",
type: "success"
});
navigate({
to: "/organization/projects"
});
navigate({
to: "/organization/projects"
});
localStorage.setItem("orgData.id", organization.id);
localStorage.setItem("orgData.id", organization.id);
reset();
onClose();
} catch (err) {
console.error(err);
createNotification({
text: "Failed to created organization",
type: "error"
});
}
reset();
onClose();
};
return (
@@ -20,28 +20,19 @@ export const DeletePkiSyncModal = ({ isOpen, onOpenChange, pkiSync, onComplete }
const handleDeletePkiSync = async () => {
const destinationName = PKI_SYNC_MAP[destination].name;
try {
await deleteSync.mutateAsync({
syncId,
projectId,
destination
});
await deleteSync.mutateAsync({
syncId,
projectId,
destination
});
createNotification({
text: `Successfully deleted ${destinationName} PKI Sync`,
type: "success"
});
createNotification({
text: `Successfully deleted ${destinationName} PKI Sync`,
type: "success"
});
if (onComplete) onComplete();
onOpenChange(false);
} catch (err) {
console.error(err);
createNotification({
text: `Failed to delete ${destinationName} PKI Sync`,
type: "error"
});
}
if (onComplete) onComplete();
onOpenChange(false);
};
return (
@@ -21,27 +21,18 @@ const Content = ({ pkiSync, onComplete }: ContentProps) => {
const triggerImportCertificates = useTriggerPkiSyncImportCertificates();
const handleTriggerImportCertificates = async () => {
try {
await triggerImportCertificates.mutateAsync({
syncId,
destination,
projectId
});
await triggerImportCertificates.mutateAsync({
syncId,
destination,
projectId
});
createNotification({
text: `Successfully triggered certificate import for ${destinationName} Sync`,
type: "success"
});
createNotification({
text: `Successfully triggered certificate import for ${destinationName} Sync`,
type: "success"
});
onComplete();
} catch (err) {
console.error(err);
createNotification({
text: `Failed to trigger certificate import for ${destinationName} Sync`,
type: "error"
});
}
onComplete();
};
return (
@@ -21,27 +21,18 @@ const Content = ({ pkiSync, onComplete }: ContentProps) => {
const triggerRemoveCertificates = useTriggerPkiSyncRemoveCertificates();
const handleTriggerRemoveCertificates = async () => {
try {
await triggerRemoveCertificates.mutateAsync({
syncId,
destination,
projectId
});
await triggerRemoveCertificates.mutateAsync({
syncId,
destination,
projectId
});
createNotification({
text: `Successfully triggered certificate removal for ${destinationName} Sync`,
type: "success"
});
createNotification({
text: `Successfully triggered certificate removal for ${destinationName} Sync`,
type: "success"
});
onComplete();
} catch (err) {
console.error(err);
createNotification({
text: `Failed to trigger certificate removal for ${destinationName} Sync`,
type: "error"
});
}
onComplete();
};
return (
@@ -72,14 +72,8 @@ export const CreatePkiSyncForm = ({ destination, onComplete, onCancel }: Props)
type: "success"
});
onComplete(pkiSync);
} catch (err: Error | unknown) {
console.error(err);
} catch {
setShowConfirmation(false);
createNotification({
title: `Failed to add ${destinationName} Certificate Sync`,
text: err instanceof Error ? err.message : "An unknown error occurred",
type: "error"
});
}
};
@@ -38,28 +38,19 @@ export const EditPkiSyncForm = ({ pkiSync, fields, onComplete }: Props) => {
});
const onSubmit = async ({ connection, ...formData }: TUpdatePkiSyncForm) => {
try {
const updatedPkiSync = await updatePkiSync.mutateAsync({
syncId: pkiSync.id,
...formData,
connectionId: connection.id,
projectId: pkiSync.projectId,
destination: pkiSync.destination
});
const updatedPkiSync = await updatePkiSync.mutateAsync({
syncId: pkiSync.id,
...formData,
connectionId: connection.id,
projectId: pkiSync.projectId,
destination: pkiSync.destination
});
createNotification({
text: `Successfully updated ${destinationName} PKI Sync`,
type: "success"
});
onComplete(updatedPkiSync);
} catch (err: any) {
console.error(err);
createNotification({
title: `Failed to update ${destinationName} PKI Sync`,
text: err.message,
type: "error"
});
}
createNotification({
text: `Successfully updated ${destinationName} PKI Sync`,
type: "success"
});
onComplete(updatedPkiSync);
};
let Component: ReactNode;
@@ -56,30 +56,22 @@ export const ProjectOverviewChangeSection = ({ showSlugField = false }: Props) =
}, [currentProject, showSlugField]);
const onFormSubmit = async (data: BaseFormData | FormDataWithSlug) => {
try {
if (!currentProject?.id) return;
if (!currentProject?.id) return;
await mutateAsync({
projectId: currentProject.id,
newProjectName: data.name,
newProjectDescription: data.description,
...(showSlugField &&
"slug" in data && {
newSlug: data.slug !== currentProject.slug ? data.slug : undefined
})
});
await mutateAsync({
projectId: currentProject.id,
newProjectName: data.name,
newProjectDescription: data.description,
...(showSlugField &&
"slug" in data && {
newSlug: data.slug !== currentProject.slug ? data.slug : undefined
})
});
createNotification({
text: "Successfully updated project overview",
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to update project overview",
type: "error"
});
}
createNotification({
text: "Successfully updated project overview",
type: "success"
});
};
return (
@@ -141,29 +141,24 @@ const NewProjectForm = ({ onOpenChange }: NewProjectFormProps) => {
// type check
if (!currentOrg) return;
if (!user) return;
try {
const {
data: { project }
} = await createWs.mutateAsync({
projectName: name,
projectDescription: description,
kmsKeyId: kmsKeyId !== INTERNAL_KMS_KEY_ID ? kmsKeyId : undefined,
template,
type
});
await refetchWorkspaces();
const {
data: { project }
} = await createWs.mutateAsync({
projectName: name,
projectDescription: description,
kmsKeyId: kmsKeyId !== INTERNAL_KMS_KEY_ID ? kmsKeyId : undefined,
template,
type
});
await refetchWorkspaces();
createNotification({ text: "Project created", type: "success" });
reset();
onOpenChange(false);
navigate({
to: getProjectHomePage(project.type, project.environments),
params: { projectId: project.id }
});
} catch (err) {
console.error(err);
createNotification({ text: "Failed to create project", type: "error" });
}
createNotification({ text: "Project created", type: "success" });
reset();
onOpenChange(false);
navigate({
to: getProjectHomePage(project.type, project.environments),
params: { projectId: project.id }
});
};
const onSubmit = handleSubmit((data) => {
return onCreateProject(data);
@@ -37,29 +37,22 @@ export const DeleteSecretRotationV2Modal = ({
const handleDeleteSecretRotation = async () => {
const rotationType = SECRET_ROTATION_MAP[type].name;
try {
await deleteSecretRotation.mutateAsync({
rotationId,
type,
revokeGeneratedCredentials,
deleteSecrets,
projectId,
secretPath: folder.path
});
await deleteSecretRotation.mutateAsync({
rotationId,
type,
revokeGeneratedCredentials,
deleteSecrets,
projectId,
secretPath: folder.path
});
createNotification({
text: `Successfully deleted ${rotationType} Rotation`,
type: "success"
});
createNotification({
text: `Successfully deleted ${rotationType} Rotation`,
type: "success"
});
if (onComplete) onComplete();
onOpenChange(false);
} catch {
createNotification({
text: `Failed to delete ${rotationType} Rotation`,
type: "error"
});
}
if (onComplete) onComplete();
onOpenChange(false);
};
return (
@@ -22,28 +22,19 @@ const Content = ({ secretRotation, onComplete }: ContentProps) => {
const rotationType = SECRET_ROTATION_MAP[type].name;
const handleRotateSecrets = async () => {
try {
await rotateSecrets.mutateAsync({
rotationId,
type,
projectId,
secretPath: folder.path
});
await rotateSecrets.mutateAsync({
rotationId,
type,
projectId,
secretPath: folder.path
});
createNotification({
text: `Successfully rotated ${rotationType} secrets`,
type: "success"
});
createNotification({
text: `Successfully rotated ${rotationType} secrets`,
type: "success"
});
onComplete();
} catch (err) {
console.error(err);
createNotification({
text: `Failed to rotate ${rotationType} secrets`,
type: "error"
});
}
onComplete();
};
return (
@@ -120,21 +120,13 @@ export const SecretRotationV2Form = ({
environment: environment.slug,
projectId: currentProject.id
});
try {
const rotation = await mutation;
const rotation = await mutation;
createNotification({
text: `Successfully ${secretRotation ? "updated" : "created"} ${rotationType} Rotation`,
type: "success"
});
onComplete(rotation);
} catch (err: any) {
createNotification({
title: `Failed to ${secretRotation ? "update" : "create"} ${rotationType} Rotation`,
text: err.message,
type: "error"
});
}
createNotification({
text: `Successfully ${secretRotation ? "updated" : "created"} ${rotationType} Rotation`,
type: "success"
});
onComplete(rotation);
};
const handlePrev = () => {
@@ -28,26 +28,19 @@ export const DeleteSecretScanningDataSourceModal = ({
const handleDeleteDataSource = async () => {
const dataSourceType = SECRET_SCANNING_DATA_SOURCE_MAP[type].name;
try {
await deleteDataSource.mutateAsync({
dataSourceId,
type,
projectId
});
await deleteDataSource.mutateAsync({
dataSourceId,
type,
projectId
});
createNotification({
text: `Successfully deleted ${dataSourceType} Data Source`,
type: "success"
});
createNotification({
text: `Successfully deleted ${dataSourceType} Data Source`,
type: "success"
});
if (onComplete) onComplete();
onOpenChange(false);
} catch {
createNotification({
text: `Failed to delete ${dataSourceType} Data Source`,
type: "error"
});
}
if (onComplete) onComplete();
onOpenChange(false);
};
return (
@@ -73,21 +73,13 @@ export const SecretScanningDataSourceForm = ({
connectionId: connection?.id,
projectId: currentProject.id
});
try {
const source = await mutation;
const source = await mutation;
createNotification({
text: `Successfully ${source ? "updated" : "created"} ${sourceType} Data Source`,
type: "success"
});
onComplete(source);
} catch (err: any) {
createNotification({
title: `Failed to ${dataSource ? "update" : "create"} ${sourceType} Data Source`,
text: err.message,
type: "error"
});
}
createNotification({
text: `Successfully ${source ? "updated" : "created"} ${sourceType} Data Source`,
type: "success"
});
onComplete(source);
};
const handlePrev = () => {
@@ -23,29 +23,20 @@ export const DeleteSecretSyncModal = ({ isOpen, onOpenChange, secretSync, onComp
const handleDeleteSecretSync = async () => {
const destinationName = SECRET_SYNC_MAP[destination].name;
try {
await deleteSync.mutateAsync({
syncId,
destination,
removeSecrets,
projectId
});
await deleteSync.mutateAsync({
syncId,
destination,
removeSecrets,
projectId
});
createNotification({
text: `Successfully removed ${destinationName} Sync`,
type: "success"
});
createNotification({
text: `Successfully removed ${destinationName} Sync`,
type: "success"
});
if (onComplete) onComplete();
onOpenChange(false);
} catch (err) {
console.error(err);
createNotification({
text: `Failed to remove ${destinationName} Sync`,
type: "error"
});
}
if (onComplete) onComplete();
onOpenChange(false);
};
return (
@@ -51,28 +51,19 @@ const Content = ({ secretSync, onComplete }: ContentProps) => {
const triggerImportSecrets = useTriggerSecretSyncImportSecrets();
const handleTriggerImportSecrets = async ({ importBehavior }: TFormData) => {
try {
await triggerImportSecrets.mutateAsync({
syncId,
destination,
importBehavior,
projectId
});
await triggerImportSecrets.mutateAsync({
syncId,
destination,
importBehavior,
projectId
});
createNotification({
text: `Successfully triggered secret import for ${destinationName} Sync`,
type: "success"
});
createNotification({
text: `Successfully triggered secret import for ${destinationName} Sync`,
type: "success"
});
onComplete();
} catch (err) {
console.error(err);
createNotification({
text: `Failed to trigger secret import for ${destinationName} Sync`,
type: "error"
});
}
onComplete();
};
return (
@@ -21,27 +21,18 @@ const Content = ({ secretSync, onComplete }: ContentProps) => {
const triggerSyncImport = useTriggerSecretSyncRemoveSecrets();
const handleTriggerRemoveSecrets = async () => {
try {
await triggerSyncImport.mutateAsync({
syncId,
destination,
projectId
});
await triggerSyncImport.mutateAsync({
syncId,
destination,
projectId
});
createNotification({
text: `Successfully triggered secret removal for ${destinationName} Sync`,
type: "success"
});
createNotification({
text: `Successfully triggered secret removal for ${destinationName} Sync`,
type: "success"
});
onComplete();
} catch (err) {
console.error(err);
createNotification({
text: `Failed to trigger secret removal for ${destinationName} Sync`,
type: "error"
});
}
onComplete();
};
return (
@@ -88,14 +88,8 @@ export const CreateSecretSyncForm = ({
type: "success"
});
onComplete(secretSync);
} catch (err: any) {
console.error(err);
} catch {
setShowConfirmation(false);
createNotification({
title: `Failed to add ${destinationName} Sync`,
text: err.message,
type: "error"
});
}
};
@@ -58,29 +58,20 @@ export const EditSecretSyncForm = ({ secretSync, fields, onComplete }: Props) =>
const performUpdate = useCallback(
async (formData: TSecretSyncForm) => {
try {
const { environment, connection, ...updateData } = formData;
const updatedSecretSync = await updateSecretSync.mutateAsync({
syncId: secretSync.id,
...updateData,
environment: environment?.slug,
connectionId: connection.id,
projectId: secretSync.projectId
});
const { environment, connection, ...updateData } = formData;
const updatedSecretSync = await updateSecretSync.mutateAsync({
syncId: secretSync.id,
...updateData,
environment: environment?.slug,
connectionId: connection.id,
projectId: secretSync.projectId
});
createNotification({
text: `Successfully updated ${destinationName} Sync`,
type: "success"
});
onComplete(updatedSecretSync);
} catch (err: any) {
console.error(err);
createNotification({
title: `Failed to update ${destinationName} Sync`,
text: err.message,
type: "error"
});
}
createNotification({
text: `Successfully updated ${destinationName} Sync`,
type: "success"
});
onComplete(updatedSecretSync);
},
[updateSecretSync, secretSync.id, secretSync.projectId, destinationName, onComplete]
);
@@ -130,26 +130,18 @@ export const CreateTagModal = ({ isOpen, onToggle, append, currentSecret }: Prop
}, [isOpen]);
const onFormSubmit = async ({ slug, color }: FormData) => {
try {
const data = await createWsTag({
projectId,
tagColor: color,
tagSlug: slug
});
append(data);
onToggle(false);
reset();
createNotification({
text: "Successfully created a tag",
type: "success"
});
} catch (error) {
console.error(error);
createNotification({
text: "Failed to create a tag",
type: "error"
});
}
const data = await createWsTag({
projectId,
tagColor: color,
tagSlug: slug
});
append(data);
onToggle(false);
reset();
createNotification({
text: "Successfully created a tag",
type: "success"
});
};
return (