Proyectos de Subversion LeadersLinked - SPA

Rev

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

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