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

100% Statements 38/38
93.33% Branches 14/15
100% Functions 11/11
100% Lines 35/35

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 1361x 1x 1x 1x 1x   1x 1x                                               8x                     8x         3x               1x       5x 8x     8x 8x 8x   8x 5x 2x     3x   6x 6x 6x   6x           6x 6x     6x 4x           6x         3x 3x     8x 3x 6x   4x                                                      
import { CircularProgress, List, styled, Typography } from "@mui/material";
import Snackbar from "components/Snackbar";
import { AUTH } 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";
 
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",
});
 
export default function EditNotificationSettingsPage() {
  const { t } = useTranslation(AUTH, {
    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) => {
          if (topic && topic.itemsList) {
            topic.itemsList.forEach((subTopic) => {
              const key =
                group.heading === "Account Security"
                  ? "account_security"
                  : group.heading === "Account Settings"
                  ? "account_settings"
                  : topic.topic;
 
              if (!acc[key]) {
                acc[key] = [];
              }
 
              if (subTopic.userEditable) {
                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>
  );
}