All files / app/features/auth/email DoNotEmail.tsx

0% Statements 0/21
0% Branches 0/11
0% Functions 0/5
0% Lines 0/20

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                                                                                                                                                         
import { Typography } from "@mui/material";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import { doNotEmailQueryKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { Trans, useTranslation } from "i18n";
import { AUTH } from "i18n/namespaces";
import { service } from "service";
 
import { GetNotificationSettingsRes } from "../../../proto/notifications_pb";
 
interface DoNotEmailFormData {
  doNotEmailEnabled: boolean;
}
 
export default function DoNotEmail() {
  const { t } = useTranslation(AUTH);
 
  const queryClient = useQueryClient();
 
  const { data, error, isPending } = useQuery<GetNotificationSettingsRes.AsObject, RpcError>({
    queryKey: [doNotEmailQueryKey],
    queryFn: service.notifications.getNotificationSettings,
  });
 
  const mutation = useMutation<GetNotificationSettingsRes.AsObject, RpcError, DoNotEmailFormData>({
    mutationFn: ({ doNotEmailEnabled }) => service.notifications.setNotificationSettings(doNotEmailEnabled),
 
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [doNotEmailQueryKey] });
    },
  });
 
  const toggleDoNotEmail = async () => {
    if (!data) return;
    mutation.mutate({
      doNotEmailEnabled: !data.doNotEmailEnabled,
    });
  };
 
  return (
    <div>
      <Typography variant="h2">{t("do_not_email.title")}</Typography>
      <Typography variant="body1" gutterBottom>
        {t("do_not_email.caveat")}
      </Typography>
      {mutation.error && <Alert severity="error">{mutation.error.message}</Alert>}
      {error && <Alert severity="error">{error.message}</Alert>}
      {isPending || !data ? (
        <CenteredSpinner />
      ) : (
        <>
          <Typography variant="body1" gutterBottom>
            <Trans
              t={t}
              i18nKey={
                data.doNotEmailEnabled
                  ? "do_not_email.status.no_emails_enabled_message"
                  : "do_not_email.status.no_emails_disabled_message"
              }
            />
          </Typography>
          <Typography variant="body1">
            <Button onClick={() => toggleDoNotEmail()} loading={mutation.isPending}>
              {data.doNotEmailEnabled
                ? t("do_not_email.action_button.no_emails_disable_text")
                : t("do_not_email.action_button.no_emails_enable_text")}
            </Button>
          </Typography>
        </>
      )}
    </div>
  );
}