Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4812 | Rev 4904 | 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
 
9
const NotificationsOption = ({ addNotification, sessionLink, Icon, title, url, }) => {
10
    const [notifications, setNotifications] = useState([])
4809 stevensc 11
    const [displayMenu, setDisplayMenu] = useState(false)
4630 stevensc 12
    const [notificationsCount, setNotificationsCount] = useState(0)
13
    const [loading, setLoading] = useState(false);
4812 stevensc 14
    const menu = useRef(null)
15
    const outsideClick = useOutsideClick(menu)
4630 stevensc 16
 
17
    useEffect(() => {
4812 stevensc 18
        if (outsideClick) setDisplayMenu(false)
19
    }, [outsideClick])
20
 
21
    useEffect(() => {
4630 stevensc 22
        handleNotifications()
23
    }, [notificationsCount])
24
 
25
    useEffect(() => {
26
        let timer;
27
        if (!loading) {
28
            timer = setTimeout(() => checkSession(), 1000);
29
        }
30
        return () => {
31
            clearTimeout(timer);
32
        };
33
    }, [loading])
34
 
35
    const checkSession = async () => {
36
        try {
37
            setLoading(true)
38
            const { data: response } = await axios.get(sessionLink)
39
            const { total_notifications } = response.data
40
 
41
            if (response.success) {
42
                setNotificationsCount(Number(total_notifications))
43
            }
44
            setLoading(false)
45
        } catch (error) {
46
            console.log(error)
47
        }
48
    }
49
 
50
    const readNotification = (link_read, link_notification) => {
51
        axios.post(link_read)
52
            .then(({ data }) =>
53
                data.success
54
                    ? window.open(link_notification, '_blank').focus()
55
                    : addNotification({ style: 'danger', msg: data.data }))
56
    }
57
 
58
    const deleteNotification = (link_delete) => {
59
        axios.post(link_delete)
60
            .then(({ data }) => {
61
                !data.success && addNotification({ style: 'danger', msg: data.data })
62
                setNotificationsCount(prev => prev - 1)
63
                setNotifications(notifications.filter(notification => notification.link_delete !== link_delete))
64
                addNotification({ style: 'success', msg: data.data })
65
            })
66
    }
67
 
68
    const handleNotifications = () => {
69
        axios.get('/notifications/unreads')
70
            .then(({ data }) => {
71
                if (data.success) {
72
                    let notifications = new Set(data.data.notifications)
73
                    setNotifications([...notifications])
74
                }
75
            })
76
            .catch(err => {
77
                addNotification({
78
                    style: "error",
79
                    msg: 'Disculpe, ha ocurrido un error buscando notificaciones',
80
                })
81
                console.log('>>: err > ', err)
82
            })
83
    }
84
 
4809 stevensc 85
    const handleClick = (e) => {
86
        if (window.innerWidth > 768) {
87
            e.preventDefault()
88
            setDisplayMenu(!displayMenu)
89
        }
90
    }
91
 
4630 stevensc 92
    return (
4812 stevensc 93
        <li ref={menu}>
4814 stevensc 94
            <a href={url} className={`header__option mobile ${displayMenu && 'active'}`} target='framename' onClick={handleClick}>
4630 stevensc 95
                {Icon && <Icon className="header__option-icon" />}
96
                <span>{title}</span>
4631 stevensc 97
                <span className={`badge ${notificationsCount ? 'd-block' : 'd-none'}`} style={{ top: '10px' }}>
4630 stevensc 98
                    {notificationsCount}
99
                </span>
100
            </a>
101
            {!!notifications.length &&
4809 stevensc 102
                <nav className={`nav__options-dropdown d-none d-md-block ${displayMenu && 'show'}`} style={{ maxHeight: '300px', overflow: 'auto' }}>
4630 stevensc 103
                    <ul>{notifications.map(({ message, link_mark_read, link, link_delete, time_elapsed }, index) =>
104
                        <li key={index}>
4735 stevensc 105
                            <div className="d-flex align-items-center" style={{ gap: '.5rem' }}>
4630 stevensc 106
                                <a
107
                                    href={link}
4763 stevensc 108
                                    target='secondary'
4630 stevensc 109
                                    onClick={(e) => {
110
                                        e.preventDefault()
111
                                        readNotification(link_mark_read, link)
112
                                    }}
113
                                >
114
                                    {message}
115
                                </a>
116
                                <DeleteOutlineIcon onClick={() => deleteNotification(link_delete)} />
117
                            </div>
118
                            <small style={{ fontSize: '.85rem' }}>
119
                                {time_elapsed}
120
                            </small>
121
                        </li>
122
                    )}
123
                    </ul>
124
                </nav>
125
            }
126
        </li>
127
    )
128
}
129
 
130
const mapDispatchToProps = {
131
    addNotification: (notification) => addNotification(notification),
132
};
133
 
134
export default connect(null, mapDispatchToProps)(NotificationsOption)