All files / app/features/dashboard MyEvents.tsx

100% Statements 30/30
100% Branches 15/15
100% Functions 7/7
100% Lines 29/29

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          1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 1x 1x 1x   5x                                                           1x   1x 15x 15x 15x 15x     15x     7x       28x     15x                                       9x   17x                           1x                        
import {
  CircularProgress,
  Typography,
  useMediaQuery,
  useTheme,
} from "@material-ui/core";
import classNames from "classnames";
import Alert from "components/Alert";
import Button from "components/Button";
import HorizontalScroller from "components/HorizontalScroller";
import TextBody from "components/TextBody";
import { useCommunityPageStyles } from "features/communities/CommunityPage";
import EventCard from "features/communities/events/EventCard";
import { myEventsKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { COMMUNITIES, DASHBOARD } from "i18n/namespaces";
import { ListMyEventsRes } from "proto/events_pb";
import { useInfiniteQuery } from "react-query";
import { service } from "service";
import hasAtLeastOnePage from "utils/hasAtLeastOnePage";
import makeStyles from "utils/makeStyles";
 
const useStyles = makeStyles((theme) => ({
  root: {
    display: "grid",
    justifyItems: "start",
    rowGap: theme.spacing(2),
    margin: theme.spacing(2, 0, 3),
  },
  upcomingEventContainer: {
    [theme.breakpoints.up("sm")]: {
      display: "grid",
      gridGap: theme.spacing(3),
      gridTemplateColumns: "repeat(2, 1fr)",
    },
  },
  eventCard: {
    width: "90%",
    [theme.breakpoints.up("sm")]: {
      width: "auto",
    },
  },
  allUpcomingEventsLink: {
    justifySelf: "center",
  },
  loaderContainer: {
    display: "flex",
    justifyContent: "center",
    width: "100%",
  },
}));
 
const PAGE_SIZE = 2;
 
export default function MyEvents() {
  const { t } = useTranslation([COMMUNITIES, DASHBOARD]);
  const classes = { ...useCommunityPageStyles(), ...useStyles() };
  const theme = useTheme();
  const isBelowSm = useMediaQuery(theme.breakpoints.down("xs"));
 
  const { data, error, fetchNextPage, hasNextPage, isFetching, isLoading } =
    useInfiniteQuery<ListMyEventsRes.AsObject, RpcError>({
      queryKey: myEventsKey,
      queryFn: ({ pageParam }) =>
        service.events.listMyEvents({
          pageToken: pageParam,
          pageSize: PAGE_SIZE,
        }),
      getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined,
    });
 
  return (
    <div className={classes.root}>
      <Typography variant="h2">{t("dashboard:upcoming_events")}</Typography>
      {error && <Alert severity="error">{error.message}</Alert>}
      {isLoading ? (
        <div className={classes.loaderContainer}>
          <CircularProgress />
        </div>
      ) : hasAtLeastOnePage(data, "eventsList") ? (
        <>
          <HorizontalScroller
            className={classNames(
              classes.cardContainer,
              classes.upcomingEventContainer
            )}
            fetchNext={isBelowSm ? fetchNextPage : undefined}
            hasMore={hasNextPage}
            isFetching={isFetching}
          >
            {data.pages
              .flatMap((page) => page.eventsList)
              .map((event) => {
                return (
                  <EventCard
                    key={event.eventId}
                    event={event}
                    className={classNames(
                      classes.placeEventCard,
                      classes.eventCard
                    )}
                  />
                );
              })}
          </HorizontalScroller>
          {hasNextPage && !isBelowSm && (
            <div className={classes.loaderContainer}>
              <Button onClick={() => fetchNextPage()} variant="outlined">
                {t("communities:see_more_events_label")}
              </Button>
            </div>
          )}
        </>
      ) : (
        !error && <TextBody>{t("communities:events_empty_state")}</TextBody>
      )}
    </div>
  );
}