All files / app/features/dashboard MyEvents.tsx

100% Statements 35/35
100% Branches 10/10
100% Functions 11/11
100% Lines 31/31

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 1421x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x   1x 1x 1x   7x                                           15x                               16x           1x           1x   7x 16x 16x     16x     9x           30x                                   9x   15x         15x               1x                                            
import { styled, Typography, useMediaQuery } from "@mui/material";
import { useInfiniteQuery } from "@tanstack/react-query";
import Alert from "components/Alert";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import HorizontalScroller from "components/HorizontalScroller";
import StyledLink from "components/StyledLink";
import TextBody from "components/TextBody";
import EventCard from "features/communities/events/EventCard";
import { RpcError } from "grpc-web";
import { Trans, useTranslation } from "i18n";
import { DASHBOARD } from "i18n/namespaces";
import { routeToNewEvent } from "routes";
import { theme } from "theme";
 
import Button from "../../components/Button";
import { ListMyEventsRes } from "../../proto/events_pb";
import { service } from "../../service";
import hasAtLeastOnePage from "../../utils/hasAtLeastOnePage";
import { myEventsKey } from "../queryKeys";
 
const StyledCardContainer = styled(HorizontalScroller)(() => ({
  paddingLeft: theme.spacing(1),
  paddingRight: theme.spacing(1),
  [theme.breakpoints.down("sm")]: {
    left: "50%",
    marginLeft: "-50vw",
    marginRight: "-50vw",
    position: "relative",
    right: "50%",
    width: "100vw",
  },
  [theme.breakpoints.up("md")]: {
    gridTemplateColumns: "repeat(3, 1fr)",
    gap: theme.spacing(3),
  },
  [theme.breakpoints.up("sm")]: {
    display: "grid",
    gap: theme.spacing(3),
    gridTemplateColumns: "repeat(2, 1fr)",
  },
}));
 
const StyledCard = styled(EventCard)(() => ({
  width: "90%",
  [theme.breakpoints.down("md")]: {
    width: "100%",
  },
  [theme.breakpoints.up("sm")]: {
    width: "100%",
  },
  [theme.breakpoints.down("sm")]: {
    margin: theme.spacing(0, 2, 1, 0),
  },
  flexShrink: 0,
  borderRadius: "var(--mui-shape-borderRadius) * 2",
  scrollSnapAlign: "start",
}));
 
const StyledWrapper = styled("div")(() => ({
  display: "grid",
  rowGap: theme.spacing(2),
  margin: theme.spacing(2, 0, 3),
}));
 
const StyledButtonContainer = styled("div")(() => ({
  display: "flex",
  justifyContent: "center",
  width: "100%",
}));
 
const PAGE_SIZE = 2;
 
export default function MyEvents() {
  const { t } = useTranslation([DASHBOARD]);
  const isBelowSm = useMediaQuery(theme.breakpoints.down("sm"));
 
  const { data, error, fetchNextPage, hasNextPage, isFetching, isLoading } =
    useInfiniteQuery<ListMyEventsRes.AsObject, RpcError>({
      queryKey: myEventsKey("upcoming"),
      queryFn: ({ pageParam }) =>
        service.events.listMyEvents({
          pageToken: pageParam as string | undefined,
          pageSize: PAGE_SIZE,
          myCommunities: true,
          myCommunitiesExcludeGlobal: true,
        }),
      getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined,
      initialPageParam: undefined,
    });
 
  return (
    <StyledWrapper>
      <Typography variant="h2">{t("dashboard:upcoming_events")}</Typography>
      {error && <Alert severity="error">{error.message}</Alert>}
      {isLoading ? (
        <CenteredSpinner />
      ) : hasAtLeastOnePage(data, "eventsList") ? (
        <>
          <StyledCardContainer
            fetchNext={isBelowSm ? fetchNextPage : undefined}
            hasMore={hasNextPage}
            isFetching={isFetching}
          >
            {data.pages
              .flatMap((page) => page.eventsList)
              .map((event) => {
                return (
                  <StyledCard
                    key={event.eventId}
                    event={event}
                    attendeesCountFormatter={(count) =>
                      t("dashboard:attendees_count", { count })
                    }
                  />
                );
              })}
          </StyledCardContainer>
          {hasNextPage && !isBelowSm && (
            <StyledButtonContainer>
              <Button onClick={() => fetchNextPage()}>
                {t("dashboard:load_more")}
              </Button>
            </StyledButtonContainer>
          )}
        </>
      ) : (
        !error && (
          <TextBody>
            <Trans
              t={t}
              i18nKey="dashboard:events_empty_state"
              components={[
                <StyledLink key="create-link" href={routeToNewEvent()} />,
              ]}
            />
          </TextBody>
        )
      )}
    </StyledWrapper>
  );
}