Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
5112 stevensc 1
/* eslint-disable camelcase */
4250 stevensc 2
/* eslint-disable react/prop-types */
3
/* eslint-disable react/jsx-key */
5110 stevensc 4
import React, { useEffect, useState } from 'react'
5
import axios from '../../../../utils/axios'
6
import { connect } from 'react-redux'
7
import { HiOutlineTag } from 'react-icons/hi'
8
import { addNotification } from '../../../../redux/notification/notification.actions'
4250 stevensc 9
 
10
const ICON_OPTIONS = [
11
  <img src="/images/icons/home.png" className="img-icon lg" />,
12
  <img src="/images/icons/conecctions.png" className="img-icon lg" />,
13
  <img src="/images/icons/company.png" className="img-icon lg" />,
14
  <img src="/images/icons/groups.png" className="img-icon lg" />,
15
  <HiOutlineTag />
16
]
17
 
18
const ADD_OPTIONS = [
5110 stevensc 19
  <i className="fa fa-calendar img-icon lg" />
4250 stevensc 20
]
21
 
22
const NavLinks = ({ menuData, sessionLink, addNotification }) => {
23
  const [menuItems, setMenuItems] = useState(menuData || [])
24
  const [aditionalItems, setAditionalItems] = useState([])
25
  const [notifications, setNotifications] = useState([])
26
  const [notificationsCount, setNotificationsCount] = useState(0)
27
  const [totalMessages, setTotalMessages] = useState(0)
5110 stevensc 28
  const [loading, setLoading] = useState(false)
4250 stevensc 29
 
30
  useEffect(() => {
31
    handleNotifications()
32
  }, [notificationsCount])
33
 
34
  useEffect(() => {
35
    if (menuData.length > 5) {
36
      setMenuItems(menuData.splice(0, 5))
37
      setAditionalItems(menuData.splice(menuData.length - 5))
38
    }
39
  }, [])
40
 
41
  useEffect(() => {
5110 stevensc 42
    let timer
4250 stevensc 43
    if (!loading) {
5110 stevensc 44
      timer = setTimeout(() => checkSession(), 1000)
4250 stevensc 45
    }
46
    return () => {
5110 stevensc 47
      clearTimeout(timer)
48
    }
4250 stevensc 49
  }, [loading])
50
 
51
  const checkSession = async () => {
52
    try {
53
      setLoading(true)
54
 
55
      const { data: response } = await axios.get(sessionLink)
56
      const { total_messages, total_notifications } = response.data
57
 
58
      if (response.success) {
59
        setNotificationsCount(Number(total_notifications))
60
        setTotalMessages(Number(total_messages))
61
      }
62
 
63
      setLoading(false)
64
    } catch (error) {
65
      console.log(error)
66
    }
67
  }
68
 
69
  const readNotification = (link_read, link_notification) => {
70
    axios.post(link_read)
71
      .then(({ data }) =>
72
        data.success
73
          ? window.open(link_notification, '_blank').focus()
74
          : addNotification({ style: 'danger', msg: data.data }))
75
  }
76
 
77
  const deleteNotification = (link_delete) => {
78
    axios.post(link_delete)
79
      .then(({ data }) => {
80
        !data.success && addNotification({ style: 'danger', msg: data.data })
81
        setNotificationsCount(prev => prev - 1)
82
        setNotifications(notifications.filter(notification => notification.link_delete !== link_delete))
83
        addNotification({ style: 'success', msg: data.data })
84
      })
85
  }
86
 
87
  const handleNotifications = () => {
88
    axios.get('/notifications/unreads')
89
      .then(({ data }) => {
90
        if (data.success) {
5110 stevensc 91
          const notifications = new Set(data.data.notifications)
4250 stevensc 92
          setNotifications([...notifications])
93
        }
94
      })
95
      .catch(err => {
96
        addNotification({
5110 stevensc 97
          style: 'error',
98
          msg: 'Disculpe, ha ocurrido un error buscando notificaciones'
4250 stevensc 99
        })
100
        console.log('>>: err > ', err)
101
      })
102
  }
103
 
5004 stevensc 104
  const handleAjaxRequest = async (url) => {
105
    try {
106
      const { data } = await axios.get(url)
107
      if (data.success) window.open(data.data, '_backend')
108
    } catch (error) {
109
      console.log('>>: error > ', error)
110
    }
111
  }
112
 
4250 stevensc 113
  return (
114
    <ul>
115
      {menuItems.map((item, index) =>
116
        <li key={index}>
117
          <a
5004 stevensc 118
            href={item.childs.length ? '/' : item.href}
119
            onClick={(e) => {
120
              if (item.ajax) {
121
                e.preventDefault()
122
                handleAjaxRequest(item.href)
123
              }
124
              if (item.childs.length) {
125
                e.preventDefault()
126
              }
127
            }}
4250 stevensc 128
          >
129
            {ICON_OPTIONS[index]}
130
            <p>{item.label}</p>
131
          </a>
132
          {!!item.childs.length &&
133
            <nav className='navLinkDropdown'>
134
              <ul>
135
                {item.childs.map((_element, _i) =>
136
                  <li key={_i}>
137
                    {_element.childs?.length
5004 stevensc 138
                      ? <a href='/' onClick={(e) => e.preventDefault()}>{_element.label}</a>
139
                      : <a href={_element.href[0] === '/' ? _element.href : `/${_element.href}`} target='framename'>{_element.label}</a>
4250 stevensc 140
                    }
141
                    {!!_element.childs?.length &&
142
                      <>
143
                        <i className="fa fa-angle-right" />
5110 stevensc 144
                        <nav className='navigation-level_three'>
4250 stevensc 145
                          <ul>
146
                            {_element.childs?.map((levelThree, index) =>
147
                              <li key={index}>
5004 stevensc 148
                                <a href={levelThree.href}>
4250 stevensc 149
                                  {levelThree.label}
150
                                </a>
151
                              </li>
152
                            )}
153
                          </ul>
154
                        </nav>
155
                      </>
156
                    }
157
                  </li>
158
                )}
159
              </ul>
160
            </nav>
161
          }
162
        </li>
163
      )}
164
      <li>
165
        <a href="/inmail">
166
          <i className="fa fa-envelope-o" />
167
          <p>Inmail</p>
168
          <span className={`badge ${totalMessages ? 'd-block' : 'd-none'}`}>
169
            {totalMessages}
170
          </span>
171
        </a>
172
      </li>
173
      <li className='d-md-none'>
174
        <a href="/chat">
175
          <i className="fa fa-comment-o" />
176
          <p>Chat</p>
177
          <span className="badge" />
178
        </a>
179
      </li>
180
      {!!aditionalItems.length &&
181
        <li>
182
          <a
183
            href={'#'}
184
            onClick={(e) => e.preventDefault()}
185
          >
186
            <i className="fa fa-inbox img-icon lg" />
187
            <p>Comunicación</p>
188
          </a>
189
          <div className="aditional_links">
190
            <ul>
191
              {aditionalItems.map((item, index) =>
192
                <li key={index}>
193
                  <a href={item.href}>
194
                    {ADD_OPTIONS[index] || null}
195
                    <p>{item.label}</p>
196
                  </a>
197
                </li>
198
              )}
199
              <li>
200
                <a href="/notifications">
201
                  <img src="/images/icons/bell.png" className="img-icon lg" />
202
                  <p>Notificaciones</p>
203
                  <span className={`badge ${notificationsCount ? 'd-block' : 'd-none'}`}>
204
                    {notificationsCount}
205
                  </span>
206
                </a>
207
                {!!notifications.length &&
5117 stevensc 208
                  <nav className='navigation-level_three'>
4250 stevensc 209
                    <ul>
210
                      {[...notifications].reverse().map(element => {
211
                        return (
212
                          <li key={element.message} className="d-block text-center">
5112 stevensc 213
                            <div className="d-flex align-items-center">
4250 stevensc 214
                              <a
215
                                href={element.link}
216
                                onClick={(e) => {
217
                                  e.preventDefault()
218
                                  readNotification(element.link_mark_read, element.link)
219
                                }}
220
                              >
221
                                {element.message}
222
                              </a>
223
                              <i className="ml-3 fa fa-trash-o cursor-pointer" onClick={() => deleteNotification(element.link_delete)} />
224
                            </div>
225
                            <small style={{ fontSize: '.85rem' }}>
226
                              {element.time_elapsed}
227
                            </small>
228
                          </li>
229
                        )
230
                      })
231
                      }
232
                    </ul>
233
                  </nav>
234
                }
235
              </li>
236
            </ul>
237
          </div>
238
        </li>
239
      }
240
    </ul>
5110 stevensc 241
  )
242
}
4250 stevensc 243
 
244
const mapDispatchToProps = {
5110 stevensc 245
  addNotification: (notification) => addNotification(notification)
246
}
4250 stevensc 247
 
5110 stevensc 248
export default connect(null, mapDispatchToProps)(NavLinks)