Rev 6953 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
import { useEffect, useState } from 'react'
import { axios } from '../utils'
const useFetchHelper = (helper = '') => {
const [data, setData] = useState([])
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
useEffect(() => {
const getHelper = (helper) => {
setLoading(true)
axios
.get(`/helpers/${helper}`)
.then(({ data: response }) => {
const { data, success } = response
if (!success) {
const error =
typeof data === 'string' ? data : 'Ha ocurrido un error'
setError(error)
return
}
if (!Array.isArray(data)) {
const adapterData = Object.entries(data).map(([key, value]) => {
return { value: key, name: value }
})
setData(adapterData)
return
}
setData(data)
})
.catch((err) => setError(`Error: ${err}`))
.finally(() => setLoading(false))
}
getHelper(helper)
}, [])
return { data, error, loading }
}
export default useFetchHelper