All files / app/features/communities/events hooks.ts

85.29% Statements 29/34
68.75% Branches 11/16
82.35% Functions 14/17
87.87% Lines 29/33

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171              5x                   5x 5x                   5x   79x         79x     22x         95x     79x 34x     79x     79x         79x     23x         98x     79x 34x   79x           22x 22x   22x   10x       22x                                                 3x           3x             1x                   3x                             3x                   1x                    
import {
  eventAttendeesKey,
  eventKey,
  eventOrganizersKey,
  eventsKey,
  myEventsKey,
  QueryType,
} from "features/queryKeys";
import { RpcError } from "grpc-web";
import {
  Event,
  ListAllEventsRes,
  ListEventAttendeesRes,
  ListEventOrganizersRes,
  ListMyEventsRes,
} from "proto/events_pb";
import { EventSearchRes } from "proto/search_pb";
import { useInfiniteQuery, useQuery } from "react-query";
import { service } from "service";
import type { ListAllEventsInput, ListMyEventsInput } from "service/events";
import { GeocodeResult } from "utils/hooks";
 
export interface UseEventUsersInput {
  eventId: number;
  type: QueryType;
  enabled?: boolean;
}
 
export const SUMMARY_QUERY_PAGE_SIZE = 5;
 
export function useEventOrganizers({
  enabled = true,
  eventId,
  type,
}: UseEventUsersInput) {
  const query = useInfiniteQuery<ListEventOrganizersRes.AsObject, RpcError>({
    queryKey: eventOrganizersKey({ eventId, type }),
    queryFn: ({ pageParam }) =>
      service.events.listEventOrganizers({
        eventId,
        pageSize: type === "summary" ? SUMMARY_QUERY_PAGE_SIZE : undefined,
        pageToken: pageParam,
      }),
    getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined,
    enabled,
  });
  const organizerIds = query.data?.pages.flatMap(
    (res) => res.organizerUserIdsList
  );
 
  return { ...query, organizerIds };
}
 
export function useEventAttendees({
  enabled = true,
  eventId,
  type,
}: UseEventUsersInput) {
  const query = useInfiniteQuery<ListEventAttendeesRes.AsObject, RpcError>({
    queryKey: eventAttendeesKey({ eventId, type }),
    queryFn: ({ pageParam }) =>
      service.events.listEventAttendees({
        eventId,
        pageSize: type === "summary" ? SUMMARY_QUERY_PAGE_SIZE : undefined,
        pageToken: pageParam,
      }),
    getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined,
    enabled,
  });
  const attendeesIds = query.data?.pages.flatMap(
    (data) => data.attendeeUserIdsList
  );
  return {
    ...query,
    attendeesIds,
  };
}
 
export function useEvent({ eventId }: { eventId: number }) {
  const isValidEventId = eventId > 0;
 
  const eventQuery = useQuery<Event.AsObject, RpcError>({
    queryKey: eventKey(eventId),
    queryFn: () => service.events.getEvent(eventId),
    enabled: isValidEventId,
  });
 
  return {
    ...eventQuery,
    eventId,
    isValidEventId,
  };
}
 
export function useListAllEvents({
  pastEvents,
  pageSize,
  showCancelled,
}: Omit<ListAllEventsInput, "pageToken">) {
  return useInfiniteQuery<ListAllEventsRes.AsObject, RpcError>({
    queryKey: [eventsKey(pastEvents ? "past" : "upcoming"), showCancelled],
    queryFn: ({ pageParam }) =>
      service.events.listAllEvents({
        pastEvents,
        pageSize,
        pageToken: pageParam,
        showCancelled,
      }),
    getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined,
  });
}
 
export function useListMyEvents({
  pastEvents,
  pageNumber,
  pageSize,
  showCancelled,
}: Omit<ListMyEventsInput, "pageToken">) {
  return useQuery<ListMyEventsRes.AsObject, RpcError>({
    queryKey: [
      myEventsKey(pastEvents ? "past" : "upcoming"),
      pageNumber,
      showCancelled,
    ],
    queryFn: ({ pageParam }) =>
      service.events.listMyEvents({
        pastEvents,
        pageNumber,
        pageSize,
        pageToken: pageParam,
        showCancelled,
      }),
  });
}
 
export function useEventSearch({
  pageNumber,
  pageSize,
  pastEvents,
  isMyCommunities,
  isOnlineOnly,
  searchLocation,
}: {
  pageNumber: number;
  pageSize: number;
  pastEvents?: boolean;
  isMyCommunities?: boolean;
  isOnlineOnly?: boolean;
  searchLocation?: GeocodeResult | "";
}) {
  return useQuery<EventSearchRes.AsObject, RpcError>({
    queryKey: [
      "searchEvents",
      isMyCommunities,
      isOnlineOnly,
      pageNumber,
      pastEvents,
      searchLocation,
    ],
    queryFn: () =>
      service.search.EventSearch({
        pageNumber,
        pageSize,
        pastEvents,
        isMyCommunities,
        isOnlineOnly,
        searchLocation,
      }),
  });
}