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

97.61% Statements 41/42
89.47% Branches 17/19
100% Functions 9/9
97.36% Lines 37/38

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 1382x 2x 2x 2x 2x   2x 2x 2x 2x 2x   2x 2x   22x             2x 178x 44x                             4x       7x             2x 22x 22x   22x 22x 22x   22x             22x 22x   22x       1x     22x 1x 1x       1x     22x 1x 1x                                                                                                   9x  
import { Pagination, styled, Typography } from "@mui/material";
import Alert from "components/Alert";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import StyledLink from "components/StyledLink";
import TextBody from "components/TextBody";
import { EventsType } from "features/queryKeys";
import { Trans, useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import { useState } from "react";
import { routeToNewEvent } from "routes";
import { theme } from "theme";
 
import EventsList from "./EventsList";
import { useListMyEvents } from "./hooks";
 
const StyledFilterTagContainer = styled("div")(() => ({
  display: "flex",
  alignItems: "center",
  marginTop: theme.spacing(2),
  marginBottom: theme.spacing(2),
}));
 
const StyledFilterTag = styled(Typography, {
  shouldForwardProp: (propName) => propName !== "isSelected",
})<{ isSelected: boolean }>(({ isSelected }) => ({
  backgroundColor: isSelected
    ? theme.palette.secondary.main
    : theme.palette.grey[200],
  color: isSelected ? theme.palette.common.white : theme.palette.text.primary,
  padding: theme.spacing(1, 2),
  textAlign: "center",
  fontWeight: "bold",
  margin: theme.spacing(0.5),
  borderRadius: theme.shape.borderRadius * 6,
  "&:hover": {
    cursor: "pointer",
  },
}));
 
const StyledEmptyBody = styled(TextBody)(() => ({
  marginBottom: theme.spacing(2),
}));
 
const StyledPagination = styled(Pagination)(() => ({
  display: "flex",
  justifyContent: "center",
  marginTop: theme.spacing(2),
  marginBottom: theme.spacing(2),
}));
 
const MyEventsList = () => {
  const { t } = useTranslation([COMMUNITIES]);
  const pageSize = 5;
 
  const [pageNumber, setPageNumber] = useState(1);
  const [eventType, setEventType] = useState<EventsType>("upcoming");
  const [showCancelled, setShowCancelled] = useState<boolean>(false);
 
  const { data, error, isLoading } = useListMyEvents({
    pastEvents: eventType === "past",
    pageSize,
    pageNumber,
    showCancelled,
  });
 
  const hasEvents = data && data.eventsList && data.eventsList.length > 0;
  const numPages = Math.ceil((data?.totalItems ?? 0) / pageSize) ?? 1;
 
  const handlePageNumberChange = (
    event: React.ChangeEvent<unknown>,
    value: number,
  ) => {
    setPageNumber(value);
  };
 
  const handleFilterPastClick = () => {
    if (eventType === "upcoming") {
      setEventType("past");
    } else E{
      setEventType("upcoming");
    }
    setPageNumber(1);
  };
 
  const handleFilterShowCancelledClick = () => {
    setShowCancelled(!showCancelled);
    setPageNumber(1);
  };
 
  return (
    <>
      <StyledFilterTagContainer>
        <StyledFilterTag
          isSelected={eventType === "past"}
          variant="body2"
          onClick={handleFilterPastClick}
        >
          {t("communities:past")}
        </StyledFilterTag>
        <StyledFilterTag
          isSelected={showCancelled}
          variant="body2"
          onClick={handleFilterShowCancelledClick}
        >
          {t("communities:show_cancelled_events")}
        </StyledFilterTag>
      </StyledFilterTagContainer>
      {!hasEvents && !isLoading && (
        <StyledEmptyBody>
          <Trans
            t={t}
            i18nKey="communities:events_empty_state"
            components={[
              <StyledLink key="create-link" href={routeToNewEvent()} />,
            ]}
          />
        </StyledEmptyBody>
      )}
      {error && <Alert severity="error">{error.message}</Alert>}
      {isLoading && <CenteredSpinner minHeight="theme.spacing(20)" />}
      {hasEvents && !isLoading && (
        <>
          <EventsList events={data.eventsList} />
          <StyledPagination
            count={numPages}
            page={pageNumber}
            color="primary"
            onChange={handlePageNumberChange}
            size="large"
          />
        </>
      )}
    </>
  );
};
 
export default MyEventsList;