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

100% Statements 36/36
93.75% Branches 15/16
100% Functions 9/9
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 136 137 138 139 140 1411x 1x 1x 1x 1x 1x   1x 1x                                               5x                                                             1x 8x 8x     8x 8x 8x   8x 5x 2x     3x   6x 6x 6x   6x           6x 6x     6x 4x           6x         3x 3x     8x 3x 6x   4x             8x                                        
import { CircularProgress, List, Typography } from "@material-ui/core";
import Snackbar from "components/Snackbar";
import { AUTH } from "i18n/namespaces";
import { useTranslation } from "next-i18next";
import { useEffect, useState } from "react";
import makeStyles from "utils/makeStyles";
 
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 useStyles = makeStyles((theme) => ({
  descriptionText: {
    fontSize: theme.spacing(1.8),
    color: theme.palette.text.secondary,
  },
  list: {
    border: `1px solid ${theme.palette.divider}`,
    marginTop: theme.spacing(1),
    display: "flex",
    flexDirection: "column",
    padding: `0 ${theme.spacing(1)}`,
  },
  loading: {
    position: "absolute",
  },
  notificationSettingsContainer: {
    display: "flex",
    flexDirection: "column",
    padding: theme.spacing(4),
    margin: "0 auto",
    width: "100%",
    [theme.breakpoints.up("md")]: {
      width: "50%",
    },
  },
  notificationDescription: {
    margin: theme.spacing(1, 0),
    paddingBottom: theme.spacing(3),
  },
}));
 
export default function EditNotificationSettingsPage() {
  const classes = useStyles();
  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 (
    <div className={classes.notificationSettingsContainer}>
      <Typography variant="h2">{t("title")}</Typography>
      <Typography className={classes.notificationDescription} variant="body1">
        {t("description")}
      </Typography>
      <Typography variant="h3">{t("list_heading")}</Typography>
      {isError && (
        <Snackbar severity="error">
          <Typography>{t("error_loading")}</Typography>
        </Snackbar>
      )}
      {!isLoading && !areGroupsLoading ? (
        <List className={classes.list}>{renderNotificationListItems()}</List>
      ) : (
        <CircularProgress className={classes.loading} />
      )}
    </div>
  );
}