From 1779be2b39989055e7e684ea7aa1b98aa50c94d9 Mon Sep 17 00:00:00 2001 From: plohner Date: Sat, 29 May 2021 17:19:11 +0200 Subject: [PATCH] changeRole implemented in UserManagement --- candymat-user-app | 2 +- .../src/backend/mutations/userRole.ts | 2 +- redaktions-app/src/components/ChangeRole.tsx | 32 +++++++--- redaktions-app/src/components/PersonCard.tsx | 5 +- .../src/components/ProfileManagement.tsx | 62 +++++++++++++++++++ .../src/components/UserManagement.tsx | 1 + 6 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 redaktions-app/src/components/ProfileManagement.tsx diff --git a/candymat-user-app b/candymat-user-app index d414b95..4e29044 160000 --- a/candymat-user-app +++ b/candymat-user-app @@ -1 +1 @@ -Subproject commit d414b95c1c664adcd5149aee8eac4436b40d7dfb +Subproject commit 4e290449cb4b5821f5542d1ea6a9a629b99e6aa3 diff --git a/redaktions-app/src/backend/mutations/userRole.ts b/redaktions-app/src/backend/mutations/userRole.ts index 7836b60..088574f 100644 --- a/redaktions-app/src/backend/mutations/userRole.ts +++ b/redaktions-app/src/backend/mutations/userRole.ts @@ -2,7 +2,7 @@ import { gql } from "@apollo/client"; import { UppercaseUserRole } from "../../jwt/jwt"; import { BasicPersonFragment, BasicPersonResponse } from "../queries/person"; -export const ChangeRole = gql` +export const CHANGE_ROLE = gql` mutation ChangeRole($personRowId: Int!, $newRole: Role!) { changeRole(input: { personRowId: $personRowId, newRole: $newRole }) { person { diff --git a/redaktions-app/src/components/ChangeRole.tsx b/redaktions-app/src/components/ChangeRole.tsx index e1b23d6..83a7ce9 100644 --- a/redaktions-app/src/components/ChangeRole.tsx +++ b/redaktions-app/src/components/ChangeRole.tsx @@ -3,9 +3,16 @@ import { IconButton, MenuItem } from "@material-ui/core"; import Menu from "@material-ui/core/Menu"; import EditIcon from "@material-ui/icons/Edit"; import { UppercaseUserRole, UPPERCASE_USER_ROLES } from "../jwt/jwt"; +import { useMutation } from "@apollo/client"; +import { + ChangeRoleResponse, + ChangeRoleVariables, + CHANGE_ROLE, +} from "../backend/mutations/userRole"; interface ChangeRoleProps { currentRole: UppercaseUserRole; + currentUserRowId: number; } export default function ChangeRole(props: ChangeRoleProps): React.ReactElement { @@ -22,12 +29,18 @@ export default function ChangeRole(props: ChangeRoleProps): React.ReactElement { const handleClose = () => { setAnchorEl(null); }; - - function changeRole(role: UppercaseUserRole) { - console.log("Role Selected: ", role); - alert("Die Rolle wurde geƤndert in: " + role); - handleClose(); - } + const [changeRole] = useMutation( + CHANGE_ROLE, + { + onCompleted() { + console.log("Role changed"); + }, + onError(e) { + console.error(e); + handleClose(); + }, + } + ); const displayRole = (role: UppercaseUserRole) => { switch (role) { @@ -69,7 +82,12 @@ export default function ChangeRole(props: ChangeRoleProps): React.ReactElement { { - changeRole(role); + changeRole({ + variables: { + personRowId: props.currentUserRowId, + newRole: role, + }, + }); }} > {displayRole(role)} diff --git a/redaktions-app/src/components/PersonCard.tsx b/redaktions-app/src/components/PersonCard.tsx index e1ece2d..1e54f1b 100644 --- a/redaktions-app/src/components/PersonCard.tsx +++ b/redaktions-app/src/components/PersonCard.tsx @@ -1,5 +1,4 @@ import { Avatar, Paper, Typography } from "@material-ui/core"; -import EditIcon from "@material-ui/icons/Edit"; import React from "react"; import { makeStyles } from "@material-ui/core/styles"; import ChangeRole from "./ChangeRole"; @@ -29,11 +28,13 @@ interface PersonCardProps { firstName: string; lastName: string; userRole: UppercaseUserRole; + userRowId: number; } export default function PersonCard(props: PersonCardProps): React.ReactElement { const classes = useStyles(); const userRole = props.userRole; + const currentUserRowId = props.userRowId; const getInitials = (): string => { const initials = (props.firstName.charAt(0) || "") + (props.lastName.charAt(0) || ""); @@ -47,7 +48,7 @@ export default function PersonCard(props: PersonCardProps): React.ReactElement { {getInitials()} {fullName} - + ); } diff --git a/redaktions-app/src/components/ProfileManagement.tsx b/redaktions-app/src/components/ProfileManagement.tsx new file mode 100644 index 0000000..be09f0a --- /dev/null +++ b/redaktions-app/src/components/ProfileManagement.tsx @@ -0,0 +1,62 @@ +import React from "react"; +import { Paper, Typography } from "@material-ui/core"; +import { makeStyles } from "@material-ui/core/styles"; +import PersonCard from "./PersonCard"; +import { useQuery } from "@apollo/client"; +import { + BasicPersonResponse, + GET_PERSONS_SORTED_BY_ROLE, + GetPersonsSortedByRoleResponse, +} from "../backend/queries/person"; + +const useStyles = makeStyles((theme) => ({ + root: { + width: "100%", + padding: theme.spacing(1), + marginBottom: theme.spacing(3), + }, +})); + +export function ProfileManagement(): React.ReactElement { + const classes = useStyles(); + const persons = useQuery( + GET_PERSONS_SORTED_BY_ROLE + ).data; + + const convertPersonNodeToPersonCard = ( + person: BasicPersonResponse + ): JSX.Element => { + return ( + + ); + }; + + return ( +
+ + + Ertellen Sie hier ihr Profil + + {persons?.editors.nodes.map(convertPersonNodeToPersonCard)} + + + + KandidatInnen + + {persons?.candidates.nodes.map(convertPersonNodeToPersonCard)} + + + + Andere registrierte Personen + + {persons?.users.nodes.map(convertPersonNodeToPersonCard)} + +
+ ); +} diff --git a/redaktions-app/src/components/UserManagement.tsx b/redaktions-app/src/components/UserManagement.tsx index da0622f..f7cab50 100644 --- a/redaktions-app/src/components/UserManagement.tsx +++ b/redaktions-app/src/components/UserManagement.tsx @@ -32,6 +32,7 @@ export function UserManagement(): React.ReactElement { firstName={person.firstName || ""} lastName={person.lastName || ""} userRole={person.role} + userRowId={person.rowId} /> ); };