Proyectos de Subversion LeadersLinked - SPA

Rev

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

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