Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3432 Rev 3585
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';
Línea 3... Línea 3...
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 { 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'
11
import ErrorPage from './pages/error/error-page';
12
import Spinner from './components/UI/Spinner'
-
 
13
 
-
 
Línea 14... Línea 12...
14
import './styles/globals.scss'
12
import Spinner from './components/UI/Spinner';
15
 
13
 
16
export default function App() {
14
export default function App() {
17
  const [loading, setLoading] = useState(true)
15
  const [loading, setLoading] = useState(true);
18
  const [credentialsError, setCredentialsError] = useState(false)
16
  const [credentialsError, setCredentialsError] = useState(false);
19
  const { data: labels } = useFetch('/language')
17
  const { data: labels } = useFetch('/language');
Línea 20... Línea 18...
20
  const { is_logged_in } = useSelector((state) => state.auth)
18
  const { is_logged_in } = useSelector((state) => state.auth);
21
  const dispatch = useDispatch()
19
  const dispatch = useDispatch();
22
 
20
 
23
  const getCredentials = async () => {
21
  const getCredentials = async () => {
24
    const response = await axios.get('/signin')
22
    const response = await axios.get('/signin');
25
    const { data, success } = response.data
23
    const { data, success } = response.data;
Línea 26... Línea 24...
26
    if (!success) throw new Error('Error al obtener las credenciales')
24
    if (!success) throw new Error('Error al obtener las credenciales');
27
    return data
25
    return data;
Línea 28... Línea 26...
28
  }
26
  };
29
 
27
 
30
  useEffect(() => {
28
  useEffect(() => {
31
    setLoading(true)
29
    setLoading(true);
32
 
30
 
33
    getCredentials()
31
    getCredentials()
34
      .then((credentials) => {
32
      .then((credentials) => {
35
        window.localStorage.setItem('jwt', credentials.jwt)
33
        window.localStorage.setItem('jwt', credentials.jwt);
36
        dispatch(setCrendentials(credentials))
34
        dispatch(setCrendentials(credentials));
37
      })
35
      })
38
      .catch((error) => {
36
      .catch((error) => {
Línea 39... Línea 37...
39
        console.error(error.message)
37
        console.error(error.message);
40
        setCredentialsError(true)
38
        setCredentialsError(true);
41
      })
39
      })
Línea 42... Línea 40...
42
      .finally(() => setLoading(false))
40
      .finally(() => setLoading(false));
43
  }, [is_logged_in])
41
  }, [is_logged_in]);
44
 
42
 
Línea 45... Línea 43...
45
  useEffect(() => {
43
  useEffect(() => {
46
    dispatch(setIntlLabels(labelsAdapter(labels)))
44
    dispatch(setIntlLabels(labelsAdapter(labels)));
47
  }, [labels])
45
  }, [labels]);
Línea 48... Línea 46...
48
 
46
 
49
  if (loading) {
47
  if (loading) {