Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2209 | Rev 2211 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 2209 Rev 2210
Línea 1... Línea 1...
1
import { useState, useEffect } from 'react'
1
import { useState, useEffect } from 'react'
-
 
2
 
2
import { axios } from '../utils'
3
import { axios } from '@app/utils'
3
import { useDispatch } from 'react-redux'
-
 
4
import { addNotification } from '../redux/notification/notification.actions'
-
 
Línea 5... Línea 4...
5
 
4
 
6
const useFetch = (url, defaultValue = {}) => {
5
const useFetch = (url, defaultValue = {}) => {
7
  const [data, setData] = useState(defaultValue)
6
  const [data, setData] = useState(defaultValue)
8
  const [isLoading, setIsLoading] = useState(true)
-
 
Línea -... Línea 7...
-
 
7
  const [isLoading, setIsLoading] = useState(true)
-
 
8
 
-
 
9
  const handleError = (response) => {
-
 
10
    const { success, data } = response.data
-
 
11
 
-
 
12
    if (!data) {
-
 
13
      return response.data
-
 
14
    }
-
 
15
 
-
 
16
    if (success === false) {
-
 
17
      const errorMessage =
-
 
18
        typeof data === 'string'
-
 
19
          ? data
-
 
20
          : Object.entries(data)
-
 
21
              .map(([key, value]) => `${key}: ${value}`)
-
 
22
              .join(', ')
-
 
23
      throw new Error(errorMessage)
-
 
24
    }
-
 
25
 
-
 
26
    return data
9
  const dispatch = useDispatch()
27
  }
10
 
28
 
Línea 11... Línea 29...
11
  const getResources = () => {
29
  const getResources = (url) => {
12
    setIsLoading(true)
30
    setIsLoading(true)
13
 
-
 
14
    axios
-
 
15
      .get(url)
-
 
16
      .then(({ data }) => {
-
 
17
        if (!data.data) {
-
 
18
          setData(data)
-
 
19
          return
-
 
20
        }
-
 
21
 
-
 
22
        if (!data.success) {
-
 
23
          const errorMessage =
-
 
24
            typeof data === 'string'
-
 
25
              ? data
-
 
26
              : Object.entries(data)
31
 
27
                  .map(([key, value]) => `${key}: ${value}`)
-
 
28
                  .join(', ')
-
 
29
          throw new Error(errorMessage)
32
    axios
30
        }
-
 
31
 
33
      .get(url)
32
        setData(data.data)
34
      .then((response) => handleError(response))
Línea 33... Línea 35...
33
      })
35
      .then((data) => setData(data))
34
      .finally(() => setIsLoading(false))
36
      .finally(() => setIsLoading(false))
35
  }
37
  }
Línea 36... Línea 38...
36
 
38
 
37
  useEffect(() => {
39
  useEffect(() => {
38
    getResources()
40
    url && getResources(url)