Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 14843 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 14843 Rev 14991
Línea 1... Línea 1...
1
import axios from 'axios'
1
import axios from 'axios'
2
import { useState, useEffect } from 'react'
2
import { useState, useEffect } from 'react'
-
 
3
import { useDispatch } from 'react-redux'
-
 
4
import { addNotification } from '../redux/notification/notification.actions'
Línea 3... Línea 5...
3
 
5
 
Línea 4... Línea 6...
4
function useDataFetching({ url, params }) {
6
function useDataFetching({ url = '', params = null }) {
5
 
7
 
6
	const [loading, setLoading] = useState(true)
8
	const [loading, setLoading] = useState(true)
Línea 7... Línea 9...
7
	const [results, setResults] = useState([])
9
	const [results, setResults] = useState([])
8
	const [error, setError] = useState('')
10
	const dispatch = useDispatch()
9
 
11
 
10
	useEffect(() => {
12
	useEffect(() => {
11
		const fetchData = async () => {
13
		const fetchData = async () => {
-
 
14
			try {
-
 
15
				const { data } = await axios.get(url, { params })
-
 
16
				if (!data.success) {
-
 
17
					typeof data.data === 'string'
-
 
18
						?
-
 
19
						dispatch(addNotification({
-
 
20
							style: 'danger',
-
 
21
							msg: data.data
-
 
22
						}))
-
 
23
						: Object.entries(data.data).map(([key, value]) =>
-
 
24
							value.map(err =>
-
 
25
								dispatch(addNotification({
-
 
26
									style: 'danger',
-
 
27
									msg: `${key}: ${err}`
12
			try {
28
								}))
13
				const { data } = await params ? axios.get(url) : axios.get(url, { params: params })
-
 
14
				if (!data.success) {
29
							)
15
					setLoading(false)
30
						)
16
					setError(data.data)
31
					setLoading(false)
17
					return
32
					return
18
				}
33
				}
19
				setLoading(false)
34
				setLoading(false)
-
 
35
				setResults(data.data)
-
 
36
			} catch (error) {
20
				setResults(data.data)
37
				setLoading(false)
-
 
38
				dispatch(addNotification({
-
 
39
					style: 'danger',
21
			} catch (error) {
40
					msg: error
22
				setLoading(false)
41
				}))
23
				setError(error.message)
42
				return
Línea 24... Línea 43...
24
			}
43
			}
25
			setLoading(false)
44
			setLoading(false)
Línea 26... Línea 45...
26
		}
45
		}
27
 
-
 
28
		fetchData()
46
 
29
	}, [])
47
		fetchData()
30
 
48
	}, [])
31
	return {
49