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

97.67% Statements 42/43
88% Branches 22/25
100% Functions 6/6
97.56% Lines 40/41

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2072x 2x 2x 2x 2x 2x 2x 2x 2x   2x   2x 2x   8x                                                                                                                                 2x 14x         14x     14x 14x 14x 14x   14x 14x 14x 14x   14x                 14x 14x   14x       1x     14x 1x 1x     14x 1x 1x     14x 1x 1x       1x                                                                                                                                             9x  
import { Pagination, Typography, useMediaQuery, useTheme } from "@mui/material";
import Alert from "components/Alert";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import LocationAutocomplete from "components/LocationAutocomplete";
import TextBody from "components/TextBody";
import { useTranslation } from "i18n";
import { COMMUNITIES, GLOBAL } from "i18n/namespaces";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { GeocodeResult } from "utils/hooks";
import makeStyles from "utils/makeStyles";
 
import EventsList from "./EventsList";
import { useEventSearch } from "./hooks";
 
const useStyles = makeStyles((theme) => ({
  column: {
    display: "flex",
    flexDirection: "column",
  },
  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),
  },
  loadingBox: {
    display: "flex",
    justifyContent: "center",
    padding: theme.spacing(2),
    width: "100%",
    minHeight: theme.spacing(20),
  },
  locationSearch: {
    marginRight: theme.spacing(2),
    paddingBottom: theme.spacing(2),
  },
  pagination: {
    display: "flex",
    justifyContent: "center",
    marginTop: theme.spacing(2),
    marginBottom: theme.spacing(2),
  },
  row: {
    display: "flex",
    justifyContent: "space-between",
    width: "100%",
    [theme.breakpoints.down("md")]: {
      flexDirection: "column",
    },
  },
  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 DiscoverEventsList = () => {
  const classes = useStyles();
  // @TODO - Basically just making the form since LocationAutocomplete required the control prop
  // We don't validate or require this field so it's just a dummy form
  // Too much refactoring needed to change existing components to not require the control prop
  // Might be worth making a new uncontrolled omponent that doesn't require the control prop
  const { control } = useForm<{ location: GeocodeResult | undefined }>({
    mode: "onChange",
  });
  const { t } = useTranslation([GLOBAL, COMMUNITIES]);
  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down("md"));
  const pageSize = 12;
 
  const [pageNumber, setPageNumber] = useState(1);
  const [isMyCommunities, setIsMyCommunities] = useState<boolean>(false);
  const [isOnlineOnly, setIsOnlineOnly] = useState<boolean>(false);
  const [locationResult, setLocationResult] = useState<GeocodeResult | "">("");
 
  const { data, error, isLoading } = useEventSearch({
    pastEvents: false,
    pageSize,
    pageNumber,
    isMyCommunities,
    isOnlineOnly,
    searchLocation: locationResult,
  });
 
  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 handleFilterIsMyCommunitiesClick = () => {
    setIsMyCommunities(!isMyCommunities);
    setPageNumber(1);
  };
 
  const handleFilterIsOnlineOnlyClick = () => {
    setIsOnlineOnly(!isOnlineOnly);
    setPageNumber(1);
  };
 
  const handleOnChangeAutocomplete = (newLocationResult: GeocodeResult) => {
    if (typeof newLocationResult === "object") {
      setLocationResult(newLocationResult);
    } else E{
      setLocationResult("");
    }
    setPageNumber(1);
  };
 
  const renderLocationAutoComplete = () => (
    <LocationAutocomplete
      className={classes.locationSearch}
      control={control}
      name="location"
      defaultValue={typeof locationResult === "object" ? locationResult : ""}
      label={t("global:location_autocomplete.search_location_button")}
      onChange={handleOnChangeAutocomplete}
      fieldError={undefined}
      fullWidth={isMobile}
    />
  );
 
  return (
    <>
      <div className={classes.column}>
        <Typography variant="h2">
          {t("communities:discover_events_title")}
        </Typography>
        <div className={classes.row}>
          <div className={classes.filterTags}>
            <Typography
              className={
                isMyCommunities ? classes.selectedFilter : classes.filter
              }
              variant="body2"
              onClick={handleFilterIsMyCommunitiesClick}
            >
              {t("communities:my_communities")}
            </Typography>
            <Typography
              className={isOnlineOnly ? classes.selectedFilter : classes.filter}
              variant="body2"
              onClick={handleFilterIsOnlineOnlyClick}
            >
              {t("communities:online")}
            </Typography>
          </div>
          {!isMobile && renderLocationAutoComplete()}
        </div>
        {isMobile && renderLocationAutoComplete()}
      </div>
      {!hasEvents && !isLoading && (
        <TextBody>{t("communities:events_empty_state")}</TextBody>
      )}
      {error && <Alert severity="error">{error.message}</Alert>}
      {isLoading && (
        <div className={classes.loadingBox}>
          <CenteredSpinner />
        </div>
      )}
      {hasEvents && !isLoading && (
        <>
          <EventsList events={data.eventsList} isVerticalStyle />
          <Pagination
            className={classes.pagination}
            count={numPages}
            page={pageNumber}
            color="primary"
            onChange={handlePageNumberChange}
            size="large"
          />
        </>
      )}
    </>
  );
};
 
export default DiscoverEventsList;