Skip to content

feat: add delete custom role context menu button and modal #14228

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

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: delete custom role
  • Loading branch information
jaaydenh committed Aug 9, 2024
commit 70f98a6f696c47ad23dc9722cc9ccd26683d1157
8 changes: 4 additions & 4 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,20 +601,20 @@ class ApiMethods {
};

patchOrganizationRole = async (
organizationId: string,
organization: string,
Copy link
Member

Choose a reason for hiding this comment

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

can you copy-paste the doc comment a lot of these other org functions have? it's a minor thing, but I like the easily accessible reassurance that it can be the id or the name 😄

role: TypesGen.Role,
): Promise<TypesGen.Role> => {
const response = await this.axios.patch<TypesGen.Role>(
`/api/v2/organizations/${organizationId}/members/roles`,
`/api/v2/organizations/${organization}/members/roles`,
role,
);

return response.data;
};

deleteOrganizationRole = async (organizationId: string, roleName: string) => {
deleteOrganizationRole = async (organization: string, roleName: string) => {
Copy link
Member

Choose a reason for hiding this comment

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

same for this function pls

await this.axios.delete(
`/api/v2/organizations/${organizationId}/members/roles/${roleName}`,
`/api/v2/organizations/${organization}/members/roles/${roleName}`,
);
};

Expand Down
21 changes: 11 additions & 10 deletions site/src/api/queries/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,37 @@ export const roles = () => {
};
};

export const organizationRoles = (organizationId: string) => {
export const organizationRoles = (organization: string) => {
return {
queryKey: ["organization", organizationId, "roles"],
queryFn: () => API.getOrganizationRoles(organizationId),
queryKey: ["organization", organization, "roles"],
queryFn: () => API.getOrganizationRoles(organization),
};
};

export const patchOrganizationRole = (
queryClient: QueryClient,
organizationId: string,
organization: string,
) => {
return {
mutationFn: (request: Role) =>
API.patchOrganizationRole(organizationId, request),
API.patchOrganizationRole(organization, request),
onSuccess: async (updatedRole: Role) =>
await queryClient.invalidateQueries(
getRoleQueryKey(organizationId, updatedRole.name),
getRoleQueryKey(organization, updatedRole.name),
),
};
};

export const deleteRole = (
export const deleteOrganizationRole = (
queryClient: QueryClient,
organizationId: string,
organization: string,
) => {
return {
mutationFn: API.deleteOrganizationRole,
mutationFn: (roleName: string) =>
API.deleteOrganizationRole(organization, roleName),
onSuccess: async (_: void, roleName: string) =>
await queryClient.invalidateQueries(
getRoleQueryKey(organizationId, roleName),
getRoleQueryKey(organization, roleName),
),
};
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import AddIcon from "@mui/icons-material/AddOutlined";
import Button from "@mui/material/Button";
import { type FC, useEffect } from "react";
import { type FC, useEffect, useState } from "react";
import { Helmet } from "react-helmet-async";
import { useQuery } from "react-query";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { Link as RouterLink, useParams } from "react-router-dom";
import { getErrorMessage } from "api/errors";
import { organizationPermissions } from "api/queries/organizations";
import { organizationRoles } from "api/queries/roles";
import { displayError } from "components/GlobalSnackbar/utils";
import { deleteOrganizationRole, organizationRoles } from "api/queries/roles";
import type { Role } from "api/typesGenerated";
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog";
import { displayError, displaySuccess } from "components/GlobalSnackbar/utils";
import { Loader } from "components/Loader/Loader";
import { SettingsHeader } from "components/SettingsHeader/SettingsHeader";
import { Stack } from "components/Stack/Stack";
Expand All @@ -17,13 +19,18 @@ import { useOrganizationSettings } from "../ManagementSettingsLayout";
import CustomRolesPageView from "./CustomRolesPageView";

export const CustomRolesPage: FC = () => {
const queryClient = useQueryClient();
const { custom_roles: isCustomRolesEnabled } = useFeatureVisibility();
const { organization: organizationName } = useParams() as {
organization: string;
};
const { organizations } = useOrganizationSettings();
const organization = organizations?.find((o) => o.name === organizationName);
const permissionsQuery = useQuery(organizationPermissions(organization?.id));
const deleteRoleMutation = useMutation(
deleteOrganizationRole(queryClient, organizationName),
);
const [roleToDelete, setRoleToDelete] = useState<Role>();
const organizationRolesQuery = useQuery(organizationRoles(organizationName));
const filteredRoleData = organizationRolesQuery.data?.filter(
(role) => role.built_in === false,
Expand Down Expand Up @@ -69,9 +76,31 @@ export const CustomRolesPage: FC = () => {

<CustomRolesPageView
roles={filteredRoleData}
onDeleteRole={setRoleToDelete}
canAssignOrgRole={permissions.assignOrgRole}
isCustomRolesEnabled={isCustomRolesEnabled}
/>

<DeleteDialog
key={roleToDelete?.name}
isOpen={roleToDelete !== undefined}
confirmLoading={deleteRoleMutation.isLoading}
name={roleToDelete?.name ?? ""}
entity="role"
onCancel={() => setRoleToDelete(undefined)}
onConfirm={async () => {
try {
await deleteRoleMutation.mutateAsync(roleToDelete!.name);
setRoleToDelete(undefined);
await organizationRolesQuery.refetch();
displaySuccess("Custom role deleted successfully!");
} catch (error) {
displayError(
getErrorMessage(error, "Failed to delete custom role"),
);
}
}}
/>
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Interpolation, Theme } from "@emotion/react";
import AddOutlined from "@mui/icons-material/AddOutlined";
import KeyboardArrowRight from "@mui/icons-material/KeyboardArrowRight";
import Button from "@mui/material/Button";
import Skeleton from "@mui/material/Skeleton";
import Table from "@mui/material/Table";
Expand All @@ -14,22 +13,30 @@ import { Link as RouterLink, useNavigate } from "react-router-dom";
import type { Role } from "api/typesGenerated";
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne";
import { EmptyState } from "components/EmptyState/EmptyState";
import {
MoreMenu,
MoreMenuContent,
MoreMenuItem,
MoreMenuTrigger,
ThreeDotsButton,
} from "components/MoreMenu/MoreMenu";
import { Paywall } from "components/Paywall/Paywall";
import {
TableLoaderSkeleton,
TableRowSkeleton,
} from "components/TableLoader/TableLoader";
import { useClickableTableRow } from "hooks";
import { docs } from "utils/docs";

export type CustomRolesPageViewProps = {
roles: Role[] | undefined;
onDeleteRole: (role: Role) => void;
canAssignOrgRole: boolean;
isCustomRolesEnabled: boolean;
};

export const CustomRolesPageView: FC<CustomRolesPageViewProps> = ({
roles,
onDeleteRole,
canAssignOrgRole,
isCustomRolesEnabled,
}) => {
Expand All @@ -53,7 +60,7 @@ export const CustomRolesPageView: FC<CustomRolesPageViewProps> = ({
<TableRow>
<TableCell width="50%">Name</TableCell>
<TableCell width="49%">Permissions</TableCell>
<TableCell width="1%"></TableCell>
<TableCell width="1%" />
</TableRow>
</TableHead>
<TableBody>
Expand Down Expand Up @@ -91,7 +98,12 @@ export const CustomRolesPageView: FC<CustomRolesPageViewProps> = ({

<Cond>
{roles?.map((role) => (
<RoleRow key={role.name} role={role} />
<RoleRow
key={role.name}
role={role}
canAssignOrgRole={canAssignOrgRole}
onDelete={() => onDeleteRole(role)}
/>
))}
</Cond>
</ChooseOne>
Expand All @@ -106,26 +118,41 @@ export const CustomRolesPageView: FC<CustomRolesPageViewProps> = ({

interface RoleRowProps {
role: Role;
onDelete: () => void;
canAssignOrgRole: boolean;
}

const RoleRow: FC<RoleRowProps> = ({ role }) => {
const RoleRow: FC<RoleRowProps> = ({ role, onDelete, canAssignOrgRole }) => {
const navigate = useNavigate();
const rowProps = useClickableTableRow({
onClick: () => navigate(role.name),
});

return (
<TableRow data-testid={`role-${role.name}`} {...rowProps}>
<TableRow data-testid={`role-${role.name}`}>
<TableCell>{role.display_name || role.name}</TableCell>

<TableCell css={styles.secondary}>
{role.organization_permissions.length}
</TableCell>

<TableCell>
<div css={styles.arrowCell}>
<KeyboardArrowRight css={styles.arrowRight} />
</div>
<MoreMenu>
<MoreMenuTrigger>
<ThreeDotsButton />
</MoreMenuTrigger>
<MoreMenuContent>
<MoreMenuItem
onClick={() => {
navigate(role.name);
}}
>
Edit
</MoreMenuItem>
{canAssignOrgRole && (
<MoreMenuItem danger onClick={onDelete}>
Delete&hellip;
</MoreMenuItem>
)}
</MoreMenuContent>
</MoreMenu>
</TableCell>
</TableRow>
);
Expand All @@ -150,14 +177,6 @@ const TableLoader = () => {
};

const styles = {
arrowRight: (theme) => ({
color: theme.palette.text.secondary,
width: 20,
height: 20,
}),
arrowCell: {
display: "flex",
},
secondary: (theme) => ({
color: theme.palette.text.secondary,
}),
Expand Down
Loading