All files / app/features/auth/notifications NotificationSettingsListItem.tsx

95.45% Statements 21/22
50% Branches 1/2
83.33% Functions 5/6
95.23% Lines 20/21

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            1x                     1x 1x 1x 1x 1x     1x             2x                             1x                   1x       4x   4x   4x     4x   4x       4x 4x 4x   4x                 4x                                  
import {
  Collapse,
  ListItem,
  ListItemIcon,
  ListItemText,
  Typography,
} from "@material-ui/core";
import {
  AccountSecurityIcon,
  AccountSettingsIcon,
  ChatBubbleIcon,
  CouchFilledIcon,
  EventIcon,
  ExpandLessIcon,
  ExpandMoreIcon,
  PenIcon,
  SinglePersonIcon,
} from "components/Icons";
import { AUTH } from "i18n/namespaces";
import { useTranslation } from "next-i18next";
import { useState } from "react";
import makeStyles from "utils/makeStyles";
 
import { GroupAction, NotificationType } from "./EditNotificationSettingsPage";
import NotificationSettingsSubListItem from "./NotificationSettingsSubListItem";
 
export interface NotificationSettingsListItemProps {
  items: GroupAction[];
  type: NotificationType;
}
 
const useStyles = makeStyles((theme) => ({
  descriptionText: {
    fontSize: theme.spacing(1.8),
    color: theme.palette.text.secondary,
  },
  listItem: {
    "&:hover": {
      backgroundColor: "transparent",
    },
    "&:not(:first-child)": {
      borderTop: `1px solid ${theme.palette.divider}`,
    },
  },
}));
 
const mapTypeToIcon: { [key: string]: JSX.Element } = {
  account_security: <AccountSecurityIcon fontSize="large" color="action" />,
  account_settings: <AccountSettingsIcon fontSize="large" color="action" />,
  chat: <ChatBubbleIcon fontSize="large" color="action" />,
  event: <EventIcon fontSize="large" color="action" />,
  reference: <PenIcon fontSize="large" color="action" />,
  friend_request: <SinglePersonIcon fontSize="large" color="action" />,
  host_request: <CouchFilledIcon fontSize="large" color="action" />,
};
 
export default function NotificationSettingsListItem({
  items,
  type,
}: NotificationSettingsListItemProps) {
  const classes = useStyles();
  const notificationType =
    type as `auth:notification_settings.edit_preferences.list_items.${NotificationType}`;
 
  const { t } = useTranslation([AUTH], {
    keyPrefix: "notification_settings.edit_preferences.list_items",
  });
  const [isCollapseOpen, setIsCollapseOpen] = useState<boolean>(false);
 
  const handleCollapseClick = () => {
    setIsCollapseOpen(!isCollapseOpen);
  };
 
  const renderItems = () =>
    items
      .filter((item) => item.userEditable)
      .map((item) => (
        <NotificationSettingsSubListItem
          key={`${item.topic}:${item.action}`}
          topic={item.topic}
          action={item.action}
          push={item.push}
          email={item.email}
        />
      ));
 
  return (
    <>
      <ListItem
        button
        className={classes.listItem}
        onClick={handleCollapseClick}
      >
        <ListItemIcon>{mapTypeToIcon[type]}</ListItemIcon>
        <ListItemText>
          <Typography variant="h3">{t(notificationType)}</Typography>
        </ListItemText>
        {isCollapseOpen ? <ExpandLessIcon /> : <ExpandMoreIcon />}
      </ListItem>
      <Collapse in={isCollapseOpen}>{renderItems()}</Collapse>
    </>
  );
}