first commit
This commit is contained in:
37
resources/js/hooks/AxiosTools.tsx
Normal file
37
resources/js/hooks/AxiosTools.tsx
Normal file
@@ -0,0 +1,37 @@
|
||||
import {useState} from "react";
|
||||
import axios from "axios";
|
||||
import React from "react";
|
||||
import {cleanErrorsForm, displayFormErrors} from "../utilities/form";
|
||||
|
||||
const useAxiosTools = (isLoading = false) => {
|
||||
|
||||
const [loading, setLoading] = useState(isLoading)
|
||||
const [error, setError] = useState<string|null>(null)
|
||||
|
||||
const axiosGet = axios.get
|
||||
const axiosPost = axios.post
|
||||
const axiosPut = axios.put
|
||||
const axiosDelete = axios.delete
|
||||
|
||||
const errorCatch = (error: any) => {
|
||||
if (error.response && error.response.status === 422) {
|
||||
displayFormErrors(error)
|
||||
} else {
|
||||
setError(error.response?.data.message || error.message)
|
||||
}
|
||||
}
|
||||
|
||||
const errorLabel = () => {
|
||||
|
||||
return error ? <div className="bg-red-600 rounded m-2 text-center text-white px-2 py-1 mx-auto">{error}</div>: null
|
||||
}
|
||||
|
||||
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