Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6694 stevensc 1
import React, { useState, useEffect } from 'react'
2
import { axios } from '../../utils'
3
import { getBackendVars } from '../../services/backendVars'
4
 
7085 stevensc 5
import DefaultNavbar from './default/Navbar'
6
import LinkendInHeader from './linkedin/Header'
7
 
6755 stevensc 8
const Header = ({ theme = 1 }) => {
6694 stevensc 9
  const [backendVars, setBackendVars] = useState(null)
6699 stevensc 10
  const [notificationsCount, setNotificationsCount] = useState(0)
11
  const [messagesCount, setMessagesCount] = useState(0)
6694 stevensc 12
  const [loading, setLoading] = useState(false)
13
 
6846 stevensc 14
  const checkSession = async (url) => {
6694 stevensc 15
    try {
16
      setLoading(true)
6846 stevensc 17
      const { data: response } = await axios.get(url)
6694 stevensc 18
      const { total_messages, total_notifications } = response.data
19
 
20
      if (response.success) {
7233 stevensc 21
        const msgs = Number(total_messages)
22
        const nots = Number(total_notifications)
23
 
24
        msgs !== messagesCount && setMessagesCount(msgs)
25
        nots !== notificationsCount && setNotificationsCount(nots)
6694 stevensc 26
      }
7233 stevensc 27
 
6694 stevensc 28
      setLoading(false)
29
    } catch (error) {
7233 stevensc 30
      throw new Error(error)
6694 stevensc 31
    }
32
  }
33
 
34
  useEffect(() => {
35
    getBackendVars('/helpers/menu')
6700 stevensc 36
      .then((results) => {
6694 stevensc 37
        const knowledgeRoutes = {
38
          label: 'Conocimiento',
39
          href: '/knowledge',
40
          img: '',
6699 stevensc 41
          childs: [],
6694 stevensc 42
          ajax: 0,
43
        }
44
 
6699 stevensc 45
        const comunicationRoutes = {
46
          label: 'Comunicación',
47
          href: '/comunication',
48
          img: '',
49
          childs: [
50
            { label: 'Calendario', href: '/calendar' },
51
            { label: 'Inmail', href: '/inmail', count: messagesCount },
52
            { label: 'Chat', href: '/chat' },
53
            {
54
              label: 'Notificaciones',
55
              href: '/notifications',
56
              count: notificationsCount,
57
            },
58
          ],
59
          ajax: 0,
60
        }
6694 stevensc 61
 
6701 stevensc 62
        const menuItems = [
63
          ...results.menu.splice(0, 5),
64
          knowledgeRoutes,
65
          comunicationRoutes,
66
        ]
6699 stevensc 67
 
6700 stevensc 68
        if (results.linkMyCoach) {
69
          knowledgeRoutes.childs.push({ label: 'Mi Coach', href: '/my-coach' })
70
        }
71
 
6699 stevensc 72
        if (results.linkKnowledgeArea) {
73
          knowledgeRoutes.childs.push({
74
            label: 'Área de conocimiento',
75
            href: results.routeKnowledgeArea,
76
          })
77
        }
78
 
6700 stevensc 79
        setBackendVars({ ...results, menu: menuItems })
6694 stevensc 80
      })
81
      .catch((err) => {
82
        console.log(err)
83
        throw new Error(err)
84
      })
85
  }, [])
86
 
87
  useEffect(() => {
6846 stevensc 88
    if (loading) return
89
 
90
    const timer = setTimeout(() => {
91
      checkSession(backendVars?.routeCheckSession)
92
    }, 2000)
93
 
6694 stevensc 94
    return () => {
95
      clearTimeout(timer)
96
    }
6846 stevensc 97
  }, [loading, backendVars])
6694 stevensc 98
 
7233 stevensc 99
  useEffect(() => {
100
    const newMenu = [...backendVars.menu]
101
    const comunications = newMenu[newMenu.length - 1]
102
 
103
    comunications.childs.forEach.map((child) => {
104
      if (child.label === 'Inmail') {
105
        child.count = messagesCount
106
      }
107
      if (child.label === 'Notificaciones') {
108
        child.count = notificationsCount
109
      }
110
    })
111
 
112
    newMenu[newMenu.length - 1] = comunications
113
 
114
    setBackendVars((prevVars) => {
115
      return {
116
        ...prevVars,
117
        menu: newMenu,
118
      }
119
    })
120
  }, [messagesCount, notificationsCount])
121
 
7087 stevensc 122
  if (theme === 2) {
123
    return (
124
      <LinkendInHeader
125
        menu={backendVars?.menu}
126
        logo={backendVars?.logoForNavbar}
127
        fullName={backendVars?.fullName}
128
        notificationsCount={notificationsCount}
129
        messagesCount={messagesCount}
130
        {...backendVars}
131
      />
132
    )
6694 stevensc 133
  }
7087 stevensc 134
 
135
  return (
136
    <DefaultNavbar
137
      menu={backendVars?.menu}
138
      logo={backendVars?.logoForNavbar}
139
      name={backendVars?.fullName}
140
      {...backendVars}
141
    />
142
  )
6694 stevensc 143
}
144
 
145
export default Header