#20 Refetch AllPersons on changeRole

Cache update seems too complicated since there is more
than one field "allPerson" when only the cached query
witch sorts by role needs to be updated.

Automatically refetching the query solves the problem
sufficiently good.
This commit is contained in:
Christoph Lienhard 2021-06-13 11:54:16 +02:00
parent 1f895d4bad
commit f0738aca27
Signed by: christoph.lienhard
GPG key ID: 6B98870DDC270884

View file

@ -2,31 +2,30 @@ import React from "react";
import { IconButton, MenuItem } from "@material-ui/core"; import { IconButton, MenuItem } from "@material-ui/core";
import Menu from "@material-ui/core/Menu"; import Menu from "@material-ui/core/Menu";
import EditIcon from "@material-ui/icons/Edit"; import EditIcon from "@material-ui/icons/Edit";
import { UppercaseUserRole, UPPERCASE_USER_ROLES } from "../jwt/jwt"; import { UPPERCASE_USER_ROLES, UppercaseUserRole } from "../jwt/jwt";
import { useMutation } from "@apollo/client"; import { useMutation } from "@apollo/client";
import { useHistory } from "react-router-dom";
import { import {
CHANGE_ROLE,
ChangeRoleResponse, ChangeRoleResponse,
ChangeRoleVariables, ChangeRoleVariables,
CHANGE_ROLE,
} from "../backend/mutations/userRole"; } from "../backend/mutations/userRole";
import { useSnackbar } from "notistack";
import { GET_PERSONS_SORTED_BY_ROLE } from "../backend/queries/person";
interface ChangeRoleProps { interface ChangeRoleProps {
currentRole: UppercaseUserRole; currentRole: UppercaseUserRole;
currentUserRowId: number; currentUserRowId: number;
} }
export default function ChangeRole(props: ChangeRoleProps): React.ReactElement { export default function ChangeRole(props: ChangeRoleProps): React.ReactElement {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null); const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const open = Boolean(anchorEl); const open = Boolean(anchorEl);
const history = useHistory(); const { enqueueSnackbar } = useSnackbar();
const otherRoles = UPPERCASE_USER_ROLES.filter( const otherRoles = UPPERCASE_USER_ROLES.filter(
(role) => role != props.currentRole (role) => role != props.currentRole
); );
const handleMenu = (event: React.MouseEvent<HTMLElement>) => { const handleMenu = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget); setAnchorEl(event.currentTarget);
}; };
const handleClose = () => { const handleClose = () => {
setAnchorEl(null); setAnchorEl(null);
}; };
@ -34,21 +33,22 @@ export default function ChangeRole(props: ChangeRoleProps): React.ReactElement {
CHANGE_ROLE, CHANGE_ROLE,
{ {
onCompleted() { onCompleted() {
console.log("Role changed");
history.go(0);
handleClose(); handleClose();
}, },
onError(e) { onError(e) {
console.error(e); console.error(e);
handleClose(); handleClose();
enqueueSnackbar("Ein Fehler ist aufgetreten, versuche es erneut.", {
variant: "error",
});
}, },
refetchQueries: [{ query: GET_PERSONS_SORTED_BY_ROLE }],
} }
); );
const displayRole = (role: UppercaseUserRole) => { const displayRole = (role: UppercaseUserRole) => {
switch (role) { switch (role) {
case "CANDYMAT_CANDIDATE": case "CANDYMAT_CANDIDATE":
return "zu * machen"; return "zu Kandidat:in machen";
case "CANDYMAT_EDITOR": case "CANDYMAT_EDITOR":
return "zu RedakteurIn machen"; return "zu RedakteurIn machen";
case "CANDYMAT_PERSON": case "CANDYMAT_PERSON":