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

74.28% Statements 26/35
25% Branches 1/4
37.5% Functions 3/8
73.52% Lines 25/34

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            1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x                 3x                                   1x           6x 6x         6x 6x 6x 6x   6x 1x 1x           1x                                 6x                                                 6x                                                                                        
import {
  List,
  ListItem,
  ListItemIcon,
  ListItemText,
  Typography,
} from "@material-ui/core";
import { MailOutline } from "@material-ui/icons";
import Alert from "components/Alert";
import CustomColorSwitch from "components/CustomColorSwitch";
import { NotificationNewIcon } from "components/Icons";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { useTranslation } from "next-i18next";
import { useState } from "react";
import { NotificationPreferenceData } from "service/notifications";
import { theme } from "theme";
import makeStyles from "utils/makeStyles";
 
import useUpdateNotificationSettings from "./useUpdateNotificationSettings";
 
export interface NotificationSettingsSubListItemProps {
  topic: string;
  action: string;
  email: boolean;
  push: boolean;
}
 
const useStyles = makeStyles((theme) => ({
  descriptionText: {
    fontSize: theme.spacing(1.8),
    color: theme.palette.text.secondary,
  },
  nested: {
    display: "flex",
    paddingLeft: theme.spacing(4),
    width: "100%",
    "&:hover": {
      backgroundColor: "transparent",
    },
    "&:not(:first-child)": {
      borderTop: `1px solid ${theme.palette.divider}`,
    },
  },
}));
 
export default function NotificationSettingsSubListItem({
  topic,
  action,
  email,
  push,
}: NotificationSettingsSubListItemProps) {
  const classes = useStyles();
  const { t } = useTranslation([AUTH, GLOBAL], {
    keyPrefix: "notification_settings.edit_preferences.item_descriptions",
  });
 
  const { updateNotificationSettings, status } =
    useUpdateNotificationSettings();
  const [mutationError, setMutationError] = useState<string | null>(null);
  const [isPushLoading, setIsPushLoading] = useState(false);
  const [isEmailLoading, setIsEmailLoading] = useState(false);
 
  const handlePushSwitchClick = () => {
    setIsPushLoading(true);
    const updatedItem: NotificationPreferenceData = {
      topic,
      action,
      deliveryMethod: "push",
      enabled: !push,
    };
    updateNotificationSettings(
      {
        preferenceData: updatedItem,
        setMutationError,
      },
      {
        onError: () => {
          window.scroll({ top: 0, behavior: "smooth" });
          setIsPushLoading(false);
        },
        onSettled: () => {
          setIsPushLoading(false);
        },
      }
    );
  };
 
  const handleEmailSwitchClick = () => {
    const updatedItem: NotificationPreferenceData = {
      topic,
      action,
      deliveryMethod: "email",
      enabled: !email,
    };
    setIsEmailLoading(true);
    updateNotificationSettings(
      {
        preferenceData: updatedItem,
        setMutationError,
      },
      {
        onError: () => {
          window.scroll({ top: 0, behavior: "smooth" });
          setIsEmailLoading(false);
        },
        onSettled: () => {
          setIsEmailLoading(false);
        },
      }
    );
  };
 
  return (
    <>
      {mutationError && (
        <Alert severity="error">
          {mutationError || t("global:error.unknown")}
        </Alert>
      )}
      <Typography className={classes.descriptionText}>
        {t(
          //@ts-ignore - I spent hours on this type with no luck
          `${topic}.${action}`
        )}
      </Typography>
      <List component="div" disablePadding>
        <ListItem button className={classes.nested}>
          <ListItemIcon>
            <NotificationNewIcon fontSize="medium" />
          </ListItemIcon>
          <ListItemText primary="Push" />
          <CustomColorSwitch
            color={theme.palette.primary.main}
            checked={push}
            isLoading={isPushLoading}
            onClick={handlePushSwitchClick}
            status={status}
          />
        </ListItem>
        <ListItem button className={classes.nested}>
          <ListItemIcon>
            <MailOutline fontSize="medium" />
          </ListItemIcon>
          <ListItemText primary="Email" />
          <CustomColorSwitch
            color={theme.palette.primary.main}
            checked={email}
            isLoading={isEmailLoading}
            onClick={handleEmailSwitchClick}
            status={status}
          />
        </ListItem>
      </List>
    </>
  );
}