Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7234 | | 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(() => {
7234 stevensc 100
    if (backendVars) {
101
      const newMenu = [...backendVars.menu]
102
      const comunications = newMenu[newMenu.length - 1]
7233 stevensc 103
 
7235 stevensc 104
      comunications.childs.forEach((child) => {
7234 stevensc 105
        if (child.label === 'Inmail') {
106
          child.count = messagesCount
107
        }
108
        if (child.label === 'Notificaciones') {
109
          child.count = notificationsCount
110
        }
111
      })
7233 stevensc 112
 
7234 stevensc 113
      newMenu[newMenu.length - 1] = comunications
7233 stevensc 114
 
7234 stevensc 115
      setBackendVars((prevVars) => {
116
        return {
117
          ...prevVars,
118
          menu: newMenu,
119
        }
120
      })
121
    }
7233 stevensc 122
  }, [messagesCount, notificationsCount])
123
 
7087 stevensc 124
  if (theme === 2) {
125
    return (
126
      <LinkendInHeader
127
        menu={backendVars?.menu}
128
        logo={backendVars?.logoForNavbar}
129
        fullName={backendVars?.fullName}
130
        notificationsCount={notificationsCount}
131
        messagesCount={messagesCount}
132
        {...backendVars}
133
      />
134
    )
6694 stevensc 135
  }
7087 stevensc 136
 
137
  return (
138
    <DefaultNavbar
139
      menu={backendVars?.menu}
140
      logo={backendVars?.logoForNavbar}
141
      name={backendVars?.fullName}
142
      {...backendVars}
143
    />
144
  )
6694 stevensc 145
}
146
 
147
export default Header