Rev 6842 | Rev 7087 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useState, useEffect } from 'react'
import DefaultNavbar from './default/Navbar'
import LinkendInHeader from './linkedin/Header'
import { axios } from '../../utils'
import { getBackendVars } from '../../services/backendVars'
const Header = ({ theme = 1 }) => {
const [backendVars, setBackendVars] = useState(null)
const [notificationsCount, setNotificationsCount] = useState(0)
const [messagesCount, setMessagesCount] = useState(0)
const [userImage, setUserImage] = useState('')
const [loading, setLoading] = useState(false)
const checkUserImage = () => {
const session_image = sessionStorage.getItem('user_session_image')
if (!session_image) {
return
}
setUserImage(session_image)
sessionStorage.removeItem('user_session_image')
}
const checkSession = async (url) => {
try {
setLoading(true)
const { data: response } = await axios.get(url)
const { total_messages, total_notifications } = response.data
if (response.success) {
setMessagesCount(Number(total_messages))
setNotificationsCount(Number(total_notifications))
}
setLoading(false)
} catch (error) {
console.log(error)
}
}
useEffect(() => {
getBackendVars('/helpers/menu')
.then((results) => {
const knowledgeRoutes = {
label: 'Conocimiento',
href: '/knowledge',
img: '',
childs: [],
ajax: 0,
}
const comunicationRoutes = {
label: 'Comunicación',
href: '/comunication',
img: '',
childs: [
{ label: 'Calendario', href: '/calendar' },
{ label: 'Inmail', href: '/inmail', count: messagesCount },
{ label: 'Chat', href: '/chat' },
{
label: 'Notificaciones',
href: '/notifications',
count: notificationsCount,
},
],
ajax: 0,
}
const menuItems = [
...results.menu.splice(0, 5),
knowledgeRoutes,
comunicationRoutes,
]
if (results.linkMyCoach) {
knowledgeRoutes.childs.push({ label: 'Mi Coach', href: '/my-coach' })
}
if (results.linkKnowledgeArea) {
knowledgeRoutes.childs.push({
label: 'Área de conocimiento',
href: results.routeKnowledgeArea,
})
}
setUserImage(results.image)
setBackendVars({ ...results, menu: menuItems })
})
.catch((err) => {
console.log(err)
throw new Error(err)
})
}, [])
useEffect(() => {
if (loading) return
const timer = setTimeout(() => {
checkUserImage()
checkSession(backendVars?.routeCheckSession)
}, 2000)
return () => {
clearTimeout(timer)
}
}, [loading, backendVars])
switch (theme) {
case 1:
return (
<DefaultNavbar
menu={backendVars?.menu}
image={userImage}
logo={backendVars?.logoForNavbar}
name={backendVars?.fullName}
{...backendVars}
/>
)
case 2:
return (
<LinkendInHeader
menu={backendVars?.menu}
image={userImage}
logo={backendVars?.logoForNavbar}
fullName={backendVars?.fullName}
notificationsCount={notificationsCount}
messagesCount={messagesCount}
{...backendVars}
/>
)
default:
return (
<DefaultNavbar
menu={backendVars?.menu}
image={userImage}
logo={backendVars?.logoForNavbar}
name={backendVars?.fullName}
{...backendVars}
/>
)
}
}
export default Header