| 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>
|
| 4742 |
stevensc |
79 |
<a href={url} className="header__option mobile">
|
| 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 &&
|
| 4709 |
stevensc |
87 |
<nav className='nav__options-dropdown' 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}
|
|
|
93 |
onClick={(e) => {
|
|
|
94 |
e.preventDefault()
|
|
|
95 |
readNotification(link_mark_read, link)
|
|
|
96 |
}}
|
|
|
97 |
>
|
|
|
98 |
{message}
|
|
|
99 |
</a>
|
|
|
100 |
<DeleteOutlineIcon onClick={() => deleteNotification(link_delete)} />
|
|
|
101 |
</div>
|
|
|
102 |
<small style={{ fontSize: '.85rem' }}>
|
|
|
103 |
{time_elapsed}
|
|
|
104 |
</small>
|
|
|
105 |
</li>
|
|
|
106 |
)}
|
|
|
107 |
</ul>
|
|
|
108 |
</nav>
|
|
|
109 |
}
|
|
|
110 |
</li>
|
|
|
111 |
)
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
const mapDispatchToProps = {
|
|
|
115 |
addNotification: (notification) => addNotification(notification),
|
|
|
116 |
};
|
|
|
117 |
|
|
|
118 |
export default connect(null, mapDispatchToProps)(NotificationsOption)
|