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

92.85% Statements 52/56
82.6% Branches 19/23
86.66% Functions 13/15
94.33% Lines 50/53

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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147  2x   2x 2x 2x 2x     2x 2x     2x 2x   2x 2x           2x   40x 70x           70x 70x   70x   70x 1x     70x 3x         3x 3x 3x       70x           70x 70x   70x 47x 94x       70x     70x   70x 70x       70x 70x   70x   70x   70x 1x   1x           70x 20x 3x     17x       1x 1x                                                               1x 1x   1x              
import { Add } from "@mui/icons-material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { EllipsisMenuItem } from "components/EllipsisMenu";
import Snackbar from "components/Snackbar";
import MakeCoOrganizerDialog from "features/communities/events/MakeCoOrganizerDialog";
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 { useMemo, useState } from "react";
import { service } from "service";
 
import EventUsers from "./EventUsers";
import { useEventAttendees, useEventOrganizers } from "./hooks";
 
interface EventAttendeesProps {
  event: Event.AsObject;
}
 
const PAGE_SIZE = 9;
 
export default function EventAttendees({ event }: EventAttendeesProps) {
  const { data, error, hasNextPage, fetchNextPage, isLoading } = useEventAttendees({
    eventId: event.eventId,
    type: "summary",
    pageSize: PAGE_SIZE,
  });
 
  const [pageIndex, setPageIndex] = useState(0);
  const currentPage = data?.pages?.[pageIndex];
 
  const pagesLength = data?.pages.length ?? 0;
 
  const handlePreviousPageClick = () => {
    setPageIndex((current) => Math.max(current - 1, 0));
  };
 
  const handleNextPageClick = async () => {
    Iif (pageIndex < pagesLength - 1) {
      setPageIndex((current) => current + 1);
      return;
    }
 
    Eif (hasNextPage) {
      await fetchNextPage();
      setPageIndex((current) => current + 1);
    }
  };
 
  const { organizerIds } = useEventOrganizers({
    eventId: event.eventId,
    type: "all",
  });
 
  // Optimize searching for organizer ids
  const organizerIdSet = useMemo(() => {
    const set = new Set<number>();
 
    if (organizerIds) {
      organizerIds.forEach((id) => {
        set.add(id);
      });
    }
 
    return set;
  }, [organizerIds]);
 
  const currentUser = useCurrentUser();
 
  const isCoOrganizedByCurrentUser = useMemo(
    () => currentUser.data && organizerIdSet.has(currentUser.data.userId),
    [currentUser.data, organizerIdSet],
  );
 
  const { t } = useTranslation([COMMUNITIES]);
  const queryClient = useQueryClient();
 
  const [userToPromote, setUserToPromote] = useState<undefined | LiteUser.AsObject>();
 
  const [isCoOrganizerDialogOpen, setIsCoOrganizerDialogOpen] = useState(false);
 
  const { mutate: makeEventOrganizer, error: mutationError } = useMutation<Empty.AsObject, RpcError, number>({
    mutationFn: (userId) => service.events.inviteEventOrganizer(event.eventId, userId),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: eventOrganizersKey({ eventId: event.eventId, type: "all" }),
      });
    },
  });
 
  const getUserMenuItems = (user: LiteUser.AsObject): EllipsisMenuItem[] | undefined => {
    if (user.userId === currentUser.data?.userId || organizerIdSet.has(user.userId)) {
      return undefined;
    }
 
    return [
      {
        icon: Add,
        onClick: () => {
          setUserToPromote(user);
          setIsCoOrganizerDialogOpen(true);
        },
        label: t("communities:make_co_organizer.title"),
      },
    ];
  };
 
  return (
    <>
      <EventUsers
        emptyState={t("communities:no_attendees")}
        error={error}
        hasNextPage={hasNextPage}
        userIds={currentPage?.attendeeUserIdsList}
        title={t("communities:attendees")}
        layout="grid"
        isLoading={isLoading}
        pagination={{
          pageIndex: pageIndex,
          currentPage: currentPage,
          handlePreviousPageClick: handlePreviousPageClick,
          handleNextPageClick: handleNextPageClick,
        }}
        getUserMenuItems={isCoOrganizedByCurrentUser ? getUserMenuItems : undefined}
        attendeeCount={event.goingCount}
      />
      <MakeCoOrganizerDialog
        username={userToPromote?.name ?? ""}
        eventName={event.title ?? ""}
        open={isCoOrganizerDialogOpen}
        onClose={() => setIsCoOrganizerDialogOpen(false)}
        onSubmit={() => {
          Eif (userToPromote) {
            makeEventOrganizer(userToPromote.userId);
          }
          setIsCoOrganizerDialogOpen(false);
        }}
      />
      {mutationError && <Snackbar severity="error">{mutationError?.message}</Snackbar>}
    </>
  );
}