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
@@ -74,43 +74,36 @@ export const OrgGroupModal = ({ popUp, handlePopUpClose, handlePopUpToggle }: Pr
}, [popUp?.group?.data, roles]);
const onGroupModalSubmit = async ({ name, slug, role }: TGroupFormData) => {
try {
if (!currentOrg?.id) return;
if (!currentOrg?.id) return;
const group = popUp?.group?.data as {
groupId: string;
name: string;
slug: string;
};
const group = popUp?.group?.data as {
groupId: string;
name: string;
slug: string;
};
if (group) {
await updateMutateAsync({
id: group.groupId,
name,
slug,
role: role.slug || undefined
});
} else {
await createMutateAsync({
name,
slug,
organizationId: currentOrg.id,
role: role.slug || undefined
});
}
handlePopUpToggle("group", false);
reset();
createNotification({
text: `Successfully ${popUp?.group?.data ? "updated" : "created"} group`,
type: "success"
if (group) {
await updateMutateAsync({
id: group.groupId,
name,
slug,
role: role.slug || undefined
});
} catch {
createNotification({
text: `Failed to ${popUp?.group?.data ? "updated" : "created"} group`,
type: "error"
} else {
await createMutateAsync({
name,
slug,
organizationId: currentOrg.id,
role: role.slug || undefined
});
}
handlePopUpToggle("group", false);
reset();
createNotification({
text: `Successfully ${popUp?.group?.data ? "updated" : "created"} group`,
type: "success"
});
};
return (
@@ -37,21 +37,13 @@ export const OrgGroupsSection = () => {
};
const onDeleteGroupSubmit = async ({ name, groupId }: { name: string; groupId: string }) => {
try {
await deleteMutateAsync({
id: groupId
});
createNotification({
text: `Successfully deleted the group named ${name}`,
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: `Failed to delete the group named ${name}`,
type: "error"
});
}
await deleteMutateAsync({
id: groupId
});
createNotification({
text: `Successfully deleted the group named ${name}`,
type: "success"
});
handlePopUpClose("deleteGroup");
};
@@ -79,23 +79,15 @@ export const OrgGroupsTable = ({ handlePopUpOpen }: Props) => {
const { data: roles } = useGetOrgRoles(orgId);
const handleChangeRole = async ({ id, role }: { id: string; role: string }) => {
try {
await updateMutateAsync({
id,
role
});
await updateMutateAsync({
id,
role
});
createNotification({
text: "Successfully updated group role",
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to update group role",
type: "error"
});
}
createNotification({
text: "Successfully updated group role",
type: "success"
});
};
const {
@@ -56,53 +56,31 @@ export const IdentitySection = withPermission(
const isEnterprise = subscription?.slug === "enterprise";
const onDeleteIdentitySubmit = async (identityId: string) => {
try {
await deleteMutateAsync({
identityId,
organizationId: orgId
});
await deleteMutateAsync({
identityId,
organizationId: orgId
});
createNotification({
text: "Successfully deleted identity",
type: "success"
});
createNotification({
text: "Successfully deleted identity",
type: "success"
});
handlePopUpClose("deleteIdentity");
} catch (err) {
console.error(err);
const error = err as any;
const text = error?.response?.data?.message ?? "Failed to delete identity";
createNotification({
text,
type: "error"
});
}
handlePopUpClose("deleteIdentity");
};
const onDeleteTemplateSubmit = async (templateId: string) => {
try {
await deleteTemplateMutateAsync({
templateId,
organizationId: orgId
});
await deleteTemplateMutateAsync({
templateId,
organizationId: orgId
});
createNotification({
text: "Successfully deleted template",
type: "success"
});
createNotification({
text: "Successfully deleted template",
type: "success"
});
handlePopUpClose("deleteTemplate");
} catch (err) {
console.error(err);
const error = err as any;
const text = error?.response?.data?.message ?? "Failed to delete template";
createNotification({
text,
type: "error"
});
}
handlePopUpClose("deleteTemplate");
};
return (
@@ -85,46 +85,30 @@ export const OrgMembersSection = () => {
};
const onDeactivateMemberSubmit = async (orgMembershipId: string) => {
try {
await updateOrgMembership({
organizationId: orgId,
membershipId: orgMembershipId,
isActive: false
});
await updateOrgMembership({
organizationId: orgId,
membershipId: orgMembershipId,
isActive: false
});
createNotification({
text: "Successfully deactivated user in organization",
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to deactivate user in organization",
type: "error"
});
}
createNotification({
text: "Successfully deactivated user in organization",
type: "success"
});
handlePopUpClose("deactivateMember");
};
const onRemoveMemberSubmit = async (orgMembershipId: string) => {
try {
await deleteMutateAsync({
orgId,
membershipId: orgMembershipId
});
await deleteMutateAsync({
orgId,
membershipId: orgMembershipId
});
createNotification({
text: "Successfully removed user from org",
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to remove user from the organization",
type: "error"
});
}
createNotification({
text: "Successfully removed user from org",
type: "success"
});
handlePopUpClose("removeMember");
};
@@ -132,27 +116,20 @@ export const OrgMembersSection = () => {
const { data: members = [] } = useGetOrgUsers(orgId);
const handleRemoveMembers = async (selectedMembers: OrgUser[]) => {
try {
await deleteBatchMutateAsync({
orgId,
membershipIds: selectedMembers
.filter((member) => member.user.id !== userId)
.map((member) => member.id)
});
await deleteBatchMutateAsync({
orgId,
membershipIds: selectedMembers
.filter((member) => member.user.id !== userId)
.map((member) => member.id)
});
createNotification({
text: "Successfully removed users from organization",
type: "success"
});
createNotification({
text: "Successfully removed users from organization",
type: "success"
});
setSelectedMemberIds([]);
handlePopUpClose("removeMembers");
} catch {
createNotification({
text: "Failed to remove users from the organization",
type: "error"
});
}
setSelectedMemberIds([]);
handlePopUpClose("removeMembers");
};
return (
@@ -127,35 +127,26 @@ export const OrgMembersTable = ({
const onRoleChange = async (membershipId: string, role: string) => {
if (!currentOrg?.id) return;
try {
// TODO: replace hardcoding default role
const isCustomRole = !["admin", "member", "no-access"].includes(role);
// TODO: replace hardcoding default role
const isCustomRole = !["admin", "member", "no-access"].includes(role);
if (isCustomRole && subscription && !subscription?.rbac) {
handlePopUpOpen("upgradePlan", {
description:
"You can assign custom roles to members if you switch to Infisical's Pro plan."
});
return;
}
await updateOrgMembership({
organizationId: currentOrg?.id,
membershipId,
role
});
createNotification({
text: "Successfully updated user role",
type: "success"
});
} catch (error) {
console.error(error);
createNotification({
text: "Failed to update user role",
type: "error"
if (isCustomRole && subscription && !subscription?.rbac) {
handlePopUpOpen("upgradePlan", {
description: "You can assign custom roles to members if you switch to Infisical's Pro plan."
});
return;
}
await updateOrgMembership({
organizationId: currentOrg?.id,
membershipId,
role
});
createNotification({
text: "Successfully updated user role",
type: "success"
});
};
const onResendInvite = async (membershipId: string) => {
@@ -174,12 +165,6 @@ export const OrgMembersTable = ({
text: "Successfully resent org invitation",
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to resend org invitation",
type: "error"
});
} finally {
setResendInviteId(null);
}
@@ -86,17 +86,12 @@ export const OrgRoleTable = () => {
const handleRoleDelete = async () => {
const { id } = popUp?.deleteRole?.data as TOrgRole;
try {
await deleteRole({
orgId,
id
});
createNotification({ type: "success", text: "Successfully removed the role" });
handlePopUpClose("deleteRole");
} catch (err) {
console.log(err);
createNotification({ type: "error", text: "Failed to delete role" });
}
await deleteRole({
orgId,
id
});
createNotification({ type: "success", text: "Successfully removed the role" });
handlePopUpClose("deleteRole");
};
const handleSetRoleAsDefault = async (defaultMembershipRoleSlug: string) => {
@@ -110,17 +105,12 @@ export const OrgRoleTable = () => {
return;
}
try {
await updateOrg({
orgId,
defaultMembershipRoleSlug
});
createNotification({ type: "success", text: "Successfully updated default membership role" });
handlePopUpClose("deleteRole");
} catch (err) {
console.log(err);
createNotification({ type: "error", text: "Failed to update default membership role" });
}
await updateOrg({
orgId,
defaultMembershipRoleSlug
});
createNotification({ type: "success", text: "Successfully updated default membership role" });
handlePopUpClose("deleteRole");
};
const {
@@ -73,24 +73,15 @@ const CreateForm = ({ app, onComplete, projectId }: CreateFormProps) => {
"method" | "name" | "app" | "credentials" | "isPlatformManagedCredentials"
>
) => {
try {
const connection = await createAppConnection.mutateAsync({
...formData,
projectId
});
createNotification({
text: `Successfully added ${appName} Connection`,
type: "success"
});
onComplete(connection);
} catch (err: any) {
console.error(err);
createNotification({
title: `Failed to add ${appName} Connection`,
text: err.message,
type: "error"
});
}
const connection = await createAppConnection.mutateAsync({
...formData,
projectId
});
createNotification({
text: `Successfully added ${appName} Connection`,
type: "success"
});
onComplete(connection);
};
switch (app) {
@@ -191,24 +182,15 @@ const UpdateForm = ({ appConnection, onComplete }: UpdateFormProps) => {
"method" | "name" | "app" | "credentials" | "isPlatformManagedCredentials"
>
) => {
try {
const connection = await updateAppConnection.mutateAsync({
connectionId: appConnection.id,
...formData
});
createNotification({
text: `Successfully updated ${appName} Connection`,
type: "success"
});
onComplete(connection);
} catch (err: any) {
console.error(err);
createNotification({
title: `Failed to update ${appName} Connection`,
text: err.message,
type: "error"
});
}
const connection = await updateAppConnection.mutateAsync({
connectionId: appConnection.id,
...formData
});
createNotification({
text: `Successfully updated ${appName} Connection`,
type: "success"
});
onComplete(connection);
};
switch (appConnection.app) {
@@ -77,43 +77,36 @@ export const GroupCreateUpdateModal = ({ popUp, handlePopUpClose, handlePopUpTog
}, [popUp?.groupCreateUpdate?.data, roles]);
const onGroupModalSubmit = async ({ name, slug, role }: TGroupFormData) => {
try {
if (!currentOrg?.id) return;
if (!currentOrg?.id) return;
const group = popUp?.groupCreateUpdate?.data as {
groupId: string;
name: string;
slug: string;
};
const group = popUp?.groupCreateUpdate?.data as {
groupId: string;
name: string;
slug: string;
};
if (group) {
await updateMutateAsync({
id: group.groupId,
name,
slug,
role: role.slug || undefined
});
} else {
await createMutateAsync({
name,
slug,
organizationId: currentOrg.id,
role: role.slug || undefined
});
}
handlePopUpToggle("groupCreateUpdate", false);
reset();
createNotification({
text: `Successfully ${popUp?.groupCreateUpdate?.data ? "updated" : "created"} group`,
type: "success"
if (group) {
await updateMutateAsync({
id: group.groupId,
name,
slug,
role: role.slug || undefined
});
} catch {
createNotification({
text: `Failed to ${popUp?.groupCreateUpdate?.data ? "updated" : "created"} group`,
type: "error"
} else {
await createMutateAsync({
name,
slug,
organizationId: currentOrg.id,
role: role.slug || undefined
});
}
handlePopUpToggle("groupCreateUpdate", false);
reset();
createNotification({
text: `Successfully ${popUp?.groupCreateUpdate?.data ? "updated" : "created"} group`,
type: "success"
});
};
return (
@@ -44,34 +44,23 @@ const Page = () => {
] as const);
const onDeleteIdentitySubmit = async (id: string) => {
try {
await deleteIdentity({
identityId: id,
organizationId: orgId
});
await deleteIdentity({
identityId: id,
organizationId: orgId
});
createNotification({
text: "Successfully deleted identity",
type: "success"
});
createNotification({
text: "Successfully deleted identity",
type: "success"
});
handlePopUpClose("deleteIdentity");
navigate({
to: "/organization/access-management",
search: {
selectedTab: OrgAccessControlTabSections.Identities
}
});
} catch (err) {
console.error(err);
const error = err as any;
const text = error?.response?.data?.message ?? "Failed to delete identity";
createNotification({
text,
type: "error"
});
}
handlePopUpClose("deleteIdentity");
navigate({
to: "/organization/access-management",
search: {
selectedTab: OrgAccessControlTabSections.Identities
}
});
};
return (
@@ -107,18 +107,13 @@ export const RolePermissionsSection = ({ roleId }: Props) => {
const { mutateAsync: updateRole } = useUpdateOrgRole();
const onSubmit = async (el: TFormSchema) => {
try {
await updateRole({
orgId,
id: roleId,
...el,
permissions: formRolePermission2API(el.permissions)
});
createNotification({ type: "success", text: "Successfully updated role" });
} catch (err) {
console.log(err);
createNotification({ type: "error", text: "Failed to update role" });
}
await updateRole({
orgId,
id: roleId,
...el,
permissions: formRolePermission2API(el.permissions)
});
createNotification({ type: "success", text: "Successfully updated role" });
};
const isCustomRole = !["admin", "member", "no-access"].includes(role?.slug ?? "");
@@ -64,56 +64,38 @@ const Page = withPermission(
] as const);
const onDeactivateMemberSubmit = async (orgMembershipId: string) => {
try {
await updateOrgMembership({
organizationId: orgId,
membershipId: orgMembershipId,
isActive: false
});
await updateOrgMembership({
organizationId: orgId,
membershipId: orgMembershipId,
isActive: false
});
createNotification({
text: "Successfully deactivated user in organization",
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to deactivate user in organization",
type: "error"
});
}
createNotification({
text: "Successfully deactivated user in organization",
type: "success"
});
handlePopUpClose("deactivateMember");
};
const onRemoveMemberSubmit = async (orgMembershipId: string) => {
try {
await deleteOrgMembership({
orgId,
membershipId: orgMembershipId
});
await deleteOrgMembership({
orgId,
membershipId: orgMembershipId
});
createNotification({
text: "Successfully removed user from org",
type: "success"
});
handlePopUpClose("removeMember");
navigate({
to: "/organization/access-management" as const,
search: {
selectedTab: OrgAccessControlTabSections.Member
}
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to remove user from the organization",
type: "error"
});
}
createNotification({
text: "Successfully removed user from org",
type: "success"
});
handlePopUpClose("removeMember");
navigate({
to: "/organization/access-management" as const,
search: {
selectedTab: OrgAccessControlTabSections.Member
}
});
};
return (
@@ -46,26 +46,18 @@ export const UserDetailsSection = ({ membershipId, handlePopUpOpen }: Props) =>
const { mutateAsync: resendOrgMemberInvitation, isPending } = useResendOrgMemberInvitation();
const onResendInvite = async () => {
try {
const signupToken = await resendOrgMemberInvitation({
membershipId
});
const signupToken = await resendOrgMemberInvitation({
membershipId
});
if (signupToken) {
return;
}
createNotification({
text: "Successfully resent org invitation",
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to resend org invitation",
type: "error"
});
if (signupToken) {
return;
}
createNotification({
text: "Successfully resent org invitation",
type: "success"
});
};
const getStatus = (m: OrgUser) => {