Commit 2072a38d authored by Lorenzo Trujillo Rojas's avatar Lorenzo Trujillo Rojas
Browse files

Se creó un hook para inicializar los datos del usuario

parent dddb7e42
Loading
Loading
Loading
Loading
+25 −28
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import { ApiRequestStatus } from "../../common/constants/api_request_states";
import { useDataContext } from "../../common/contexts/data_context";
import { useSetUp } from "../../common/contexts/set_up_context";
import { router } from "expo-router";
import { useChangeInterests } from "./useChangeInterests";

export type SetUpProfileFormValues = {
  interests: number[];
@@ -13,38 +14,34 @@ export type SetUpProfileFormValues = {
};

export const useSetUpProfile = () => {
  const { control, handleSubmit, setValue } = useForm<SetUpProfileFormValues>();
  const { interests: allCategories, requestStatus: changeInterestsRequest } = useGetInterests();
  const { interests, toogleInterest, onSubmit: onSubmitInterests, setAllInterests } = useChangeInterests();
  const [requestStatus, setRequestStatus] = useState<ApiRequestStatus>(ApiRequestStatus.LOADING);

  // const { control, handleSubmit, setValue, getValues } = useForm<SetUpProfileFormValues>();
  const { setFirstTime } = useSetUp();
  const { interests: allCategories, requestStatus } = useGetInterests();
  // TODO: Si se agregan mas campos que requieran datos de la API, debemos procurar que todos los request status esten en success
  const [interests, setInterests] = useState<IOption[]>([]);
  
  useEffect(() => {
    if (requestStatus === ApiRequestStatus.SUCCESS && allCategories && allCategories.length > 0) {
        setInterests([...allCategories]);
    const requests = [changeInterestsRequest];
    if (requests.every((request) => request === ApiRequestStatus.SUCCESS)) {
      setAllInterests(allCategories!);
      setRequestStatus(ApiRequestStatus.SUCCESS);
    }
  }, [requestStatus]);
  }, [changeInterestsRequest]);
  
  const toogleInterest = (id: number) => {
    console.log("toogle");
    const newInterests = interests.map((interest) => {
      if (interest.id === id) {
        return { ...interest, isSelected: !interest.isSelected };
      }
      return interest;
    });
    setInterests(newInterests);
  };

  const onSubmit = async () => {
    setValue("interests", interests.filter((interest) => interest.isSelected).map((interest) => interest.id));
    await handleSubmit((data) => {
        //TODO: Implementar la logica de envio de datos
        //TODO: Verificar si se enviará la fecha de nacimiento y los intereses en el mismo request o por separado
      console.log(data);
    })();
/*     await handleSubmit((data) => {
      
    })(); */
    
    try {
      await onSubmitInterests();
      await setFirstTime();
    router.replace("(tabs)");
      router.replace("/(tabs)");
    } catch (error) {
      console.log(error);
    }
  };
  return { control, onSubmit, toogleInterest, interests, requestStatus };
  return { onSubmit, toogleInterest, interests, requestStatus };
};