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

Se crea el metodo que se encarga de actualizar al pueblo

parent 2f23fa78
Loading
Loading
Loading
Loading
+34 −3
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ const resolver: Resolver<Town> = async (data) => {
    };
  }

  if (!data.state) {
  if (!data.idState) {
    errors.state = {
      type: "required",
      message: "Debe seleccionar el estado al que pertenece el pueblo"
@@ -66,7 +66,7 @@ export const useTown = (forceRenderList?: () => void) => {
  const [errorMessage, setErrorMessage] = useState("");
  const {getStateById} = useGetStatesList();
  
  const onSubmit: SubmitHandler<Town> = (data: Town) => {
  const onSubmitRegister: SubmitHandler<Town> = (data: Town) => {
    const fetch = async () => {
      try{
        await townRepository.registerTown(data);
@@ -97,6 +97,37 @@ export const useTown = (forceRenderList?: () => void) => {
    )
  }

  const onSubmitUpdate: SubmitHandler<Town> = (data: Town) => {
    const fetch = async () => {
      try{
        await townRepository.updateTown(data);
        if(forceRenderList){
          forceRenderList();
        }
      }catch(error: any){
        if(axios.isAxiosError(error)){
          error as AxiosError;
          switch(error.code){
            case(axios.AxiosError.ERR_BAD_REQUEST):
              setErrorMessage("Acceso no autorizado");
              break;
            case(axios.AxiosError.ERR_NETWORK):
              setErrorMessage("Conexión con el servidor fallida");
              break;
          }
        }
        throw new Error();
      }
    }
    toast.promise(
      fetch(),{
        pending: "Subiendo datos...",
        success: "Los datos se han subido correctamente",
        error: errorMessage
      }
    )
  }
  
  const getTownsByState = async (stateId: number, stateName:string) => {
    try{
      const towns = await townRepository.getTownsByState(stateId, stateName);
@@ -126,5 +157,5 @@ export const useTown = (forceRenderList?: () => void) => {
    return null;
  }

  return {register, handleSubmit, errors, onSubmit, setValue, townsList, getTownsByState, setTownsList, getTownById};
  return {register, handleSubmit, errors, onSubmitRegister, onSubmitUpdate, setValue, townsList, getTownsByState, setTownsList, getTownById};
}
 No newline at end of file