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

100% Statements 21/21
100% Branches 7/7
100% Functions 3/3
100% Lines 21/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 571x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x   1x 12x   12x 12x             12x 4x     12x 5x 4x       12x                                          
import { Typography } from "@material-ui/core";
import Alert from "components/Alert";
import StyledLink from "components/StyledLink";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { AUTH } from "i18n/namespaces";
import { useRouter } from "next/router";
import { useEffect } from "react";
import { useMutation } from "react-query";
import { loginRoute } from "routes";
import { service } from "service";
import stringOrFirstString from "utils/stringOrFirstString";
 
export default function ConfirmChangeEmail() {
  const { t } = useTranslation(AUTH);
 
  const router = useRouter();
  const changeToken = stringOrFirstString(router.query.token);
 
  const {
    error,
    isLoading,
    isSuccess,
    mutate: confirmChangeEmail,
  } = useMutation<Empty, RpcError, string>((resetToken) =>
    service.account.confirmChangeEmail(resetToken)
  );
 
  useEffect(() => {
    if (changeToken) {
      confirmChangeEmail(changeToken);
    }
  }, [confirmChangeEmail, changeToken]);
 
  return isLoading ? (
    <Typography variant="body1">
      {t("change_email_confirmation.change_in_progress")}
    </Typography>
  ) : isSuccess ? (
    <>
      <Alert severity="success">
        {t("change_email_confirmation.success_message")}
      </Alert>
      <StyledLink href={loginRoute}>{t("login_prompt")}</StyledLink>
    </>
  ) : (
    error && (
      <Alert severity="error">
        {t("change_email_confirmation.error_message", {
          message: error.message,
        })}
      </Alert>
    )
  );
}