|
| 1 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 2 | +import { Grid2 as Grid, MenuItem, Modal, Paper, Select } from '@mui/material'; |
| 3 | +import { Button } from 'components/common/Input/Button'; |
| 4 | +import { BodyText, Heading3 } from 'components/common/Typography'; |
| 5 | +import { modalStyle } from 'components/common'; |
| 6 | +import { useAppDispatch, useAppSelector } from 'hooks'; |
| 7 | +import { USER_COMPOSITE_ROLE, User } from 'models/user'; |
| 8 | +import { addUserToRole, changeUserRole } from 'services/userService/api'; |
| 9 | +import { openNotification } from 'services/notificationService/notificationSlice'; |
| 10 | +import { USER_ROLES } from 'services/userService/constants'; |
| 11 | + |
| 12 | +const roleOptions = [ |
| 13 | + USER_COMPOSITE_ROLE.ADMIN, |
| 14 | + USER_COMPOSITE_ROLE.VIEWER, |
| 15 | + USER_COMPOSITE_ROLE.TEAM_MEMBER, |
| 16 | + USER_COMPOSITE_ROLE.REVIEWER, |
| 17 | +]; |
| 18 | + |
| 19 | +interface RoleAssignmentModalProps { |
| 20 | + open: boolean; |
| 21 | + user?: User; |
| 22 | + onClose: () => void; |
| 23 | + onSaved?: () => Promise<void> | void; |
| 24 | +} |
| 25 | + |
| 26 | +export const RoleAssignmentModal = ({ open, user, onClose, onSaved }: RoleAssignmentModalProps) => { |
| 27 | + const dispatch = useAppDispatch(); |
| 28 | + const { roles } = useAppSelector((state) => state.user); |
| 29 | + const [selectedRole, setSelectedRole] = useState(''); |
| 30 | + const [isSaving, setIsSaving] = useState(false); |
| 31 | + |
| 32 | + const isSuperAdmin = roles.includes(USER_ROLES.SUPER_ADMIN); |
| 33 | + const isAdminUser = user?.main_role === USER_COMPOSITE_ROLE.ADMIN.label; |
| 34 | + const isReassignment = Boolean(user?.main_role); |
| 35 | + |
| 36 | + const initialRoleValue = useMemo( |
| 37 | + () => roleOptions.find((role) => role.label === user?.main_role)?.value || '', |
| 38 | + [user?.main_role], |
| 39 | + ); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (!open) { |
| 43 | + setSelectedRole(''); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + setSelectedRole(initialRoleValue); |
| 48 | + }, [initialRoleValue, open]); |
| 49 | + |
| 50 | + const handleClose = () => { |
| 51 | + if (isSaving) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + setSelectedRole(''); |
| 56 | + onClose(); |
| 57 | + }; |
| 58 | + |
| 59 | + const handleSave = async () => { |
| 60 | + if (!user || !selectedRole) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (isAdminUser && !isSuperAdmin) { |
| 65 | + dispatch( |
| 66 | + openNotification({ |
| 67 | + severity: 'error', |
| 68 | + text: 'Only Super Admins can reassign an Administrator to a lower role.', |
| 69 | + }), |
| 70 | + ); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + setIsSaving(true); |
| 76 | + |
| 77 | + if (user.main_role) { |
| 78 | + await changeUserRole({ |
| 79 | + user_id: user.id, |
| 80 | + role: selectedRole, |
| 81 | + }); |
| 82 | + } else { |
| 83 | + await addUserToRole({ |
| 84 | + user_id: user.external_id, |
| 85 | + role: selectedRole, |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + await onSaved?.(); |
| 90 | + handleClose(); |
| 91 | + |
| 92 | + const selectedRoleLabel = roleOptions.find((role) => role.value === selectedRole)?.label || selectedRole; |
| 93 | + dispatch( |
| 94 | + openNotification({ |
| 95 | + severity: 'success', |
| 96 | + text: isReassignment |
| 97 | + ? `You have reassigned ${user.first_name} ${user.last_name} as ${selectedRoleLabel}.` |
| 98 | + : `You have successfully added ${user.first_name} ${user.last_name} to the role ${selectedRoleLabel}.`, |
| 99 | + }), |
| 100 | + ); |
| 101 | + } catch { |
| 102 | + dispatch(openNotification({ severity: 'error', text: 'Failed to update user role.' })); |
| 103 | + } finally { |
| 104 | + setIsSaving(false); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + return ( |
| 109 | + <Modal open={open} onClose={handleClose} keepMounted={false}> |
| 110 | + <Paper sx={{ ...modalStyle }}> |
| 111 | + <Grid container spacing={2}> |
| 112 | + <Grid size={12}> |
| 113 | + <Heading3 bold>{isReassignment ? 'Reassign Role' : 'Assign Role'}</Heading3> |
| 114 | + </Grid> |
| 115 | + <Grid size={12}> |
| 116 | + <BodyText> |
| 117 | + Choose a role for {user?.first_name} {user?.last_name}. |
| 118 | + </BodyText> |
| 119 | + </Grid> |
| 120 | + <Grid size={12}> |
| 121 | + <Select |
| 122 | + fullWidth |
| 123 | + value={selectedRole} |
| 124 | + onChange={(event) => setSelectedRole(event.target.value)} |
| 125 | + > |
| 126 | + {roleOptions.map((roleOption) => ( |
| 127 | + <MenuItem |
| 128 | + key={roleOption.value} |
| 129 | + value={roleOption.value} |
| 130 | + disabled={ |
| 131 | + roleOption.value === USER_COMPOSITE_ROLE.ADMIN.value && |
| 132 | + !roles.includes(USER_ROLES.SUPER_ADMIN) |
| 133 | + } |
| 134 | + > |
| 135 | + {roleOption.label} |
| 136 | + </MenuItem> |
| 137 | + ))} |
| 138 | + </Select> |
| 139 | + </Grid> |
| 140 | + <Grid container size={12} justifyContent="flex-end" spacing={1}> |
| 141 | + <Grid size="auto"> |
| 142 | + <Button onClick={handleClose} disabled={isSaving}> |
| 143 | + Cancel |
| 144 | + </Button> |
| 145 | + </Grid> |
| 146 | + <Grid size="auto"> |
| 147 | + <Button |
| 148 | + variant="primary" |
| 149 | + onClick={handleSave} |
| 150 | + loading={isSaving} |
| 151 | + disabled={!selectedRole || selectedRole === initialRoleValue} |
| 152 | + > |
| 153 | + Save |
| 154 | + </Button> |
| 155 | + </Grid> |
| 156 | + </Grid> |
| 157 | + </Grid> |
| 158 | + </Paper> |
| 159 | + </Modal> |
| 160 | + ); |
| 161 | +}; |
0 commit comments