All files / app/features/notifications EditNotificationSettingsPage.tsx

91.52% Statements 54/59
66.66% Branches 12/18
93.75% Functions 15/16
95.83% Lines 46/48

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194  1x 1x 1x 1x 1x   1x 1x                                                   8x                               8x                     8x             8x           8x         3x             1x       1x         4x 2x 2x 2x   2x     5x 8x     8x 8x 8x 8x   8x 5x 2x     3x   6x 6x 6x   6x 6x   4x         4x 4x       6x         3x 3x     8x       8x 3x 4x   4x                                                                                
import { ExpandLess, ExpandMore } from "@mui/icons-material";
import { Button, CircularProgress, 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.down("sm")]: {
    padding: theme.spacing(2),
  },
 
  [theme.breakpoints.up("md")]: {
    width: "50%",
  },
}));
 
const StyledHeaderContainer = styled("div")(({ theme }) => ({
  display: "flex",
  justifyContent: "space-between",
  alignItems: "center",
  marginBottom: theme.spacing(1),
 
  [theme.breakpoints.down("sm")]: {
    gap: theme.spacing(1),
  },
}));
 
const StyledTitle = styled(Typography)(({ theme }) => ({
  [theme.breakpoints.down("sm")]: {
    flex: "0 0 50%",
    fontSize: "1.1rem",
  },
}));
 
const StyledButton = styled(Button)(({ theme }) => ({
  [theme.breakpoints.down("sm")]: {
    fontSize: "0.75rem",
  },
}));
 
const StyledNotificationDescription = styled(Typography)(({ theme }) => ({
  margin: theme.spacing(1, 0),
  paddingBottom: theme.spacing(3),
}));
 
const StyledAccordionContainer = styled("div")(({ theme }) => ({
  border: `1px solid ${theme.palette.divider}`,
  marginTop: theme.spacing(1),
  borderRadius: theme.shape.borderRadius,
  overflow: "hidden",
}));
 
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);
  const [allExpanded, setAllExpanded] = useState<boolean>(false);
 
  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 handleToggleAll = () => {
    setAllExpanded(!allExpanded);
  };
 
  const renderNotificationListItems = () =>
    Object.keys(groups)
      .filter((key) => groups[key].length > 0)
      .map((key) => (
        <NotificationSettingsListItem
          key={key}
          items={groups[key]}
          type={key as NotificationType}
          isExpanded={allExpanded}
        />
      ));
 
  return (
    <StyledNotificationSettingsContainer>
      <Typography variant="h2">{t("title")}</Typography>
      <StyledNotificationDescription variant="body1">
        {t("description")}
      </StyledNotificationDescription>
      <StyledHeaderContainer>
        <StyledTitle variant="h3">{t("list_heading")}</StyledTitle>
        <StyledButton
          variant="outlined"
          size="small"
          onClick={handleToggleAll}
          startIcon={allExpanded ? <ExpandLess /> : <ExpandMore />}
        >
          {allExpanded ? t("collapse_all") : t("expand_all")}
        </StyledButton>
      </StyledHeaderContainer>
      {isError && (
        <Snackbar severity="error">
          <Typography>{t("error_loading")}</Typography>
        </Snackbar>
      )}
      {!isLoading && !areGroupsLoading ? (
        <StyledAccordionContainer>
          {renderNotificationListItems()}
        </StyledAccordionContainer>
      ) : (
        <StyledLoadingSpinner />
      )}
    </StyledNotificationSettingsContainer>
  );
}