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

72.72% Statements 24/33
33.33% Branches 1/3
44.44% Functions 4/9
70.96% Lines 22/31

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                 6x         12x                             5x           6x         6x 6x 6x 6x   6x 1x 1x           1x                                 6x                                                                                                                                      
import { MailOutline } from "@mui/icons-material";
import {
  List,
  ListItem,
  ListItemIcon,
  ListItemProps,
  ListItemText,
  styled,
  Typography,
} from "@mui/material";
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 useUpdateNotificationSettings from "./useUpdateNotificationSettings";
 
export interface NotificationSettingsSubListItemProps {
  topic: string;
  action: string;
  email: boolean;
  push: boolean;
}
 
const StyledDescriptionText = styled(Typography)(({ theme }) => ({
  fontSize: theme.spacing(1.8),
  color: theme.palette.text.secondary,
}));
 
const StyledListItem = styled(ListItem)<ListItemProps>(({ theme }) => ({
  display: "flex",
  paddingLeft: theme.spacing(4),
  width: "100%",
  background: "transparent",
  border: "none",
 
  "&:hover": {
    backgroundColor: "transparent",
  },
  "&:not(:first-of-type)": {
    borderTop: `1px solid ${theme.palette.divider}`,
  },
}));
 
export default function NotificationSettingsSubListItem({
  topic,
  action,
  email,
  push,
}: NotificationSettingsSubListItemProps) {
  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>
      )}
      <StyledDescriptionText>
        {/** @ts-ignore - I spent hours on this type with no luck*/}
        {t(`${topic}.${action}`)}
      </StyledDescriptionText>
      <List component="div" disablePadding>
        <StyledListItem component="button">
          <ListItemIcon>
            <NotificationNewIcon fontSize="medium" />
          </ListItemIcon>
          <ListItemText primary="Push" />
          <CustomColorSwitch
            customColor={theme.palette.primary.main}
            checked={push}
            isLoading={isPushLoading}
            onClick={handlePushSwitchClick}
            status={status}
          />
        </StyledListItem>
        <StyledListItem component="button">
          <ListItemIcon>
            <MailOutline fontSize="medium" />
          </ListItemIcon>
          <ListItemText primary="Email" />
          <CustomColorSwitch
            customColor={theme.palette.primary.main}
            checked={email}
            isLoading={isEmailLoading}
            onClick={handleEmailSwitchClick}
            status={status}
          />
        </StyledListItem>
      </List>
    </>
  );
}