Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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