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

100% Statements 28/28
100% Branches 16/16
100% Functions 5/5
100% Lines 26/26

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              7x 7x 7x 7x 7x 7x 7x   7x 7x 7x 7x 7x 7x   14x                                     45x   45x                                                                     7x           7x 45x 45x       45x 45x   45x 31x       45x       45x                                                                                                                                              
import {
  Card,
  CardContent,
  CardMedia,
  Chip,
  Theme,
  Typography,
} from "@material-ui/core";
import { eventImagePlaceholderUrl } from "appConstants";
import classNames from "classnames";
import Divider from "components/Divider";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import Link from "next/link";
import { Event } from "proto/events_pb";
import { useMemo } from "react";
import { routeToEvent } from "routes";
import { timestamp2Date } from "utils/date";
import dayjs from "utils/dayjs";
import makeStyles from "utils/makeStyles";
import stripMarkdown from "utils/stripMarkdown";
 
const useStyles = makeStyles<Theme, { eventImageSrc: string }>((theme) => ({
  root: {
    "&:hover": {
      backgroundColor: theme.palette.grey[50],
    },
  },
  cancelledChip: {
    backgroundColor: theme.palette.error.main,
    color: theme.palette.common.white,
    fontWeight: "bold",
  },
  cancelledEvent: {
    opacity: 0.6,
    backgroundColor: theme.palette.grey[200],
  },
  image: {
    padding: theme.spacing(1),
    backgroundColor: theme.palette.grey[200],
    height: 80,
    backgroundImage: ({ eventImageSrc }) => `url(${eventImageSrc})`,
    backgroundSize: ({ eventImageSrc }) =>
      eventImageSrc === eventImagePlaceholderUrl ? "contain" : "cover",
    [theme.breakpoints.up("sm")]: {
      height: 100,
    },
    [theme.breakpoints.up("md")]: {
      height: 120,
    },
  },
  chip: {
    borderRadius: theme.shape.borderRadius,
    fontWeight: "bold",
  },
  title: {
    display: "-webkit-box",
    boxOrient: "vertical",
    lineClamp: 2,
    overflow: "hidden",
  },
  eventTime: {
    display: "-webkit-box",
    boxOrient: "vertical",
    lineClamp: 2,
    overflow: "hidden",
    [theme.breakpoints.up("sm")]: {
      lineClamp: 1,
    },
  },
  content: {
    display: "-webkit-box",
    boxOrient: "vertical",
    lineClamp: 5,
    overflow: "hidden",
  },
}));
 
export const EVENT_CARD_TEST_ID = "event-card";
export interface EventCardProps {
  event: Event.AsObject;
  className?: string;
}
 
export default function EventCard({ event, className }: EventCardProps) {
  const { t } = useTranslation([COMMUNITIES]);
  const classes = useStyles({
    eventImageSrc: event.photoUrl || eventImagePlaceholderUrl,
  });
 
  const startTime = dayjs(timestamp2Date(event.startTime!));
  const endTime = dayjs(timestamp2Date(event.endTime!));
 
  const strippedContent = useMemo(
    () => stripMarkdown(event.content),
    [event.content]
  );
 
  const formattedEventDates = `${startTime.format("llll")} - ${endTime.format(
    endTime.isSame(startTime, "day") ? "LT" : "llll"
  )}`;
 
  return (
    <Card
      className={classNames(
        className,
        classes.root,
        event.isCancelled ? classes.cancelledEvent : null
      )}
      data-testid={EVENT_CARD_TEST_ID}
    >
      <Link href={routeToEvent(event.eventId, event.slug)}>
        <a>
          <CardMedia
            src={event.photoUrl || eventImagePlaceholderUrl}
            className={classes.image}
          >
            {event.onlineInformation && (
              <Chip
                className={classes.chip}
                size="medium"
                label={t("communities:online")}
              />
            )}
          </CardMedia>
 
          <CardContent>
            <Typography
              variant="body2"
              color="textSecondary"
              className={classes.eventTime}
              gutterBottom
              /* title useful to hover in case it's too long for the card */
              title={formattedEventDates}
            >
              {formattedEventDates}
            </Typography>
 
            <Typography variant="h3" gutterBottom className={classes.title}>
              {event.title}
            </Typography>
            <Typography noWrap variant="body2" gutterBottom>
              {event.offlineInformation
                ? event.offlineInformation.address
                : t("communities:virtual_event_location_placeholder")}
            </Typography>
 
            {event.isCancelled && (
              <Chip
                classes={{ root: classes.cancelledChip }}
                label={t("communities:cancelled")}
              />
            )}
 
            <Divider spacing={1} />
 
            <div>
              <Typography className={classes.content} variant="body1" paragraph>
                {strippedContent}
              </Typography>
 
              <Typography variant="body2" color="textSecondary">
                {t("communities:attendees_count", {
                  count: event.goingCount + event.maybeCount,
                })}
              </Typography>
            </div>
          </CardContent>
        </a>
      </Link>
    </Card>
  );
}