Commit b868ec96 authored by Omar Luna Hernández's avatar Omar Luna Hernández
Browse files

Se modifica para que se obtengan los lugares de un pueblo al llamar al hook

parent c2c038d5
Loading
Loading
Loading
Loading
+14 −25
Original line number Diff line number Diff line
import { FieldErrors, Resolver, SubmitHandler, useForm } from "react-hook-form"
import { Place, AvailableDays } from "../infraestructure/entities/place";
import { toast } from "react-toastify";
import { useEffect, useState } from "react";
import { Dispatch, SetStateAction, useEffect, useState } from "react";
import axios, { AxiosError } from "axios";
import { languaguesList } from "../constants/languages";
import { useGetStatesList } from "./useGetStatesList";
import { useTown } from "./useTown";
import { MIN_NUMBER_PLACE_IMAGES } from "../constants/images_nuber";
import { PlaceDatasourceProd } from "../data/datasources/prod/place_datasource";
import { PlaceRepositoryProd } from "../data/repositories/prod/place_repository";
@@ -122,7 +120,8 @@ const resolver: Resolver<Place> = async (data) => {
  };
};

export const usePlace = () => {
export const usePlace = (idTown: number, forceRenderList?: () => void, 
setIsWindowActive?: Dispatch<SetStateAction<boolean>>) => {
  const {
    register, 
    handleSubmit,
@@ -137,35 +136,25 @@ export const usePlace = () => {
  const [availableDays, setAvailableDays] = useState<AvailableDays>(AvailableDays.WEEKEND);
  const [placeList, setPlaceList] = useState<Place[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const {townsList, getTownsByState} = useTown();
  const {getStates, statesList} = useGetStatesList();
  const [pending, setPending] = useState(false);
  
  useEffect(() => {
    setIsLoading(true);
    const getStatesList = async () => {
      try{
        getStates();
      }catch(error: any){
        if(axios.isAxiosError(error)){
          error as AxiosError;
          showErrorAxios(error);
        }
      }
    }
    getStatesList();
    updatePlacesByTown(idTown);
    setValue('idTown', idTown);
    setValue('available',availableDays,{shouldValidate: true});
    setIsLoading(false);
  },[]);

  const updateTownsList = (idState: number, stateName: string) => {
    getTownsByState(idState, stateName);
  }

  const onSubmit : SubmitHandler<Place> = (data: Place) => {
    const fetch = async () => {
      try{
        await placeRepository.registerPlace(data);
        await placeRepository.registerPlace(data).then(() =>{
          if(forceRenderList && setIsWindowActive){
            forceRenderList();
            setIsWindowActive(false);
          }
        });
      }catch(error: any){
        if(axios.isAxiosError(error)){
          error as AxiosError;
@@ -176,6 +165,9 @@ export const usePlace = () => {
            case(axios.AxiosError.ERR_NETWORK):
              setErrorMessage("Conexión con el servidor fallida");
              break;
            default:
              setErrorMessage(error.message);
              break;
          }
        }
        throw new Error();
@@ -230,9 +222,6 @@ export const usePlace = () => {
    setDescriptions,
    clearErrors,
    isLoading,
    statesList,
    townsList,
    updateTownsList,
    updateTimeForm,
    availableDays,
    setAvailableDays,