add form errors

This commit is contained in:
Romulus21
2023-09-17 10:01:31 +02:00
parent 1e39c1b79f
commit 9b7a11dbd0
7 changed files with 87 additions and 14 deletions

View File

@@ -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