Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2613 | Rev 2803 | 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'
2776 stevensc 6
import { useFetch } from '@hooks'
2611 stevensc 7
import { labelsAdapter } from './utils/labels'
8
import { setIntlLabels } from './redux/intl/intl.action'
9
import { logout, setPermissions } from './redux/auth/auth.actions'
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
  if (loading) {
81
    return <Spinner />
82
  }
83
 
84
  if (credentialsError) {
85
    return <ErrorPage />
86
  }
87
 
88
  return (
2612 stevensc 89
    <>
90
      <GlobalStyle isAuth={isAuth} />
2611 stevensc 91
 
2612 stevensc 92
      {isAuth ? <Header theme={theme_id} /> : null}
2611 stevensc 93
 
2612 stevensc 94
      <Box minHeight='65vh'>
95
        <AppRoutes />
96
      </Box>
2611 stevensc 97
 
2612 stevensc 98
      {isAuth ? <Chat /> : null}
99
      <NotificationAlert />
100
      <Footer />
101
    </>
2611 stevensc 102
  )
103
}