Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4951 | Ir a la última revisión | | 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 }) => {
4630 stevensc 10
    const [notifications, setNotifications] = useState([])
4809 stevensc 11
    const [displayMenu, setDisplayMenu] = useState(false)
4812 stevensc 12
    const menu = useRef(null)
13
    const outsideClick = useOutsideClick(menu)
4630 stevensc 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 })
4951 stevensc 27
                setNotificationsCount(notificationsCount - 1)
4630 stevensc 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
                    let 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
 
4809 stevensc 50
    const handleClick = (e) => {
51
        if (window.innerWidth > 768) {
52
            e.preventDefault()
53
            setDisplayMenu(!displayMenu)
54
        }
55
    }
56
 
4904 stevensc 57
    useEffect(() => {
58
        if (outsideClick) setDisplayMenu(false)
59
    }, [outsideClick])
60
 
61
    useEffect(() => {
62
        handleNotifications()
63
    }, [notificationsCount])
64
 
4630 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}>
4735 stevensc 78
                            <div className="d-flex align-items-center" style={{ gap: '.5rem' }}>
4630 stevensc 79
                                <a
80
                                    href={link}
4763 stevensc 81
                                    target='secondary'
4630 stevensc 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),
4904 stevensc 105
}
4630 stevensc 106
 
107
export default connect(null, mapDispatchToProps)(NotificationsOption)