All files / app/features/auth/password ResetPassword.tsx

100% Statements 26/26
100% Branches 2/2
100% Functions 6/6
100% Lines 23/23

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 771x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x   13x       13x           13x             5x 13x 13x             13x 3x     13x 3x                                                          
import { styled, Typography } from "@mui/material";
import { useMutation } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import HtmlMeta from "components/HtmlMeta";
import PageTitle from "components/PageTitle";
import TextField from "components/TextField";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { useForm } from "react-hook-form";
import { service } from "service";
import { theme } from "theme";
import { lowercaseAndTrimField } from "utils/validation";
 
const StyledMain = styled("main")(() => ({
  padding: theme.spacing(0, 3),
}));
 
const StyledForm = styled("form")(() => ({
  "& > * + *": {
    marginBlockStart: theme.spacing(1),
  },
}));
 
const StyledTextField = styled(TextField)(() => ({
  width: "100%",
  [theme.breakpoints.up("md")]: {
    width: theme.typography.pxToRem(400),
  },
}));
 
export default function ResetPassword() {
  const { t } = useTranslation([AUTH, GLOBAL]);
  const { handleSubmit, register } = useForm<{ userId: string }>();
 
  const {
    error,
    isPending,
    isSuccess,
    mutate: resetPassword,
  } = useMutation<Empty, RpcError, string>({
    mutationFn: (userId) => service.account.resetPassword(userId),
  });
 
  const onSubmit = handleSubmit(({ userId }) => {
    resetPassword(lowercaseAndTrimField(userId));
  });
 
  return (
    <StyledMain>
      <HtmlMeta title={t("auth:reset_password")} />
      <PageTitle>{t("auth:reset_password")}</PageTitle>
      {error && <Alert severity="error">{error.message}</Alert>}
      <StyledForm onSubmit={onSubmit}>
        <StyledTextField
          id="userId"
          {...register("userId", { required: true })}
          label={t("auth:reset_password_form.enter_email")}
          name="userId"
          variant="standard"
          fullWidth
        />
        <Button loading={isPending} type="submit">
          {t("global:submit")}
        </Button>
        {isSuccess && (
          <Typography variant="body1">
            {t("auth:reset_password_form.success_message")}
          </Typography>
        )}
      </StyledForm>
    </StyledMain>
  );
}