Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
you can now add/remove roles
Browse files Browse the repository at this point in the history
  • Loading branch information
ALBERICLOOS committed May 23, 2024
1 parent ebd3e36 commit e43cdef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions frontend/src/pages/admin/HomeAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ function filter_on_not_role(users: User[], role: string): User[] {
return users.filter((user) => !user.user_roles.includes(role));
}

async function make_teacher(person: User, current_user_id: (number | undefined), setUsers: React.Dispatch<React.SetStateAction<User[]>>) {
const roles = [...person.user_roles, "TEACHER"];
async function adjust_role(typeOperation: OperationType, person: User, current_user_id: (number | undefined), role: string, setUsers: React.Dispatch<React.SetStateAction<User[]>>) {
let roles: string[];

switch (typeOperation) {
case OperationType.ADD:
roles = [...person.user_roles, role];
break;
case OperationType.REMOVE:
roles = person.user_roles.filter(r => r !== role);
break;
default:
roles = person.user_roles;
}

const response = await apiFetch(`/users/${person.user_id}`, {
method: 'PUT',
headers: {
Expand Down Expand Up @@ -54,7 +66,7 @@ export default function HomeAdmin(): JSX.Element {
<div className="person">
<Person name={person.user_name} operations={[
<OperationButton key={getKey()} type={OperationType.ADD}
action={() => void make_teacher(person, user?.user_id, setUsers)}/>,
action={() => void adjust_role(OperationType.ADD, person, user?.user_id, "TEACHER", setUsers)}/>,
]}/>
</div>
)
Expand All @@ -70,9 +82,8 @@ export default function HomeAdmin(): JSX.Element {
return (
<div className="person">
<Person name={person.user_name} operations={[
<OperationButton key={getKey()} type={OperationType.REMOVE} action={function () {
return undefined;
}}/>,
<OperationButton key={getKey()} type={OperationType.REMOVE}
action={() => void adjust_role(OperationType.REMOVE, person, user?.user_id, "ADMIN", setUsers)}/>,
]}/>
</div>
)
Expand All @@ -81,9 +92,8 @@ export default function HomeAdmin(): JSX.Element {
return (
<div className="person">
<Person name={person.user_name} operations={[
<OperationButton key={getKey()} type={OperationType.ADD} action={function () {
return undefined;
}}/>,
<OperationButton key={getKey()} type={OperationType.ADD}
action={() => void adjust_role(OperationType.ADD, person, user?.user_id, "ADMIN", setUsers)}/>,
]}/>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/admin/OperationButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function OperationButton(props: { type: OperationType, action: () => void
);
case OperationType.REMOVE:
return (
<button className="button is-small is-danger">
<button className="button is-small is-danger" onClick={props.action}>
<span className="icon">
<CgMathMinus/>
</span>
Expand Down

0 comments on commit e43cdef

Please sign in to comment.