All files / app/features/notifications EditNotificationSettingsPage.tsx

92% Statements 46/50
71.42% Branches 10/14
100% Functions 12/12
97.61% Lines 41/42

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 1471x 1x 1x 1x 1x   1x 1x                                                   8x                     8x         3x               1x       1x         4x 2x 2x 2x   2x     5x 8x     8x 8x 8x   8x 5x 2x     3x   6x 6x 6x   6x 6x   4x         4x 4x       6x         3x 3x     8x 3x 4x   4x                                                      
import { CircularProgress, List, styled, Typography } from "@mui/material";
import Snackbar from "components/Snackbar";
import { NOTIFICATIONS } from "i18n/namespaces";
import { useTranslation } from "next-i18next";
import { useEffect, useState } from "react";
 
import NotificationSettingsListItem from "./NotificationSettingsListItem";
import useNotificationSettings from "./useNotificationSettings";
 
export type NotificationType =
  | "account_security"
  | "account_settings"
  | "chat"
  | "event"
  | "reference"
  | "friend_request"
  | "host_request"
  | "reply"
  | "general";
 
export interface GroupAction {
  action: string;
  description: string;
  email: boolean;
  push: boolean;
  topic: string;
  userEditable: boolean;
}
 
interface GroupsByType {
  [key: string]: GroupAction[];
}
 
const StyledNotificationSettingsContainer = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  padding: theme.spacing(4),
  margin: "0 auto",
  width: "100%",
  [theme.breakpoints.up("md")]: {
    width: "50%",
  },
}));
 
const StyledNotificationDescription = styled(Typography)(({ theme }) => ({
  margin: theme.spacing(1, 0),
  paddingBottom: theme.spacing(3),
}));
 
const StyledCustomList = styled(List)(({ theme }) => ({
  border: `1px solid ${theme.palette.divider}`,
  marginTop: theme.spacing(1),
  display: "flex",
  flexDirection: "column",
  padding: `0 ${theme.spacing(1)}`,
}));
 
const StyledLoadingSpinner = styled(CircularProgress)({
  position: "absolute",
});
 
const getGroupKey = (
  groupHeading: string,
  subTopicAction: string,
  topicName: string,
) => {
  if (groupHeading === "Account Security") return "account_security";
  Iif (groupHeading === "Account Settings") return "account_settings";
  Iif (groupHeading === "Other Notifications") return "other_notifications";
  Iif (subTopicAction === "reply" || subTopicAction === "comment")
    return "reply";
  return topicName;
};
 
export default function EditNotificationSettingsPage() {
  const { t } = useTranslation(NOTIFICATIONS, {
    keyPrefix: "notification_settings.edit_preferences",
  });
  const { data, isLoading, isError } = useNotificationSettings();
  const [groups, setGroups] = useState<GroupsByType>({});
  const [areGroupsLoading, setAreGroupsLoading] = useState<boolean>(true);
 
  useEffect(() => {
    if (!data) {
      return;
    }
 
    const computedGroups = data?.groupsList.reduce<GroupsByType>(
      (acc, group) => {
        group.topicsList.forEach((topic) => {
          const items = topic?.itemsList;
          Iif (!items) return;
 
          items.forEach((subTopic) => {
            if (!subTopic?.userEditable) return;
 
            const key = getGroupKey(
              group.heading,
              subTopic.action,
              topic.topic,
            );
            acc[key] ||= [];
            acc[key].push({ ...subTopic, topic: topic.topic });
          });
        });
 
        return acc;
      },
      {},
    );
 
    setGroups(computedGroups);
    setAreGroupsLoading(false);
  }, [data]);
 
  const renderNotificationListItems = () =>
    Object.keys(groups)
      .filter((key) => groups[key].length > 0)
      .map((key) => (
        <NotificationSettingsListItem
          key={key}
          items={groups[key]}
          type={key as NotificationType}
        />
      ));
 
  return (
    <StyledNotificationSettingsContainer>
      <Typography variant="h2">{t("title")}</Typography>
      <StyledNotificationDescription variant="body1">
        {t("description")}
      </StyledNotificationDescription>
      <Typography variant="h3">{t("list_heading")}</Typography>
      {isError && (
        <Snackbar severity="error">
          <Typography>{t("error_loading")}</Typography>
        </Snackbar>
      )}
      {!isLoading && !areGroupsLoading ? (
        <StyledCustomList>{renderNotificationListItems()}</StyledCustomList>
      ) : (
        <StyledLoadingSpinner />
      )}
    </StyledNotificationSettingsContainer>
  );
}