diff --git a/frontend/src/views/Org/MembersPage/components/OrgRoleTabSection/OrgRoleTable.tsx b/frontend/src/views/Org/MembersPage/components/OrgRoleTabSection/OrgRoleTable.tsx index da7829344..0fce9f06b 100644 --- a/frontend/src/views/Org/MembersPage/components/OrgRoleTabSection/OrgRoleTable.tsx +++ b/frontend/src/views/Org/MembersPage/components/OrgRoleTabSection/OrgRoleTable.tsx @@ -66,7 +66,6 @@ export const OrgRoleTable = () => { colorSchema="primary" type="submit" leftIcon={} - // onClick={() => onSelectRole()} onClick={() => { handlePopUpOpen("role"); }} diff --git a/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionRow.tsx b/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionRow.tsx index b27a95a22..ca4902eb3 100644 --- a/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionRow.tsx +++ b/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionRow.tsx @@ -64,11 +64,8 @@ type Props = { formName: keyof Omit, "workspace">; setValue: UseFormSetValue; control: Control; - handleSubmit: () => void; }; -// permission categories - enum Permission { NoAccess = "no-access", ReadOnly = "read-only", @@ -76,16 +73,7 @@ enum Permission { Custom = "custom" } -// TODO: support for default roles - -export const RolePermissionRow = ({ - isEditable, - title, - formName, - handleSubmit, - control, - setValue -}: Props) => { +export const RolePermissionRow = ({ isEditable, title, formName, control, setValue }: Props) => { const [isRowExpanded, setIsRowExpanded] = useToggle(); const [isCustom, setIsCustom] = useToggle(); @@ -157,8 +145,6 @@ export const RolePermissionRow = ({ ); break; } - - handleSubmit(); }; return ( @@ -211,7 +197,6 @@ export const RolePermissionRow = ({ return; } field.onChange(e); - handleSubmit(); }} id={`permissions.${formName}.${action}`} > diff --git a/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionsSection.tsx b/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionsSection.tsx index 2b7d43a7d..02422af41 100644 --- a/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionsSection.tsx +++ b/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionsSection.tsx @@ -1,18 +1,162 @@ -import { RolePermissionsTable } from "./RolePermissionsTable"; +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; + +import { createNotification } from "@app/components/notifications"; +import { Button , Table, TableContainer, TBody, Th, THead, Tr } from "@app/components/v2"; +import { useOrganization } from "@app/context"; +import { useGetOrgRole, useUpdateOrgRole } from "@app/hooks/api"; +import { + formRolePermission2API, + formSchema, + rolePermission2Form, + TFormSchema +} from "@app/views/Org/MembersPage/components/OrgRoleTabSection/OrgRoleModifySection/OrgRoleModifySection.utils"; + +import { RolePermissionRow } from "./RolePermissionRow"; + +const SIMPLE_PERMISSION_OPTIONS = [ + { + title: "User management", + formName: "member" + }, + { + title: "Group management", + formName: "groups" + }, + { + title: "Machine identity management", + formName: "identity" + }, + { + title: "Billing & usage", + formName: "billing" + }, + { + title: "Role management", + formName: "role" + }, + { + title: "Incident Contacts", + formName: "incident-contact" + }, + { + title: "Organization profile", + formName: "settings" + }, + { + title: "Secret Scanning", + formName: "secret-scanning" + }, + { + title: "SSO", + formName: "sso" + }, + { + title: "LDAP", + formName: "ldap" + }, + { + title: "SCIM", + formName: "scim" + } +] as const; type Props = { roleId: string; }; export const RolePermissionsSection = ({ roleId }: Props) => { + const { currentOrg } = useOrganization(); + const orgId = currentOrg?.id || ""; + + const { data: role } = useGetOrgRole(orgId, roleId); + + const { + setValue, + control, + handleSubmit, + formState: { isDirty, isSubmitting }, + reset + } = useForm({ + defaultValues: role ? { ...role, permissions: rolePermission2Form(role.permissions) } : {}, + resolver: zodResolver(formSchema) + }); + + 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" }); + } + }; + + const isCustomRole = !["admin", "member", "no-access"].includes(role?.slug ?? ""); + return ( -
+

Permissions

+ {isCustomRole && ( +
+ + +
+ )}
- + + + + + + + + + + {SIMPLE_PERMISSION_OPTIONS.map((permission) => { + return ( + + ); + })} + +
+ ResourcePermission
+
-
+ ); }; diff --git a/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionsTable.tsx b/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionsTable.tsx deleted file mode 100644 index 0140d8eff..000000000 --- a/frontend/src/views/Org/RolePage/components/RolePermissionsSection/RolePermissionsTable.tsx +++ /dev/null @@ -1,128 +0,0 @@ -import { useForm } from "react-hook-form"; -import { zodResolver } from "@hookform/resolvers/zod"; - -import { createNotification } from "@app/components/notifications"; -import { Table, TableContainer, TBody, Th, THead, Tr } from "@app/components/v2"; -import { useOrganization } from "@app/context"; -import { useGetOrgRole, useUpdateOrgRole } from "@app/hooks/api"; -import { - formRolePermission2API, - formSchema, - rolePermission2Form, - TFormSchema -} from "@app/views/Org/MembersPage/components/OrgRoleTabSection/OrgRoleModifySection/OrgRoleModifySection.utils"; - -import { RolePermissionRow } from "./RolePermissionRow"; - -const SIMPLE_PERMISSION_OPTIONS = [ - { - title: "User management", - formName: "member" - }, - { - title: "Group management", - formName: "groups" - }, - { - title: "Machine identity management", - formName: "identity" - }, - { - title: "Billing & usage", - formName: "billing" - }, - { - title: "Role management", - formName: "role" - }, - { - title: "Incident Contacts", - formName: "incident-contact" - }, - { - title: "Organization profile", - formName: "settings" - }, - { - title: "Secret Scanning", - formName: "secret-scanning" - }, - { - title: "SSO", - formName: "sso" - }, - { - title: "LDAP", - formName: "ldap" - }, - { - title: "SCIM", - formName: "scim" - } -] as const; - -type Props = { - roleId: string; -}; - -export const RolePermissionsTable = ({ roleId }: Props) => { - const { currentOrg } = useOrganization(); - const orgId = currentOrg?.id || ""; - - const { data: role } = useGetOrgRole(orgId, roleId); - - const { setValue, control, handleSubmit } = useForm({ - defaultValues: role ? { ...role, permissions: rolePermission2Form(role.permissions) } : {}, - resolver: zodResolver(formSchema) - }); - - 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" }); - } - }; - - const isCustomRole = !["admin", "member", "no-access"].includes(role?.slug ?? ""); - - return ( - -
- - - - - - - - - {SIMPLE_PERMISSION_OPTIONS.map((permission) => { - return ( - - ); - })} - -
- ResourcePermission
-
-
- ); -};