Proyectos de Subversion LeadersLinked - SPA

Rev

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