Loading web/src/hooks/useTown.tsx +34 −3 Original line number Diff line number Diff line Loading @@ -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" Loading Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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 Loading
web/src/hooks/useTown.tsx +34 −3 Original line number Diff line number Diff line Loading @@ -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" Loading Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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