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

Se agrega un metodo para obtener un estado en base en su id

parent a8dfbc82
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
import { useState } from "react";
import { State } from "../infraestructure/entities/state";
import axios from "axios";
import axios, { AxiosError } from "axios";
import { TownDatasourceProd } from "../data/datasources/prod/town_datasource";
import { TownRepositoryProd } from "../data/repositories/prod/town_repository";
import { showErrorAxios } from "../utils/Messages";
const townDatasource = new TownDatasourceProd();
const townRepository = new TownRepositoryProd(townDatasource);

@@ -15,9 +16,29 @@ export const useGetStatesList = () => {
      setStatesList(states);
    }catch(error: any){
      if(axios.isAxiosError(error)){
        //console.log(error)
        error as AxiosError;
        showErrorAxios(error);
      }
    }
  }
  return {getStates, statesList};

  const getStateById = async (idState: number) : Promise<State | null> => {
    let stateReturned: State | null = null;
    try{
      const states = await townRepository.getStates();
      states.forEach((state) => {
        if(state.stateId===idState){
          stateReturned = state;
          return;
        }
      })
    }catch(error: any){
      if(axios.isAxiosError(error)){
        error as AxiosError;
        showErrorAxios(error);
      }
    }    
    return stateReturned;
  }
  return {getStates, statesList, getStateById};
}
 No newline at end of file