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

100% Statements 32/32
100% Branches 13/13
100% Functions 7/7
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 1161x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   7x                                                       1x       19x 19x 19x   19x     19x   19x 1x     19x                           1x                   7x   17x                         1x                        
import { FormControlLabel, Typography } from "@material-ui/core";
import classNames from "classnames";
import Alert from "components/Alert";
import Button from "components/Button";
import CircularProgress from "components/CircularProgress";
import CustomColorSwitch from "components/CustomColorSwitch";
import TextBody from "components/TextBody";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import { useRouter } from "next/router";
import { useState } from "react";
import { newEventRoute } from "routes";
import hasAtLeastOnePage from "utils/hasAtLeastOnePage";
import makeStyles from "utils/makeStyles";
 
import { useCommunityPageStyles } from "../CommunityPage";
import EventCard from "./EventCard";
import { useListAllEvents } from "./hooks";
 
const useStyles = makeStyles((theme) => ({
  root: {
    display: "grid",
    rowGap: theme.spacing(2),
    paddingBlockStart: theme.spacing(1),
    paddingBlockEnd: theme.spacing(5),
    justifyItems: "start",
  },
  container: {
    [theme.breakpoints.down("xs")]: {
      gridTemplateColumns: "1fr",
      gridGap: theme.spacing(2),
    },
    display: "grid",
  },
  eventsCard: {
    width: "100%",
  },
  moreEventButton: {
    justifySelf: "center",
  },
}));
 
interface EventsTabProps {
  pastEvents?: boolean;
  tabTitle: string;
}
 
export default function EventsTab({
  pastEvents = false,
  tabTitle,
}: EventsTabProps) {
  const { t } = useTranslation([COMMUNITIES]);
  const classes = { ...useCommunityPageStyles(), ...useStyles() };
  const router = useRouter();
 
  const [showCancelled, setShowCancelled] = useState(false);
 
  const { data, error, hasNextPage, fetchNextPage, isLoading } =
    useListAllEvents({ pastEvents, showCancelled });
 
  const handleShowCancelledClick = () => {
    setShowCancelled(!showCancelled);
  };
 
  return (
    <div className={classes.root}>
      <Typography variant="h2">{tabTitle}</Typography>
      <FormControlLabel
        control={
          <CustomColorSwitch
            checked={showCancelled}
            onClick={handleShowCancelledClick}
          />
        }
        label={t("communities:show_cancelled_events")}
      />
      {error && <Alert severity="error">{error.message}</Alert>}
      {!pastEvents && (
        <Button onClick={() => router.push(newEventRoute)}>
          {t("communities:create_an_event")}
        </Button>
      )}
      {isLoading ? (
        <CircularProgress />
      ) : hasAtLeastOnePage(data, "eventsList") ? (
        <>
          <div className={classNames(classes.cardContainer, classes.container)}>
            {data.pages
              .flatMap((page) => page.eventsList)
              .map((event) => (
                <EventCard
                  key={event.eventId}
                  event={event}
                  className={classNames(
                    classes.placeEventCard,
                    classes.eventsCard
                  )}
                />
              ))}
          </div>
          {hasNextPage && (
            <Button
              className={classes.moreEventButton}
              onClick={() => fetchNextPage()}
            >
              {t("communities:see_more_events_label")}
            </Button>
          )}
        </>
      ) : (
        !error && <TextBody>{t("communities:events_empty_state")}</TextBody>
      )}
    </div>
  );
}