1
1
import React , { useState , useEffect } from "react"
2
2
import { Link } from "react-router-dom"
3
3
import { DisplayBox , UserHelper , ApiHelper , Permissions , ChurchInterface } from "."
4
- import { RoleInterface } from "../../helpers"
5
- import { Table , TableBody , TableCell , TableHead , TableRow } from "@mui/material" ;
4
+ import { RoleInterface , RolePermissionInterface } from "../../helpers"
5
+ import { Divider , Icon , IconButton , Menu , MenuItem , Table , TableBody , TableCell , TableHead , TableRow } from "@mui/material" ;
6
6
import { SmallButton } from "../../appBase/components"
7
7
8
8
interface Props {
@@ -14,15 +14,95 @@ interface Props {
14
14
export const Roles : React . FC < Props > = ( { selectRoleId, selectedRoleId, church } ) => {
15
15
16
16
const [ roles , setRoles ] = useState < RoleInterface [ ] > ( [ ] ) ;
17
+ const [ anchorEl , setAnchorEl ] = React . useState ( null ) ;
18
+ const open = Boolean ( anchorEl ) ;
19
+ const handleClick = ( e : React . MouseEvent ) => {
20
+ setAnchorEl ( e . currentTarget ) ;
21
+ } ;
22
+ const handleClose = ( ) => {
23
+ setAnchorEl ( null ) ;
24
+ } ;
25
+
26
+ const predefined = [
27
+ { name : "Accounting" , description : "Has access to view and manage donations." , permissions : [
28
+ Permissions . membershipApi . people . view ,
29
+ Permissions . membershipApi . people . edit ,
30
+ Permissions . givingApi . donations . edit ,
31
+ Permissions . givingApi . donations . view ,
32
+ Permissions . givingApi . donations . viewSummary ,
33
+ Permissions . givingApi . settings . edit
34
+ ] } ,
35
+ { name : "Church Staff" , description : "Can edit most data in Chums.org, including people, groups, attendance and forms." , permissions : [
36
+ Permissions . membershipApi . people . view ,
37
+ Permissions . membershipApi . people . edit ,
38
+ Permissions . membershipApi . households . edit ,
39
+ Permissions . membershipApi . groups . edit ,
40
+ Permissions . membershipApi . groupMembers . view ,
41
+ Permissions . membershipApi . groupMembers . edit ,
42
+ Permissions . membershipApi . forms . edit ,
43
+ Permissions . membershipApi . forms . admin ,
44
+ Permissions . attendanceApi . attendance . edit ,
45
+ Permissions . attendanceApi . attendance . view ,
46
+ Permissions . attendanceApi . services . edit ,
47
+ Permissions . attendanceApi . settings . edit ,
48
+ ] } ,
49
+ { name : "Content Admin" , description : "Can edit website content, mobile content and sermons." , permissions : [
50
+ Permissions . contentApi . chat . host ,
51
+ Permissions . contentApi . content . edit ,
52
+ Permissions . contentApi . links . edit ,
53
+ Permissions . contentApi . pages . edit ,
54
+ Permissions . contentApi . settings . edit ,
55
+ Permissions . contentApi . streamingServices . edit ,
56
+ ] } ,
57
+ { name : "Lessons Admin" , description : "Can set up classrooms and schedule lessons on Lessons.church." , permissions : [
58
+ { api : "LessonsApi" , contentType :"Schedules" , permission : "Edit" }
59
+ ] }
60
+ ]
61
+
17
62
18
63
const loadData = ( ) => {
19
64
if ( selectedRoleId !== "notset" ) return ;
20
65
ApiHelper . get ( `/roles/church/${ church . id } ` , "MembershipApi" ) . then ( roles => setRoles ( roles ) ) ;
21
66
}
22
67
68
+ const addRole = async ( role :any ) => {
69
+ console . log ( "made it" )
70
+ console . log ( role ) ;
71
+ handleClose ( ) ;
72
+ if ( window . confirm ( "Do you wish to create a new role of " + role . name + "? It " + role . description . toLowerCase ( ) ) ) {
73
+
74
+ const roles = await ApiHelper . post ( "/roles" , [ { name : role . name } ] , "MembershipApi" ) ;
75
+ const r = roles [ 0 ] ;
76
+ const perms :RolePermissionInterface [ ] = [ ] ;
77
+ role . permissions . forEach ( ( p :any ) => {
78
+ perms . push ( { roleId : r . id , apiName : p . api , contentType : p . contentType , action : p . action } )
79
+ } ) ;
80
+ await ApiHelper . post ( "/rolepermissions/" , perms , "MembershipApi" ) ;
81
+ loadData ( ) ;
82
+ }
83
+ }
84
+
23
85
const getEditContent = ( ) => {
24
86
if ( ! UserHelper . checkAccess ( Permissions . membershipApi . roles . edit ) ) return null ;
25
- else return ( < SmallButton icon = "add" text = "Add" onClick = { ( ) => { selectRoleId ( "" ) ; } } /> ) ;
87
+ else {
88
+ return ( < >
89
+ < IconButton aria-label = "addButton" id = "addBtnGroup" data-cy = "add-button" aria-controls = { open ? "add-menu" : undefined } aria-expanded = { open ? "true" : undefined } aria-haspopup = "true" onClick = { handleClick } >
90
+ < Icon color = "primary" > add</ Icon >
91
+ </ IconButton >
92
+ < Menu id = "add-menu" MenuListProps = { { "aria-labelledby" : "addBtnGroup" } } anchorEl = { anchorEl } open = { open } onClose = { handleClose } >
93
+ < MenuItem data-cy = "add-campus" onClick = { ( ) => { handleClose ( ) ; selectRoleId ( "" ) ; } } >
94
+ < Icon sx = { { mr : "3px" } } > lock</ Icon > Add Custom Role
95
+ </ MenuItem >
96
+ < Divider />
97
+ { predefined . map ( ( role , i ) => (
98
+ < MenuItem key = { role . name } onClick = { ( ) => { addRole ( role ) ; } } title = { role . description } >
99
+ < Icon sx = { { mr : "3px" } } > lock</ Icon > Add "< b > { role . name } </ b > " Role
100
+ </ MenuItem >
101
+ ) ) }
102
+ </ Menu >
103
+ </ > )
104
+ //return (<SmallButton icon="add" text="Add" onClick={() => { selectRoleId(""); }} />);
105
+ }
26
106
}
27
107
28
108
const getRows = ( ) => {
0 commit comments