Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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