Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6632 stevensc 1
/* eslint-disable camelcase */
2
/* eslint-disable react/prop-types */
3
import React, { useEffect, useRef, useState } from 'react'
4
import { axios, useWindowSize } from '../../../utils'
5
import { useForm } from 'react-hook-form'
6
 
7
import SearchIcon from '@mui/icons-material/Search'
8
import HomeIcon from '@mui/icons-material/Home'
9
import BusinessCenterIcon from '@mui/icons-material/BusinessCenter'
10
import SellIcon from '@mui/icons-material/Sell'
11
import PeopleIcon from '@mui/icons-material/People'
12
import GroupsIcon from '@mui/icons-material/Groups'
13
import ChatIcon from '@mui/icons-material/Chat'
14
import MenuIcon from '@mui/icons-material/Menu'
15
import SchoolIcon from '@mui/icons-material/School'
16
 
17
import HeaderOptions from './HeaderOptions'
18
import UserOptions from './UserOptions'
19
 
20
import './Header.scss'
21
let MenuDrawer
22
 
23
const ICON_OPTIONS = [
24
  HomeIcon,
25
  PeopleIcon,
26
  BusinessCenterIcon,
27
  GroupsIcon,
28
  SellIcon,
29
  SchoolIcon,
30
]
31
 
32
const Header = ({
33
  menu,
34
  image = '',
35
  logoForNavbar = '',
36
  fullName,
37
  linkAdmin,
38
  linkImpersonate,
39
  linkKnowledgeArea,
40
  routeKnowledgeArea,
41
  // companyVars,
42
  // isChatPage,
43
  routeCheckSession,
44
  defaultNetwork,
45
}) => {
46
  const [menuItems, setMenuItems] = useState(menu || [])
47
  const [notificationsCount, setNotificationsCount] = useState(0)
48
  const [aditionalItems, setAditionalItems] = useState([])
49
  const [messagesCount, setMessagesCount] = useState(0)
50
  const [userImage, setUserImage] = useState(image)
51
 
52
  const [showMobileSearch, setShowMobileSearch] = useState(false)
53
  const [showDrawer, setShowDrawer] = useState(false)
54
  const [loading, setLoading] = useState(false)
55
 
56
  const [innerWidth] = useWindowSize()
57
 
58
  const searchInput = useRef(null)
59
 
60
  const { handleSubmit, register } = useForm()
61
 
62
  const handleOnSubmit = (data) =>
63
    window.location.replace(`/search/entity/user?keyword=${data.keyword}`)
64
 
65
  const checkUserImage = async () => {
66
    setLoading(true)
67
    const session_image = sessionStorage.getItem('user_session_image')
68
    if (session_image) {
69
      await setUserImage(session_image)
70
      sessionStorage.removeItem('user_session_image')
71
    }
72
    setLoading(false)
73
  }
74
 
75
  const checkSession = async () => {
76
    try {
77
      setLoading(true)
78
      const { data: response } = await axios.get(routeCheckSession)
79
      const { total_messages, total_notifications } = response.data
80
 
81
      if (response.success) {
82
        setMessagesCount(Number(total_messages))
83
        setNotificationsCount(Number(total_notifications))
84
      }
85
      setLoading(false)
86
    } catch (error) {
87
      console.log(error)
88
    }
89
  }
90
 
91
  const handleDisplayMobileSearch = () => {
92
    if (window.innerWidth < 992) {
93
      setShowMobileSearch(true)
94
    }
95
  }
96
 
97
  const getKnowledgeRoutes = () => {
6693 stevensc 98
    const childs = [{ label: 'Mi Coach', href: '/my-coach' }]
6632 stevensc 99
    if (linkKnowledgeArea) {
100
      childs.push({
101
        label: 'Área de conocimiento',
102
        href: routeKnowledgeArea,
103
      })
104
    }
105
    return childs
106
  }
107
 
108
  useEffect(() => {
109
    let timer
110
    if (!loading) {
111
      timer = setTimeout(() => {
112
        checkUserImage()
113
        checkSession()
114
      }, 2000)
115
    }
116
    return () => {
117
      clearTimeout(timer)
118
    }
119
  }, [loading])
120
 
121
  useEffect(() => {
122
    if (menu.length > 5) {
123
      setMenuItems(menu.splice(0, 5))
124
      setAditionalItems(menu.splice(menu.length - 5))
125
    }
126
  }, [])
127
 
128
  useEffect(() => {
129
    if (innerWidth < 992) {
130
      MenuDrawer = React.lazy(() => import('./Drawer'))
131
    }
132
  }, [innerWidth])
133
 
134
  useEffect(() => {
135
    const handleClickOutside = (event) => {
136
      if (
137
        searchInput?.current &&
138
        !searchInput?.current?.contains(event.target)
139
      ) {
140
        setShowMobileSearch(false)
141
      }
142
    }
143
    document.addEventListener('mousedown', handleClickOutside)
144
 
145
    return () => {
146
      document.removeEventListener('mousedown', handleClickOutside)
147
    }
148
  }, [searchInput])
149
 
150
  return (
151
    <>
152
      <div className="header">
153
        <div className="container px-0">
154
          <div className="header__nav">
155
            <div className={`header__left ${showMobileSearch && 'show'}`}>
156
              <a href="/">
157
                <img src={logoForNavbar} alt="Logo" />
158
              </a>
159
 
160
              <form
161
                className="header__search show"
162
                onClick={handleDisplayMobileSearch}
163
                onSubmit={handleSubmit(handleOnSubmit)}
164
                ref={searchInput}
165
              >
166
                <SearchIcon />
167
                <input
168
                  type="text"
169
                  placeholder="Search"
170
                  name="keyword"
171
                  ref={register}
172
                />
173
              </form>
174
            </div>
175
 
176
            <nav className={`header__right ${showMobileSearch && 'd-none'}`}>
177
              <ul>
178
                {menuItems.map((item, index) => {
179
                  return (
180
                    <HeaderOptions
181
                      key={index}
182
                      Icon={ICON_OPTIONS[index]}
183
                      title={item.label}
184
                      url={item.href}
185
                      childs={item.childs}
186
                      ajaxRequest={item.ajax}
187
                    />
188
                  )
189
                })}
190
                <HeaderOptions
191
                  Icon={SchoolIcon}
192
                  title="Conocimiento"
193
                  url={routeKnowledgeArea}
194
                  childs={getKnowledgeRoutes()}
195
                />
196
                <HeaderOptions
197
                  Icon={ChatIcon}
198
                  title="Comunicación"
199
                  badgeCount={notificationsCount || messagesCount}
200
                  childs={[
201
                    ...aditionalItems,
202
                    { label: 'Inmail', href: '/inmail', count: messagesCount },
203
                    { label: 'Chat', href: '/chat' },
204
                    {
205
                      label: 'Notificaciones',
206
                      href: '/notifications',
207
                      count: notificationsCount,
208
                    },
209
                  ]}
210
                  isMobile={true}
211
                />
212
                <UserOptions
213
                  image={userImage}
214
                  name={fullName}
215
                  adminUrl={linkAdmin}
216
                  impersonateUrl={linkImpersonate}
217
                  defaultNetwork={defaultNetwork}
218
                  knowledgeAuth={linkKnowledgeArea}
219
                  routeKnowledge={routeKnowledgeArea}
220
                />
221
                {MenuDrawer && (
222
                  <li className="d-md-none">
223
                    <a
224
                      href="/"
225
                      className={'header__option mobile'}
226
                      onClick={(e) => {
227
                        e.preventDefault()
228
                        setShowDrawer(!showDrawer)
229
                      }}
230
                    >
231
                      <MenuIcon />
232
                    </a>
233
                  </li>
234
                )}
235
              </ul>
236
            </nav>
237
          </div>
238
        </div>
239
      </div>
240
      {MenuDrawer && (
241
        <React.Suspense fallback={null}>
242
          <MenuDrawer
243
            items={[
244
              ...menuItems,
245
              {
246
                label: 'Conocimiento',
247
                href: 'Conocimiento',
248
                img: '/images/navbar/market-place.svg',
249
                ajax: 0,
250
                childs: getKnowledgeRoutes(),
251
              },
252
            ]}
253
            icons={ICON_OPTIONS}
254
            isOpen={showDrawer}
255
            closeDrawer={() => setShowDrawer(false)}
256
          />
257
        </React.Suspense>
258
      )}
259
    </>
260
  )
261
}
262
 
263
export default Header