Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4952 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4630 stevensc 1
/* eslint-disable react/prop-types */
4812 stevensc 2
import React, { useEffect, useRef, useState } from 'react'
4630 stevensc 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'
4812 stevensc 7
import useOutsideClick from '../../../hooks/useOutsideClick'
4630 stevensc 8
 
4951 stevensc 9
const NotificationsOption = ({ addNotification, Icon, title, url, notificationsCount, setNotificationsCount }) => {
5361 stevensc 10
  const [notifications, setNotifications] = useState([])
11
  const [displayMenu, setDisplayMenu] = useState(false)
12
  const menu = useRef(null)
13
  const outsideClick = useOutsideClick(menu)
4630 stevensc 14
 
5361 stevensc 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
  }
4630 stevensc 22
 
5361 stevensc 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
  }
4630 stevensc 32
 
5361 stevensc 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
  }
4630 stevensc 49
 
5361 stevensc 50
  const handleClick = (e) => {
51
    if (window.innerWidth > 768) {
52
      e.preventDefault()
53
      setDisplayMenu(!displayMenu)
4809 stevensc 54
    }
5361 stevensc 55
  }
4809 stevensc 56
 
5361 stevensc 57
  useEffect(() => {
58
    if (outsideClick) setDisplayMenu(false)
59
  }, [outsideClick])
4904 stevensc 60
 
5361 stevensc 61
  useEffect(() => {
62
    handleNotifications()
63
  }, [notificationsCount])
4904 stevensc 64
 
5361 stevensc 65
  return (
4812 stevensc 66
        <li ref={menu}>
4814 stevensc 67
            <a href={url} className={`header__option mobile ${displayMenu && 'active'}`} target='framename' onClick={handleClick}>
4630 stevensc 68
                {Icon && <Icon className="header__option-icon" />}
69
                <span>{title}</span>
4952 stevensc 70
                <span className={`badge ${notificationsCount ? 'd-block' : 'd-none'}`} style={{ top: '10px' }}>
71
                    {notificationsCount}
72
                </span>
4630 stevensc 73
            </a>
74
            {!!notifications.length &&
4809 stevensc 75
                <nav className={`nav__options-dropdown d-none d-md-block ${displayMenu && 'show'}`} style={{ maxHeight: '300px', overflow: 'auto' }}>
4630 stevensc 76
                    <ul>{notifications.map(({ message, link_mark_read, link, link_delete, time_elapsed }, index) =>
77
                        <li key={index}>
5361 stevensc 78
                            <div className="d-flex align-items-center justify-content-between" style={{ gap: '.5rem' }}>
4630 stevensc 79
                                <a
80
                                    href={link}
4763 stevensc 81
                                    target='secondary'
4630 stevensc 82
                                    onClick={(e) => {
5361 stevensc 83
                                      e.preventDefault()
84
                                      readNotification(link_mark_read, link)
4630 stevensc 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>
5361 stevensc 100
  )
4630 stevensc 101
}
102
 
103
const mapDispatchToProps = {
5361 stevensc 104
  addNotification: (notification) => addNotification(notification)
4904 stevensc 105
}
4630 stevensc 106
 
5361 stevensc 107
export default connect(null, mapDispatchToProps)(NotificationsOption)