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

97.05% Statements 33/34
89.47% Branches 17/19
100% Functions 5/5
96.87% Lines 31/32

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 1412x 2x 2x 2x   2x 2x 2x 2x   2x 2x   8x                                                                                   2x 13x 13x 13x   13x 13x 13x   13x             13x 13x   13x       1x     13x 1x 1x       1x     13x 1x 1x                                                                                             9x  
import { Pagination, Typography } from "@mui/material";
import Alert from "components/Alert";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import TextBody from "components/TextBody";
import { EventsType } from "features/queryKeys";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import { useState } from "react";
import makeStyles from "utils/makeStyles";
 
import EventsList from "./EventsList";
import { useListMyEvents } from "./hooks";
 
const useStyles = makeStyles((theme) => ({
  emptyState: {
    marginBottom: theme.spacing(2),
  },
  filter: {
    backgroundColor: theme.palette.grey[200],
    color: 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",
    },
  },
  filterTags: {
    display: "flex",
    alignItems: "center",
    marginTop: theme.spacing(2),
    marginBottom: theme.spacing(2),
  },
  pagination: {
    display: "flex",
    justifyContent: "center",
    marginTop: theme.spacing(2),
    marginBottom: theme.spacing(2),
  },
  selectedFilter: {
    backgroundColor: theme.palette.secondary.main,
    color: theme.palette.common.white,
    padding: theme.spacing(1, 2),
    textAlign: "center",
    fontWeight: "bold",
    margin: theme.spacing(0.5),
    borderRadius: theme.shape.borderRadius * 6,
    "&:hover": {
      cursor: "pointer",
    },
  },
}));
 
const MyEventsList = () => {
  const classes = useStyles();
  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 (
    <>
      <div className={classes.filterTags}>
        <Typography
          className={
            eventType === "past" ? classes.selectedFilter : classes.filter
          }
          variant="body2"
          onClick={handleFilterPastClick}
        >
          {t("communities:past")}
        </Typography>
        <Typography
          className={showCancelled ? classes.selectedFilter : classes.filter}
          variant="body2"
          onClick={handleFilterShowCancelledClick}
        >
          {t("communities:show_cancelled_events")}
        </Typography>
      </div>
      {!hasEvents && !isLoading && (
        <TextBody className={classes.emptyState}>
          {t("communities:events_empty_state")}
        </TextBody>
      )}
      {error && <Alert severity="error">{error.message}</Alert>}
      {isLoading && <CenteredSpinner minHeight="theme.spacing(20)" />}
      {hasEvents && !isLoading && (
        <>
          <EventsList events={data.eventsList} />
          <Pagination
            className={classes.pagination}
            count={numPages}
            page={pageNumber}
            color="primary"
            onChange={handlePageNumberChange}
            size="large"
          />
        </>
      )}
    </>
  );
};
 
export default MyEventsList;