Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6632 stevensc 1
/* eslint-disable react/prop-types */
2
import React, { useEffect, useRef, useState } from 'react'
3
import axios from '../../../utils/axios'
4
import { connect } from 'react-redux'
5
import { addNotification } from '../../../redux/notification/notification.actions'
6
import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline'
7
import useOutsideClick from '../../../hooks/useOutsideClick'
8
 
9
const NotificationsOption = ({ addNotification, Icon, title, url, notificationsCount, setNotificationsCount }) => {
10
  const [notifications, setNotifications] = useState([])
11
  const [displayMenu, setDisplayMenu] = useState(false)
12
  const menu = useRef(null)
13
  const outsideClick = useOutsideClick(menu)
14
 
15
  const readNotification = (link_read, link_notification) => {
16
    axios.post(link_read)
17
      .then(({ data }) =>
18
        data.success
19
          ? window.open(link_notification, '_blank').focus()
20
          : addNotification({ style: 'danger', msg: data.data }))
21
  }
22
 
23
  const deleteNotification = (link_delete) => {
24
    axios.post(link_delete)
25
      .then(({ data }) => {
26
        !data.success && addNotification({ style: 'danger', msg: data.data })
27
        setNotificationsCount(notificationsCount - 1)
28
        setNotifications(notifications.filter(notification => notification.link_delete !== link_delete))
29
        addNotification({ style: 'success', msg: data.data })
30
      })
31
  }
32
 
33
  const handleNotifications = () => {
34
    axios.get('/notifications/unreads')
35
      .then(({ data }) => {
36
        if (data.success) {
37
          const notifications = new Set(data.data.notifications)
38
          setNotifications([...notifications])
39
        }
40
      })
41
      .catch(err => {
42
        addNotification({
43
          style: 'error',
44
          msg: 'Disculpe, ha ocurrido un error buscando notificaciones'
45
        })
46
        console.log('>>: err > ', err)
47
      })
48
  }
49
 
50
  const handleClick = (e) => {
51
    if (window.innerWidth > 768) {
52
      e.preventDefault()
53
      setDisplayMenu(!displayMenu)
54
    }
55
  }
56
 
57
  useEffect(() => {
58
    if (outsideClick) setDisplayMenu(false)
59
  }, [outsideClick])
60
 
61
  useEffect(() => {
62
    handleNotifications()
63
  }, [notificationsCount])
64
 
65
  return (
66
        <li ref={menu}>
67
            <a href={url} className={`header__option mobile ${displayMenu && 'active'}`} target='framename' onClick={handleClick}>
68
                {Icon && <Icon className="header__option-icon" />}
69
                <span>{title}</span>
70
                <span className={`badge ${notificationsCount ? 'd-block' : 'd-none'}`} style={{ top: '10px' }}>
71
                    {notificationsCount}
72
                </span>
73
            </a>
74
            {!!notifications.length &&
75
                <nav className={`nav__options-dropdown d-none d-md-block ${displayMenu && 'show'}`} style={{ maxHeight: '300px', overflow: 'auto' }}>
76
                    <ul>{notifications.map(({ message, link_mark_read, link, link_delete, time_elapsed }, index) =>
77
                        <li key={index}>
78
                            <div className="d-flex align-items-center justify-content-between" style={{ gap: '.5rem' }}>
79
                                <a
80
                                    href={link}
81
                                    target='secondary'
82
                                    onClick={(e) => {
83
                                      e.preventDefault()
84
                                      readNotification(link_mark_read, link)
85
                                    }}
86
                                >
87
                                    {message}
88
                                </a>
89
                                <DeleteOutlineIcon onClick={() => deleteNotification(link_delete)} />
90
                            </div>
91
                            <small style={{ fontSize: '.85rem' }}>
92
                                {time_elapsed}
93
                            </small>
94
                        </li>
95
                    )}
96
                    </ul>
97
                </nav>
98
            }
99
        </li>
100
  )
101
}
102
 
103
const mapDispatchToProps = {
104
  addNotification: (notification) => addNotification(notification)
105
}
106
 
107
export default connect(null, mapDispatchToProps)(NotificationsOption)