All files / app/features/auth/signup BasicForm.tsx

84.84% Statements 28/33
66.66% Branches 8/12
83.33% Functions 5/6
90.32% Lines 28/31

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 1402x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x         2x                       5x       10x 10x 10x             10x         10x   2x 2x 2x       1x       2x     1x             10x 2x     10x                                                                                                                                    
import { InputLabel } from "@mui/material";
import Alert from "components/Alert";
import Button from "components/Button";
import TextField from "components/TextField";
import { useAuthContext } from "features/auth/AuthProvider";
import useAuthStyles from "features/auth/useAuthStyles";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { useRef } from "react";
import { useForm } from "react-hook-form";
import { useMutation } from "react-query";
import { service } from "service";
import {
  emailValidationPattern,
  lowercaseAndTrimField,
  nameValidationPattern,
} from "utils/validation";
 
type SignupBasicInputs = {
  name: string;
  email: string;
};
 
interface BasicFormProps {
  submitText?: string;
  successCallback?: () => void;
}
 
export default function BasicForm({
  submitText,
  successCallback,
}: BasicFormProps) {
  const { t } = useTranslation([AUTH, GLOBAL]);
  const { authActions } = useAuthContext();
  const authClasses = useAuthStyles();
 
  const {
    register,
    handleSubmit,
 
    formState: { errors },
  } = useForm<SignupBasicInputs>({
    mode: "onBlur",
    shouldUnregister: false,
  });
 
  const mutation = useMutation<void, RpcError, SignupBasicInputs>(
    async (data) => {
      const sanitizedEmail = lowercaseAndTrimField(data.email);
      const sanitizedName = data.name.trim();
      const state = await service.auth.startSignup(
        sanitizedName,
        sanitizedEmail
      );
      return authActions.updateSignupState(state);
    },
    {
      onSettled() {
        window.scroll({ top: 0, behavior: "smooth" });
      },
      onSuccess() {
        Iif (successCallback !== undefined) {
          successCallback();
        }
      },
    }
  );
 
  const onSubmit = handleSubmit((data: SignupBasicInputs) => {
    mutation.mutate(data);
  });
 
  const nameInputRef = useRef<HTMLInputElement>();
 
  return (
    <>
      {mutation.error && (
        <Alert severity="error">{mutation.error.message || ""}</Alert>
      )}
      <form className={authClasses.form} onSubmit={onSubmit}>
        <InputLabel className={authClasses.formLabel} htmlFor="name">
          {t("auth:basic_form.name.field_label")}
        </InputLabel>
        <TextField
          id="name"
          {...register("name", {
            pattern: {
              message: t("auth:basic_form.name.empty_error"),
              value: nameValidationPattern,
            },
            required: t("auth:basic_form.name.required_error"),
          })}
          fullWidth
          className={authClasses.formField}
          name="name"
          variant="standard"
          inputRef={(el: HTMLInputElement | null) => {
            Iif (!nameInputRef.current) el?.focus();
            Iif (el) nameInputRef.current = el;
          }}
          helperText={errors?.name?.message ?? " "}
          error={!!errors?.name?.message}
        />
        <InputLabel className={authClasses.formLabel} htmlFor="email">
          {t("auth:basic_form.email.field_label")}
        </InputLabel>
        <TextField
          id="email"
          {...register("email", {
            pattern: {
              message: t("auth:basic_form.email.empty_error"),
              value: emailValidationPattern,
            },
            required: t("auth:basic_form.email.required_error"),
          })}
          fullWidth
          className={authClasses.formField}
          name="email"
          variant="standard"
          helperText={errors?.email?.message ?? " "}
          error={!!errors?.email?.message}
        />
        <Button
          classes={{
            label: authClasses.buttonText,
            root: authClasses.button,
          }}
          onClick={onSubmit}
          type="submit"
          loading={mutation.isLoading}
          fullWidth
        >
          {submitText || t("global:continue")}
        </Button>
      </form>
    </>
  );
}