All files / app/features/dashboard CommunitiesList.tsx

0% Statements 0/24
0% Branches 0/13
0% Functions 0/5
0% Lines 0/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 80 81 82 83 84 85 86                                                                                                                                                                           
import { Link as MuiLink, makeStyles, Typography } from "@material-ui/core";
import { Skeleton } from "@material-ui/lab";
import Alert from "components/Alert";
import Button from "components/Button";
import StyledLink from "components/StyledLink";
import { useListSubCommunities } from "features/communities/hooks";
import useUserCommunities from "features/userQueries/useUserCommunities";
import { useTranslation } from "i18n";
import { DASHBOARD } from "i18n/namespaces";
import React from "react";
import { routeToCommunity } from "routes";
import hasAtLeastOnePage from "utils/hasAtLeastOnePage";
 
const useStyles = makeStyles((theme) => ({
  communityLink: {
    display: "flex",
    flexDirection: "column",
    padding: theme.spacing(2, 0),
    borderBottom: `solid 1px ${theme.palette.divider}`,
    "&:first-child": {
      borderTop: `solid 1px ${theme.palette.divider}`,
    },
  },
}));
 
export default function CommunitiesList({ all = false }: { all?: boolean }) {
  const { t } = useTranslation([DASHBOARD]);
  const classes = useStyles();
  const userCommunities = useUserCommunities();
  const allCommunities = useListSubCommunities(0);
  const communities = all ? allCommunities : userCommunities;
  return (
    <div>
      {communities.error?.message && (
        <Alert severity="error">{communities.error.message}</Alert>
      )}
      {communities.isLoading ? (
        <div className={classes.communityLink}>
          <MuiLink variant="h2" component="span">
            <Skeleton width={100} />
          </MuiLink>
          <Typography variant="body2">
            <Skeleton width={100} />
          </Typography>
        </div>
      ) : (
        communities.data &&
        (hasAtLeastOnePage(communities.data, "communitiesList") ? (
          <>
            {communities.data.pages
              .flatMap((page) => page.communitiesList)
              .map((community) => (
                <StyledLink
                  href={routeToCommunity(community.communityId, community.slug)}
                  key={`community-link-${community.communityId}`}
                  className={classes.communityLink}
                >
                  <Typography variant="h2" component="span">
                    {community.name}
                  </Typography>
                  <Typography variant="body1" color="textSecondary">
                    {t("dashboard:member_count", {
                      count: community.memberCount,
                    })}
                  </Typography>
                </StyledLink>
              ))}
            {communities.hasNextPage && (
              <Button
                onClick={() => communities.fetchNextPage()}
                loading={communities.isFetching}
              >
                {t("dashboard:load_more")}
              </Button>
            )}
          </>
        ) : (
          <Typography variant="body1" color="textSecondary">
            {t("dashboard:no_community")}
          </Typography>
        ))
      )}
    </div>
  );
}