Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3681 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useEffect, useState } from 'react';
2
import { useDispatch, useSelector } from 'react-redux';
3
 
4
import { axios } from './utils';
5
import { useFetch } from '@hooks';
6
import { labelsAdapter } from './utils/labels';
7
import { setIntlLabels } from './redux/intl/intl.action';
8
import { logout, setCrendentials } from './redux/auth/auth.actions';
9
 
10
import AppRoutes from './routes/routes';
11
import Spinner from './components/UI/Spinner';
12
 
13
export default function App() {
14
  const [loading, setLoading] = useState(true);
15
  const { data: labels } = useFetch('/language');
16
  const { is_logged_in } = useSelector((state) => state.auth);
17
  const dispatch = useDispatch();
18
 
19
  const getCredentials = async () => {
20
    const response = await axios.get('/signin');
21
    const { data, success } = response.data;
22
    if (!success) throw new Error('Error al obtener las credenciales');
23
    return data;
24
  };
25
 
26
  useEffect(() => {
27
    setLoading(true);
28
 
29
    getCredentials()
30
      .then((credentials) => {
31
        window.localStorage.setItem('jwt', credentials.jwt);
32
        dispatch(setCrendentials(credentials));
33
      })
34
      .catch((error) => {
35
        if (error.message.includes('La sesión ha caducado')) window.localStorage.removeItem('jwt');
36
        dispatch(logout());
37
      })
38
      .finally(() => setLoading(false));
39
  }, [is_logged_in]);
40
 
41
  useEffect(() => {
42
    dispatch(setIntlLabels(labelsAdapter(labels)));
43
  }, [labels]);
44
 
45
  if (loading) {
46
    return <Spinner absolute />;
47
  }
48
 
49
  return <AppRoutes />;
50
}