All files / app/features/dashboard MyEvents.tsx

100% Statements 32/32
100% Branches 10/10
100% Functions 10/10
100% Lines 28/28

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 1311x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 1x 1x   5x                                           13x                               12x           1x           1x   5x 12x 12x     12x     7x       24x                                   7x   13x           1x                                              
import { styled, Typography, useMediaQuery } from "@mui/material";
import { useInfiniteQuery } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import HorizontalScroller from "components/HorizontalScroller";
import TextBody from "components/TextBody";
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 { service } from "service";
import { theme } from "theme";
import hasAtLeastOnePage from "utils/hasAtLeastOnePage";
 
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: theme.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([COMMUNITIES, 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,
        }),
      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} />;
              })}
          </StyledCardContainer>
          {hasNextPage && !isBelowSm && (
            <StyledButtonContainer>
              <Button
                onClick={() => fetchNextPage()}
                variant="outlined"
                sx={{
                  color: theme.palette.common.black,
                  borderColor: theme.palette.grey[300],
 
                  "&:hover": {
                    borderColor: theme.palette.grey[300],
                    backgroundColor: "#3135390A",
                  },
                }}
              >
                {t("communities:see_more_events_label")}
              </Button>
            </StyledButtonContainer>
          )}
        </>
      ) : (
        !error && <TextBody>{t("communities:events_empty_state")}</TextBody>
      )}
    </StyledWrapper>
  );
}