Proyectos de Subversion LeadersLinked - SPA

Rev

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