All files / app/features/communities/events CancelEventDialog.tsx

70% Statements 14/20
20% Branches 1/5
20% Functions 1/5
77.77% Lines 14/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51  1x 1x 1x 1x 1x     1x 1x 1x 1x   2x 2x 2x 2x                   2x                                                
import { DialogProps } from "@mui/material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import { Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from "components/Dialog";
import { eventKey } from "features/queryKeys";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { COMMUNITIES, GLOBAL } from "i18n/namespaces";
import React from "react";
import { service } from "service";
 
export default function CancelEventDialog({ eventId, ...props }: DialogProps & { eventId: number }) {
  const { t } = useTranslation([GLOBAL, COMMUNITIES]);
  const queryClient = useQueryClient();
  const cancelEventMutation = useMutation<Empty, RpcError, void>({
    mutationFn: () => service.events.cancelEvent(eventId),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: eventKey(eventId),
      });
      if (props.onClose) props.onClose({}, "escapeKeyDown");
    },
  });
 
  const handleCancelEvent = () => cancelEventMutation.mutate();
 
  return (
    <Dialog {...props} aria-labelledby="cancel-event-dialog-title">
      <DialogTitle id="cancel-event-dialog-title">{t("communities:cancel_event_dialog.title")}</DialogTitle>
      <DialogContent>
        {cancelEventMutation.error && <Alert severity="error">{cancelEventMutation.error?.message}</Alert>}
        <DialogContentText>{t("communities:cancel_event_dialog.message")}</DialogContentText>
      </DialogContent>
      <DialogActions>
        <Button
          variant="outlined"
          onClick={() => (props.onClose ? props.onClose({}, "escapeKeyDown") : null)}
          loading={cancelEventMutation.isPending}
        >
          {t("global:no")}
        </Button>
        <Button onClick={handleCancelEvent} loading={cancelEventMutation.isPending}>
          {t("global:yes")}
        </Button>
      </DialogActions>
    </Dialog>
  );
}