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

97.14% Statements 34/35
91.66% Branches 11/12
90% Functions 9/10
97.14% Lines 34/35

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114  2x 2x 2x 2x 2x     2x 2x     2x 2x   2x 2x 2x           35x 66x 66x           66x   66x     66x   66x   66x       66x     66x 84x     66x           1x   1x                       5x       84x         1x 1x                     1x                 1x 1x   1x                  
import { Remove } from "@mui/icons-material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Snackbar from "components/Snackbar";
import RemoveAsCoOrganizerDialog from "features/communities/events/RemoveAsCoOrganizerDialog";
import { eventOrganizersKey } from "features/queryKeys";
import useCurrentUser from "features/userQueries/useCurrentUser";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import { LiteUser } from "proto/api_pb";
import { Event } from "proto/events_pb";
import { useState } from "react";
import { service } from "service";
 
import EventOrganizersDialog from "./EventOrganizersDialog";
import EventUsers from "./EventUsers";
import { useEventOrganizers } from "./hooks";
 
interface EventOrganizersProps {
  event: Event.AsObject;
}
 
export default function EventOrganizers({ event }: EventOrganizersProps) {
  const { t } = useTranslation([COMMUNITIES]);
  const queryClient = useQueryClient();
 
  const {
    error: organizerIdsError,
    hasNextPage,
    organizerIds,
  } = useEventOrganizers({ eventId: event.eventId, type: "summary" });
 
  const currentUser = useCurrentUser();
 
  const isCreatedByCurrentUser =
    currentUser.data?.userId === event.creatorUserId;
 
  const [isDialogOpen, setIsDialogOpen] = useState(false);
 
  const [organizerToRemove, setOrganizerToRemove] = useState<
    undefined | LiteUser.AsObject
  >();
 
  const [isCoOrganizerDialogOpen, setIsCoOrganizerDialogOpen] = useState(false);
 
  // Organizers can remove themselves, creator can remove organizers
  const canBeRemovedByCurrentUser = (user: LiteUser.AsObject) =>
    (isCreatedByCurrentUser && currentUser.data?.userId !== user.userId) ||
    (!isCreatedByCurrentUser && currentUser.data?.userId === user.userId);
 
  const { mutate: removeAsEventOrganizer, error: mutationError } = useMutation<
    Empty.AsObject,
    RpcError,
    number
  >({
    mutationFn: (userId) =>
      service.events.removeEventOrganizer(event.eventId, userId),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: eventOrganizersKey({ eventId: event.eventId, type: "all" }),
      });
    },
  });
 
  return (
    <>
      <EventUsers
        emptyState={t("communities:no_organizers")}
        error={organizerIdsError}
        hasNextPage={hasNextPage}
        onSeeAllClick={() => setIsDialogOpen(true)}
        userIds={organizerIds}
        title={t("communities:organizers")}
        getUserMenuItems={(user) =>
          canBeRemovedByCurrentUser(user)
            ? [
                {
                  icon: Remove,
                  onClick: () => {
                    setOrganizerToRemove(user);
                    setIsCoOrganizerDialogOpen(true);
                  },
                  label: t("communities:remove_as_co_organizer.title"),
                },
              ]
            : undefined
        }
      />
      <EventOrganizersDialog
        eventId={event.eventId}
        open={isDialogOpen}
        onClose={() => setIsDialogOpen(false)}
      />
 
      <RemoveAsCoOrganizerDialog
        username={organizerToRemove?.name ?? ""}
        eventName={event.title ?? ""}
        open={isCoOrganizerDialogOpen}
        onClose={() => setIsCoOrganizerDialogOpen(false)}
        onSubmit={() => {
          if (organizerToRemove) {
            removeAsEventOrganizer(organizerToRemove.userId);
          }
          setIsCoOrganizerDialogOpen(false);
        }}
      />
      {mutationError && (
        <Snackbar severity="error">{mutationError.message}</Snackbar>
      )}
    </>
  );
}