Proyectos de Subversion LeadersLinked - SPA

Rev

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

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