#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
1 changed files with 10 additions and 10 deletions

View File

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