1
+ import Button from "@mui/material/Button" ;
2
+ import { useFormik } from "formik" ;
1
3
import type { FC } from "react" ;
2
4
import { Helmet } from "react-helmet-async" ;
3
5
import { useMutation , useQuery , useQueryClient } from "react-query" ;
4
6
import { useNavigate , useParams } from "react-router-dom" ;
7
+ import * as Yup from "yup" ;
5
8
import { getErrorMessage } from "api/errors" ;
6
9
import { patchOrganizationRole , organizationRoles } from "api/queries/roles" ;
10
+ import type { PatchRoleRequest } from "api/typesGenerated" ;
7
11
import { displayError } from "components/GlobalSnackbar/utils" ;
8
12
import { Loader } from "components/Loader/Loader" ;
13
+ import { PageHeader } from "components/PageHeader/PageHeader" ;
14
+ import { useAuthenticated } from "contexts/auth/RequireAuth" ;
9
15
import { pageTitle } from "utils/page" ;
10
16
import CreateEditRolePageView from "./CreateEditRolePageView" ;
11
17
12
- export const CreateGroupPage :FC = ( ) => {
18
+ export const CreateEditRolePage :FC = ( ) => {
19
+ const { permissions} = useAuthenticated ( ) ;
13
20
const queryClient = useQueryClient ( ) ;
14
21
const navigate = useNavigate ( ) ;
15
22
const { organization, roleName} = useParams ( ) as {
16
23
organization :string ;
17
24
roleName :string ;
18
25
} ;
26
+ const { assignOrgRole :canAssignOrgRole } = permissions ;
19
27
const patchOrganizationRoleMutation = useMutation (
20
28
patchOrganizationRole ( queryClient , organization ?? "default" ) ,
21
29
) ;
@@ -24,6 +32,31 @@ export const CreateGroupPage: FC = () => {
24
32
) ;
25
33
const role = roleData ?. find ( ( role ) => role . name === roleName ) ;
26
34
35
+ const validationSchema = Yup . object ( {
36
+ name :Yup . string ( ) . required ( ) . label ( "Name" ) ,
37
+ } ) ;
38
+
39
+ const onSubmit = async ( data :PatchRoleRequest ) => {
40
+ try {
41
+ await patchOrganizationRoleMutation . mutateAsync ( data ) ;
42
+ navigate ( `/organizations/${ organization } /roles` ) ;
43
+ } catch ( error ) {
44
+ displayError ( getErrorMessage ( error , "Failed to update custom role" ) ) ;
45
+ }
46
+ } ;
47
+
48
+ const form = useFormik < PatchRoleRequest > ( {
49
+ initialValues :{
50
+ name :role ?. name || "" ,
51
+ display_name :role ?. display_name || "" ,
52
+ site_permissions :role ?. site_permissions || [ ] ,
53
+ organization_permissions :role ?. organization_permissions || [ ] ,
54
+ user_permissions :role ?. user_permissions || [ ] ,
55
+ } ,
56
+ validationSchema,
57
+ onSubmit,
58
+ } ) ;
59
+
27
60
if ( isLoading ) {
28
61
return < Loader /> ;
29
62
}
@@ -37,23 +70,40 @@ export const CreateGroupPage: FC = () => {
37
70
) }
38
71
</ title >
39
72
</ Helmet >
73
+
74
+ < PageHeader
75
+ actions = {
76
+ canAssignOrgRole && (
77
+ < >
78
+ < Button
79
+ onClick = { ( ) => {
80
+ navigate ( `/organizations/${ organization } /roles` ) ;
81
+ } }
82
+ >
83
+ Cancel
84
+ </ Button >
85
+ < Button
86
+ variant = "contained"
87
+ color = "primary"
88
+ onClick = { ( ) => {
89
+ form . handleSubmit ( ) ;
90
+ } }
91
+ >
92
+ { role !== undefined ?"Save" :"Create Role" }
93
+ </ Button >
94
+ </ >
95
+ )
96
+ }
97
+ > </ PageHeader >
98
+
40
99
< CreateEditRolePageView
41
100
role = { role }
42
- onSubmit = { async ( data ) => {
43
- try {
44
- await patchOrganizationRoleMutation . mutateAsync ( data ) ;
45
- navigate ( `/organizations/${ organization } /roles` ) ;
46
- } catch ( error ) {
47
- displayError (
48
- getErrorMessage ( error , "Failed to update custom role" ) ,
49
- ) ;
50
- }
51
- } }
101
+ form = { form }
52
102
error = { patchOrganizationRoleMutation . error }
53
103
isLoading = { patchOrganizationRoleMutation . isLoading }
54
104
/>
55
105
</ >
56
106
) ;
57
107
} ;
58
108
59
- export default CreateGroupPage ;
109
+ export default CreateEditRolePage ;