Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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