Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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