All files / src/components/Auth/EmailPassword EmailPasswordSignupForm.tsx

0% Statements 0/52
0% Branches 0/17
0% Functions 0/6
0% Lines 0/50

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                                                                                                                                                                                                                                                                                                                           
import { CircularProgress } from "@mui/material";
import { useState } from "react";
import CustomButton from "../../common/CustomButton/index.tsx";
import { Toast } from "../../Toast.tsx";
import { useNavigate } from "react-router-dom";
import {
  useLazySaveSiteUrlQuery,
  useSignupMutation,
} from "../../../store/features/auth/api/index.ts";
import CustomInput from "../../Inputs/CustomInput.tsx";
import { InputTypes } from "../../../types/inputs/inputTypes.ts";
import ShowPasswordIcon from "./ShowPasswordIcon.tsx";
 
function EmailPasswordSignupForm() {
  const navigate = useNavigate();
  const [error, setError] = useState("");
  const [btnLoading, setBtnLoading] = useState(false);
  const [triggerSignup, { isLoading }] = useSignupMutation();
  const [triggerSiteUrlSave] = useLazySaveSiteUrlQuery();
 
  const togglePasswordVisibility = () => {
    setShowPassword(!showPassword);
  };
 
  const handleCloseToast = () => {
    setError("");
  };
 
  const [email, setEmail] = useState("");
  const [firstName, setFirstName] = useState("");
  const [lastName, setLastName] = useState("");
  const [password, setPassword] = useState("");
  const [showPassword, setShowPassword] = useState(false);
 
  function redirectToLoginAfterSignup() {
    navigate("/login");
  }
 
  const getError = (err) => {
    const errObj = err?.data;
    Iif (errObj && Object.keys(errObj).length !== 0) {
      Iif (errObj.email) return errObj.email[0];
      Iif (!errObj.email && errObj.password) return errObj.password[0];
      Iif (!errObj.email && !errObj.password && errObj.non_field_errors)
        return errObj.non_field_errors[0];
 
      return "Something went wrong!!";
    }
  };
 
  const handleSubmit = async (e) => {
    e.preventDefault();
 
    setBtnLoading(true);
    try {
      const data = {
        email: email,
        firstName,
        lastName,
        password: password,
      };
      await triggerSignup(data).unwrap();
      await triggerSiteUrlSave({ siteUrl: window.location.origin }).unwrap();
 
      setEmail("");
      setPassword("");
      setFirstName("");
      setLastName("");
 
      redirectToLoginAfterSignup();
    } catch (err) {
      console.error(err);
      setBtnLoading(false);
      const error = getError(err);
      setError(error);
    }
  };
 
  return (
    <div className="w-full">
      <form onSubmit={handleSubmit}>
        <div className="flex flex-col gap-2 my-2">
          <div className="flex gap-2">
            <CustomInput
              id="firstname"
              inputType={InputTypes.TEXT}
              disabled={isLoading}
              value={firstName}
              handleChange={setFirstName}
              placeholder="Enter First Name"
              className="!w-full"
              containerClassName="!w-full"
            />
            <CustomInput
              id="lastname"
              inputType={InputTypes.TEXT}
              disabled={isLoading}
              value={lastName}
              handleChange={setLastName}
              placeholder="Enter Last Name"
              className="!w-full"
              containerClassName="!w-full"
            />
          </div>
 
          <CustomInput
            inputType={InputTypes.TEXT}
            disabled={isLoading}
            value={email}
            handleChange={setEmail}
            placeholder="Enter Email"
            className="!w-full"
            containerClassName="!w-full"
          />
 
          <CustomInput
            inputType={InputTypes.TEXT}
            disabled={isLoading}
            type={showPassword ? "text" : "password"}
            value={password}
            handleChange={setPassword}
            placeholder="Enter Password"
            className="!w-full border-none"
            containerClassName={`!w-full border rounded p-1`}
            suffix={
              password && (
                <ShowPasswordIcon
                  togglePasswordVisibility={togglePasswordVisibility}
                />
              )
            }
          />
        </div>
 
        <CustomButton
          className="!bg-violet-500 !text-white !text-sm w-full !justify-center hover:!bg-transparent hover:!text-violet-500 p-2 font-normal"
          onClick={handleSubmit}>
          {btnLoading ? (
            <CircularProgress style={{ color: "inherit !important" }} />
          ) : (
            "Sign up"
          )}
        </CustomButton>
      </form>
 
      <Toast
        open={!!error}
        severity="error"
        message={error}
        handleClose={handleCloseToast}
        anchorOrigin={{ vertical: "bottom", horizontal: "left" }}
      />
    </div>
  );
}
 
export default EmailPasswordSignupForm;