Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2611 | Rev 2613 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2611 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { useDispatch, useSelector } from 'react-redux'
3
import { Box } from '@mui/material'
4
 
5
import { axios } from './utils'
6
import { labelsAdapter } from './utils/labels'
7
import { setIntlLabels } from './redux/intl/intl.action'
8
import { logout, setPermissions } from './redux/auth/auth.actions'
9
import useFetch from './hooks/useFetch'
10
 
11
import { Chat } from './components/chat'
12
import Header from './components/UI/navbar/Header'
13
import NotificationAlert from './components/UI/notification/NotificationAlert'
14
import Footer from './components/footer/Footer'
15
import AppRoutes from './routes/routes'
16
import GlobalStyle from './styles/GlobalStyles'
17
import Spinner from './components/UI/Spinner'
18
import ErrorPage from './pages/error/error-page'
19
 
20
import './styles/globals.scss'
21
 
22
export default function App() {
23
  const [loading, setLoading] = useState(true)
24
  const [credentialsError, setCredentialsError] = useState(false)
25
  const { data: labels } = useFetch('/language')
26
  const { theme_id, isAuth } = useSelector(({ auth }) => auth)
27
  const dispatch = useDispatch()
28
 
29
  const getCredentials = async () => {
30
    const response = await axios.get('/signin')
31
    const { data, success } = response.data
32
    if (!success) throw new Error(data)
33
    return data
34
  }
35
 
36
  const getUserCredentials = async () => {
37
    setLoading(true)
38
    getCredentials()
39
      .then((credentials) => {
40
        const localAes = window.localStorage.getItem('aes')
41
        const localJwt = window.localStorage.getItem('jwt')
42
        const permissions = structuredClone(credentials)
43
        permissions.isAuth = credentials.is_logged_in
44
 
45
        if (localAes) {
46
          permissions.aes = localAes
47
        } else {
48
          permissions.aes = credentials.aes
49
          window.localStorage.setItem('aes', credentials.aes)
50
        }
51
 
52
        if (localJwt) {
53
          permissions.jwt = localJwt
54
        } else {
55
          permissions.jwt = credentials.jwt
56
          window.localStorage.setItem('jwt', credentials.jwt)
57
        }
58
 
59
        dispatch(setPermissions(permissions))
60
      })
61
      .catch((error) => {
62
        if (error.message.includes('sesión')) {
63
          dispatch(logout())
64
          getUserCredentials()
65
        } else {
66
          setCredentialsError(true)
67
        }
68
      })
69
      .finally(() => setLoading(false))
70
  }
71
 
72
  useEffect(() => {
73
    getUserCredentials()
74
  }, [isAuth])
75
 
76
  useEffect(() => {
77
    dispatch(setIntlLabels(labelsAdapter(labels)))
78
  }, [labels])
79
 
80
  useEffect(() => {
81
    const servicesUrl = document
82
      .querySelector('#app')
83
      .getAttribute('data-service')
84
 
85
    axios.create(`https://${servicesUrl}`)
86
  }, [])
87
 
88
  if (loading) {
89
    return <Spinner />
90
  }
91
 
92
  if (credentialsError) {
93
    return <ErrorPage />
94
  }
95
 
96
  return (
2612 stevensc 97
    <>
98
      <GlobalStyle isAuth={isAuth} />
2611 stevensc 99
 
2612 stevensc 100
      {isAuth ? <Header theme={theme_id} /> : null}
2611 stevensc 101
 
2612 stevensc 102
      <Box minHeight='65vh'>
103
        <AppRoutes />
104
      </Box>
2611 stevensc 105
 
2612 stevensc 106
      {isAuth ? <Chat /> : null}
107
      <NotificationAlert />
108
      <Footer />
109
    </>
2611 stevensc 110
  )
111
}