All files / app/features/dashboard CommunityEvents.tsx

92.59% Statements 25/27
85.71% Branches 6/7
77.77% Functions 7/9
95.65% Lines 22/23

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  1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x                   3x   4x 9x   9x   9x   9x       9x 9x                   1x           12x           7x                                      
import { Event } from "@mui/icons-material";
import { styled, Typography } from "@mui/material";
import Alert from "components/Alert";
import StyledLink from "components/StyledLink";
import { Trans, useTranslation } from "i18n";
import { DASHBOARD } from "i18n/namespaces";
import { useState } from "react";
 
import { routeToNewEvent } from "../../routes";
import DashboardSectionHeader from "./DashboardSectionHeader";
import EventListRow, { EventListContainer, EventListRowSkeleton } from "./EventListRow";
import useGetDashboard, { Dashboard, DASHBOARD_PAGE_SIZE } from "./useGetDashboard";
 
const EmptyStateRow = styled("div")(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  gap: theme.spacing(2),
  padding: theme.spacing(2),
  border: "1px dashed var(--mui-palette-divider)",
  borderRadius: 10,
  background: "var(--mui-palette-grey-50)",
}));
 
const selectCommunityEvents = (dashboard: Dashboard) => dashboard.communityEvents?.eventsList ?? [];
 
export default function CommunityEvents() {
  const { t } = useTranslation([DASHBOARD]);
 
  const [currentPageIndex, setCurrentPageIndex] = useState(0);
 
  const { data: items = [], error, isLoading } = useGetDashboard(selectCommunityEvents);
 
  const currentItems = items.slice(
    currentPageIndex * DASHBOARD_PAGE_SIZE,
    (currentPageIndex + 1) * DASHBOARD_PAGE_SIZE,
  );
  const hasPreviousPage = currentPageIndex > 0;
  const hasNextPage = (currentPageIndex + 1) * DASHBOARD_PAGE_SIZE < items.length;
 
  return (
    <div>
      <DashboardSectionHeader
        icon={Event}
        title={t("dashboard:events.community_header")}
        hasPrevious={hasPreviousPage}
        hasNext={hasNextPage}
        onPrevious={() => setCurrentPageIndex((i) => i - 1)}
        onNext={() => setCurrentPageIndex((i) => i + 1)}
      />
      {error && <Alert severity="error">{error.message}</Alert>}
      {isLoading ? (
        <EventListContainer>
          {[0, 1, 2].map((i) => (
            <EventListRowSkeleton key={i} />
          ))}
        </EventListContainer>
      ) : currentItems?.length ? (
        <EventListContainer>
          {currentItems.map((event) => (
            <EventListRow key={event.eventId} event={event} />
          ))}
        </EventListContainer>
      ) : (
        !error && (
          <EmptyStateRow>
            <Typography variant="body2" sx={{ color: "var(--mui-palette-text-secondary)" }}>
              <Trans
                t={t}
                i18nKey="dashboard:events.your_upcoming_empty_message"
                components={{ createEventLink: <StyledLink href={routeToNewEvent()} /> }}
              />
            </Typography>
          </EmptyStateRow>
        )
      )}
    </div>
  );
}