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

69.56% Statements 16/23
20% Branches 1/5
20% Functions 1/5
76.19% Lines 16/21

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 691x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x   2x         2x 2x 2x                     2x                                                                        
import { DialogProps, Link as MuiLink } 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 { howToInviteCommunityUrl } from "routes";
import { service } from "service";
 
export default function InviteCommunityDialog({
  eventId,
  afterSuccess,
  ...props
}: DialogProps & { eventId: number; afterSuccess: () => void }) {
  const { t } = useTranslation([GLOBAL, COMMUNITIES]);
  const queryClient = useQueryClient();
  const inviteCommunityMutation = useMutation<Empty, RpcError, void>({
    mutationFn: () => service.events.RequestCommunityInvite(eventId),
    onSuccess: () => {
      afterSuccess();
      queryClient.invalidateQueries({
        queryKey: eventKey(eventId),
      });
      if (props.onClose) props.onClose({}, "escapeKeyDown");
    },
  });
 
  const inviteCommunity = () => inviteCommunityMutation.mutate();
 
  return (
    <Dialog {...props} aria-labelledby="invite-community-dialog-title">
      <DialogTitle id="invite-community-dialog-title">{t("communities:invite_community_dialog.title")}</DialogTitle>
      <DialogContent>
        {inviteCommunityMutation.error && <Alert severity="error">{inviteCommunityMutation.error?.message}</Alert>}
        <DialogContentText>
          {t("communities:invite_community_dialog.message")}
          <br />
          <br />
          <MuiLink
            key={"link_invite_community"}
            target="_blank"
            rel="noreferrer"
            href={howToInviteCommunityUrl}
            underline="hover"
          >
            {t("communities:invite_community_dialog.link")}
          </MuiLink>
        </DialogContentText>
      </DialogContent>
      <DialogActions>
        <Button onClick={inviteCommunity} loading={inviteCommunityMutation.isPending}>
          {t("communities:invite_community_dialog_buttons.confirm")}
        </Button>
        <Button
          onClick={() => (props.onClose ? props.onClose({}, "escapeKeyDown") : null)}
          loading={inviteCommunityMutation.isPending}
        >
          {t("communities:invite_community_dialog_buttons.close")}
        </Button>
      </DialogActions>
    </Dialog>
  );
}