-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from zilliztech/user_roles
Support create/edit user role on user page
- Loading branch information
Showing
12 changed files
with
444 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { | ||
makeStyles, | ||
Theme, | ||
Checkbox, | ||
FormGroup, | ||
FormControlLabel, | ||
} from '@material-ui/core'; | ||
import { FC, useState, useEffect } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import DialogTemplate from '@/components/customDialog/DialogTemplate'; | ||
import { UpdateUserRoleProps, UpdateUserRoleParams } from './Types'; | ||
import { UserHttp } from '@/http/User'; | ||
|
||
const useStyles = makeStyles((theme: Theme) => ({ | ||
input: { | ||
margin: theme.spacing(2, 0, 0.5), | ||
}, | ||
dialogWrapper: { | ||
maxWidth: theme.spacing(70), | ||
'& .MuiFormControlLabel-root': { | ||
width: theme.spacing(20), | ||
}, | ||
}, | ||
})); | ||
|
||
const UpdateUserRole: FC<UpdateUserRoleProps> = ({ | ||
onUpdate, | ||
handleClose, | ||
roles, | ||
username, | ||
}) => { | ||
const { t: userTrans } = useTranslation('user'); | ||
const { t: btnTrans } = useTranslation('btn'); | ||
const [roleOptions, setRoleOptions] = useState([]); | ||
|
||
const [form, setForm] = useState<UpdateUserRoleParams>({ | ||
username: username, | ||
roles: roles, | ||
}); | ||
|
||
const classes = useStyles(); | ||
|
||
const handleUpdate = async () => { | ||
await UserHttp.updateUserRole(form); | ||
onUpdate(form); | ||
}; | ||
|
||
const fetchAllRoles = async () => { | ||
const roles = await UserHttp.getRoles(); | ||
|
||
setRoleOptions(roles.results.map((r: any) => r.role.name)); | ||
}; | ||
|
||
useEffect(() => { | ||
fetchAllRoles(); | ||
}, []); | ||
|
||
return ( | ||
<DialogTemplate | ||
title={userTrans('updateRoleTitle')} | ||
handleClose={handleClose} | ||
confirmLabel={btnTrans('update')} | ||
handleConfirm={handleUpdate} | ||
confirmDisabled={false} | ||
dialogClass={classes.dialogWrapper} | ||
> | ||
<> | ||
<FormGroup row> | ||
{roleOptions.map((roleOption: string, index: number) => ( | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
onChange={( | ||
e: React.ChangeEvent<HTMLInputElement>, | ||
checked: boolean | ||
) => { | ||
let newRoles = [...form.roles]; | ||
|
||
if (!checked) { | ||
newRoles = newRoles.filter( | ||
(n: string | number) => n !== roleOption | ||
); | ||
} else { | ||
newRoles.push(roleOption); | ||
} | ||
|
||
setForm(v => ({ ...v, roles: [...newRoles] })); | ||
}} | ||
/> | ||
} | ||
key={index} | ||
label={roleOption} | ||
value={roleOption} | ||
checked={form.roles.indexOf(roleOption) !== -1} | ||
/> | ||
))} | ||
</FormGroup> | ||
</> | ||
</DialogTemplate> | ||
); | ||
}; | ||
|
||
export default UpdateUserRole; |
Oops, something went wrong.