| 4011 |
stevensc |
1 |
import { useEffect, useState } from 'react'
|
|
|
2 |
import { useDispatch } from 'react-redux';
|
|
|
3 |
import { addNotification } from '../redux/notification/notification.actions';
|
|
|
4 |
import { axios } from '../utils';
|
|
|
5 |
|
|
|
6 |
const useFetchHelper = (url = '') => {
|
|
|
7 |
|
|
|
8 |
const [state, setState] = useState(null);
|
|
|
9 |
const [loading, setLoading] = useState(false);
|
|
|
10 |
const dispatch = useDispatch()
|
|
|
11 |
|
|
|
12 |
useEffect(() => {
|
|
|
13 |
const dataFetch = async () => {
|
|
|
14 |
setLoading(true);
|
|
|
15 |
const { data: response } = await axios.get(url);
|
|
|
16 |
const { success, data } = response
|
|
|
17 |
|
|
|
18 |
if (!success) {
|
|
|
19 |
typeof data === "string"
|
|
|
20 |
? dispatch(addNotification({ style: "danger", msg: data }))
|
|
|
21 |
: dispatch(addNotification({ style: "danger", msg: 'Ha ocurrido un error' }))
|
|
|
22 |
return
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
setState(data);
|
|
|
26 |
setLoading(false);
|
|
|
27 |
};
|
|
|
28 |
|
|
|
29 |
dataFetch();
|
|
|
30 |
}, [url]);
|
|
|
31 |
|
|
|
32 |
return { data: state, loading };
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
export default useFetchHelper
|