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

0% Statements 0/22
0% Branches 0/13
0% Functions 0/5
0% Lines 0/21

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                                                                                                                                                                             
import { Typography } from "@material-ui/core";
import Alert from "components/Alert";
import Button from "components/Button";
import CircularProgress from "components/CircularProgress";
import { doNotEmailQueryKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { Trans, useTranslation } from "i18n";
import { AUTH } from "i18n/namespaces";
import { GetNotificationSettingsRes } from "proto/notifications_pb";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { service } from "service";
 
interface DoNotEmailFormData {
  doNotEmailEnabled: boolean;
}
 
export default function DoNotEmail({ className }: { className: string }) {
  const { t } = useTranslation(AUTH);
 
  const queryClient = useQueryClient();
 
  const { data, error, isLoading } = useQuery<
    GetNotificationSettingsRes.AsObject,
    RpcError
  >(doNotEmailQueryKey, service.notifications.getNotificationSettings);
 
  const mutation = useMutation<
    GetNotificationSettingsRes.AsObject,
    RpcError,
    DoNotEmailFormData
  >(
    ({ doNotEmailEnabled }) =>
      service.notifications.setNotificationSettings(doNotEmailEnabled),
    {
      onSuccess: () => {
        queryClient.invalidateQueries(doNotEmailQueryKey);
      },
    }
  );
 
  const toggleDoNotEmail = async () => {
    Iif (!data) return;
    mutation.mutate({
      doNotEmailEnabled: !data.doNotEmailEnabled,
    });
  };
 
  return (
    <div className={className}>
      <Typography variant="h2">{t("do_not_email.title")}</Typography>
      {mutation.error && (
        <Alert severity="error">{mutation.error.message}</Alert>
      )}
      {error && <Alert severity="error">{error.message}</Alert>}
      {isLoading || !data ? (
        <CircularProgress />
      ) : (
        <>
          <Typography variant="body1">
            <Trans
              t={t}
              i18nKey={
                data.doNotEmailEnabled
                  ? "do_not_email.status.no_emails_enabled_message"
                  : "do_not_email.status.no_emails_disabled_message"
              }
            >
              Emails are currently <strong>disabled/enabled</strong> for your
              account.
            </Trans>
          </Typography>
          <Typography variant="body1">
            <Button
              onClick={() => toggleDoNotEmail()}
              loading={mutation.isLoading}
            >
              {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>
  );
}