Commit 44c7e41a authored by Lorenzo Trujillo Rojas's avatar Lorenzo Trujillo Rojas
Browse files

Se completó el flujo para resetear la contraseña

parent d22e0ca5
Loading
Loading
Loading
Loading
+49 −9
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ import { useDataContext } from "../../common/contexts/data_context";
import { useForm } from "react-hook-form";
import { ApiRequestStatus } from "../../common/constants/api_request_states";
import { PasswordForm } from "../components/password_form";
import { router } from "expo-router";
import { FullPageLoader } from "../../common/components/full_page_loader";

interface ResetPasswordSlide {
  slide: JSX.Element;
@@ -17,15 +19,31 @@ interface ResetPasswordSlide {
export interface ResetPasswordFormValues {
  email: string;
  newPassword: string;
  newPasswordConfirmation: string;
  code: string;
}

export enum ChangePaswordError {
  PASSWORD_MISMATCH,
  UNDEFINED
}

export class ResetPasswordError extends Error {
  public readonly type: ChangePaswordError;
  constructor( type: ChangePaswordError, message? :string,){
    super(message);
    this.type = type;
  }
  
} 

export const useResetPassword = () => {
  const { authRepository } = useDataContext();
  const { control, handleSubmit, getValues, setError, setValue } = useForm<ResetPasswordFormValues>({
    defaultValues: {
      email: "",
      newPassword: "",
      newPasswordConfirmation: "",
      code: "",
    }
  });
@@ -37,7 +55,8 @@ export const useResetPassword = () => {

  const getResetCode = async () => {
    await setLoading();
    if (getValues().email) {
    if (!getValues().email) {
      console.log(getValues())
      setError("email", { type: "manual", message: "Email is required" });
      throw new Error("Email is required");
    }
@@ -46,15 +65,30 @@ export const useResetPassword = () => {
  };

  const validSubmit = async (data: ResetPasswordFormValues) => {
    console.log("Valid Submit", data);
    try {
      await setLoading();
      if (data.newPassword !== data.newPasswordConfirmation) {
        setError("newPasswordConfirmation", { type: "manual", message: "Passwords do not match" });
        return;
      }
      await authRepository!.resetPassword({
        newPassword: data.newPassword,
        code: data.code,
        email: data.email
      })
      router.replace("/login");
    } catch(error) {
      if (error instanceof ResetPasswordError && error.type == ChangePaswordError.UNDEFINED) {
        throw new Error();
      }
    }
  };

  const errorSubmit = async () => {
    
    throw new Error();
  };

  const onSubmit = async () => {
    await setLoading();
    await handleSubmit(validSubmit, errorSubmit)();
  };

@@ -62,17 +96,18 @@ export const useResetPassword = () => {
    control,
    getResetCode,
    setValue,
    onSubmit
    onSubmit,
    status
  };
}

const useResetPasswordControl = () => {
  const { control, getResetCode, setValue, onSubmit } = useResetPassword();
  const { control, getResetCode, setValue, onSubmit, status: requestStatus } = useResetPassword();
  const slides: ResetPasswordSlide[] = [
    {
      slide: <ResetPasswordForm control={control}/>,
      callback: async () => {
        //await getResetCode();
        await getResetCode();
      }
    },
    {
@@ -121,12 +156,17 @@ const useResetPasswordControl = () => {
    setIsLast(step === slides.length - 1);
  }, [step]);

  return { onNext, onFinish, slide: slide.slide, isFirst, isLast, onPrevious };
  return { onNext, onFinish, slide: slide.slide, isFirst, isLast, onPrevious, requestStatus };
};

export const ResetPasswordPage = () => {
  const { onNext, onFinish, slide, isFirst, isLast, onPrevious } =
  const { onNext, onFinish, slide, isFirst, isLast, onPrevious, requestStatus } =
    useResetPasswordControl();


  if (requestStatus === ApiRequestStatus.LOADING) {
    return <FullPageLoader/>
  }  
  return (
    <View style={styles.mainContainer}>
      {slide}