Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3432 Rev 3719
Línea 1... Línea 1...
1
import { useState, useEffect } from 'react'
1
import { useState, useEffect } from 'react';
2
import { useDispatch } from 'react-redux'
2
import { useDispatch } from 'react-redux';
3
 
3
 
4
import { axios } from '@utils'
4
import { axios } from '@utils';
5
import { asyncLogout } from '@store/auth/auth.actions'
5
import { asyncLogout } from '@store/auth/auth.actions';
6
import { addNotification } from '@store/notification/notification.actions'
6
import { addNotification } from '@store/notification/notification.actions';
7
 
7
 
8
export function useFetch(url, defaultValue = {}) {
8
export function useFetch(url, defaultValue = {}) {
9
  const [data, setData] = useState(defaultValue)
9
  const [data, setData] = useState(defaultValue);
10
  const [isLoading, setIsLoading] = useState(true)
10
  const [isLoading, setIsLoading] = useState(true);
11
  const dispatch = useDispatch()
11
  const dispatch = useDispatch();
12
 
12
 
13
  const handleError = (response) => {
13
  const handleError = (response) => {
14
    const { success, data } = response.data
14
    const { success, data } = response.data;
15
 
15
 
16
    if (!data) {
16
    if (!data) {
17
      return response.data
17
      return response.data;
18
    }
18
    }
19
 
19
 
20
    if (!success) {
20
    if (!success) {
21
      const errorMessage =
21
      const errorMessage =
22
        typeof data === 'string'
22
        typeof data === 'string'
23
          ? data
23
          ? data
24
          : Object.entries(data)
24
          : Object.entries(data)
25
              .map(([key, value]) => `${key}: ${value}`)
25
              .map(([key, value]) => `${key}: ${value}`)
26
              .join(', ')
26
              .join(', ');
27
      throw new Error(errorMessage)
27
      throw new Error(errorMessage);
28
    }
28
    }
29
 
29
 
30
    return data
30
    return data;
31
  }
31
  };
32
 
32
 
33
  const getResources = (url) => {
33
  const getResources = (url) => {
34
    if (!url) return
34
    if (!url) return;
35
 
35
 
36
    setIsLoading(true)
36
    setIsLoading(true);
37
 
37
 
38
    axios
38
    axios
39
      .get(url)
39
      .get(url)
40
      .then((response) => handleError(response))
40
      .then((response) => handleError(response))
41
      .then((data) => setData(data))
41
      .then((data) => setData(data))
42
      .catch((error) => {
42
      .catch((error) => {
43
        dispatch(addNotification({ style: 'danger', msg: error.message }))
43
        dispatch(addNotification({ style: 'danger', msg: error.message }));
44
        if (error.message.includes('sesión')) dispatch(asyncLogout())
44
        if (error.message.includes('sesión')) dispatch(asyncLogout());
45
      })
45
      })
46
      .finally(() => setIsLoading(false))
46
      .finally(() => setIsLoading(false));
47
  }
47
  };
48
 
48
 
49
  const refetch = () => getResources(url)
49
  const refetch = () => getResources(url);
50
 
50
 
51
  const mutate = (data) => setData(data)
51
  const mutate = (data) => setData(data);
52
 
52
 
53
  useEffect(() => {
53
  useEffect(() => {
54
    getResources(url)
54
    getResources(url);
55
  }, [url])
55
  }, [url]);
56
 
56
 
57
  return {
57
  return {
58
    data,
58
    data,
59
    mutate,
59
    mutate,
60
    isLoading,
60
    isLoading,
61
    refetch
61
    refetch
62
  }
62
  };
63
}
63
}