add form errors
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {useState} from "react";
|
||||
import axios from "axios";
|
||||
import React from "react";
|
||||
import {cleanErrorsForm, displayFormErrors} from "../utilities/form";
|
||||
|
||||
const useAxiosTools = () => {
|
||||
|
||||
@@ -13,7 +14,11 @@ const useAxiosTools = () => {
|
||||
const axiosDelete = axios.delete
|
||||
|
||||
const errorCatch = (error: any) => {
|
||||
setError(error.response.data.message || error.message)
|
||||
if (error.response && error.response.status === 422) {
|
||||
displayFormErrors(error)
|
||||
} else {
|
||||
setError(error.response.data.message || error.message)
|
||||
}
|
||||
}
|
||||
|
||||
const errorLabel = () => {
|
||||
@@ -21,7 +26,12 @@ const useAxiosTools = () => {
|
||||
return error ? <div className="bg-red-600 rounded m-2 text-center text-white px-2 py-1 mx-auto">{error}</div>: null
|
||||
}
|
||||
|
||||
return {loading, setLoading, error, setError, errorCatch, errorLabel, axiosGet, axiosPost, axiosPut, axiosDelete}
|
||||
const cleanErrors = () => {
|
||||
cleanErrorsForm()
|
||||
setError(null)
|
||||
}
|
||||
|
||||
return {loading, setLoading, error, setError, errorCatch, errorLabel, cleanErrors, axiosGet, axiosPost, axiosPut, axiosDelete}
|
||||
}
|
||||
|
||||
export default useAxiosTools
|
||||
|
||||
Reference in New Issue
Block a user