| 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)
|
| 4904 |
stevensc |
13 |
const [loading, setLoading] = useState(false)
|
| 4812 |
stevensc |
14 |
const menu = useRef(null)
|
|
|
15 |
const outsideClick = useOutsideClick(menu)
|
| 4630 |
stevensc |
16 |
|
|
|
17 |
const checkSession = async () => {
|
|
|
18 |
try {
|
|
|
19 |
setLoading(true)
|
|
|
20 |
const { data: response } = await axios.get(sessionLink)
|
|
|
21 |
const { total_notifications } = response.data
|
|
|
22 |
|
|
|
23 |
if (response.success) {
|
|
|
24 |
setNotificationsCount(Number(total_notifications))
|
|
|
25 |
}
|
|
|
26 |
setLoading(false)
|
|
|
27 |
} catch (error) {
|
|
|
28 |
console.log(error)
|
|
|
29 |
}
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
const readNotification = (link_read, link_notification) => {
|
|
|
33 |
axios.post(link_read)
|
|
|
34 |
.then(({ data }) =>
|
|
|
35 |
data.success
|
|
|
36 |
? window.open(link_notification, '_blank').focus()
|
|
|
37 |
: addNotification({ style: 'danger', msg: data.data }))
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
const deleteNotification = (link_delete) => {
|
|
|
41 |
axios.post(link_delete)
|
|
|
42 |
.then(({ data }) => {
|
|
|
43 |
!data.success && addNotification({ style: 'danger', msg: data.data })
|
|
|
44 |
setNotificationsCount(prev => prev - 1)
|
|
|
45 |
setNotifications(notifications.filter(notification => notification.link_delete !== link_delete))
|
|
|
46 |
addNotification({ style: 'success', msg: data.data })
|
|
|
47 |
})
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
const handleNotifications = () => {
|
|
|
51 |
axios.get('/notifications/unreads')
|
|
|
52 |
.then(({ data }) => {
|
|
|
53 |
if (data.success) {
|
|
|
54 |
let notifications = new Set(data.data.notifications)
|
|
|
55 |
setNotifications([...notifications])
|
|
|
56 |
}
|
|
|
57 |
})
|
|
|
58 |
.catch(err => {
|
|
|
59 |
addNotification({
|
|
|
60 |
style: "error",
|
|
|
61 |
msg: 'Disculpe, ha ocurrido un error buscando notificaciones',
|
|
|
62 |
})
|
|
|
63 |
console.log('>>: err > ', err)
|
|
|
64 |
})
|
|
|
65 |
}
|
|
|
66 |
|
| 4809 |
stevensc |
67 |
const handleClick = (e) => {
|
|
|
68 |
if (window.innerWidth > 768) {
|
|
|
69 |
e.preventDefault()
|
|
|
70 |
setDisplayMenu(!displayMenu)
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
| 4904 |
stevensc |
74 |
useEffect(() => {
|
|
|
75 |
if (outsideClick) setDisplayMenu(false)
|
|
|
76 |
}, [outsideClick])
|
|
|
77 |
|
|
|
78 |
useEffect(() => {
|
|
|
79 |
handleNotifications()
|
|
|
80 |
}, [notificationsCount])
|
|
|
81 |
|
|
|
82 |
useEffect(() => {
|
|
|
83 |
let timer
|
|
|
84 |
if (!loading) {
|
|
|
85 |
timer = setTimeout(() => checkSession(), 1000)
|
|
|
86 |
}
|
|
|
87 |
return () => {
|
|
|
88 |
clearTimeout(timer)
|
|
|
89 |
}
|
|
|
90 |
}, [loading])
|
|
|
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),
|
| 4904 |
stevensc |
132 |
}
|
| 4630 |
stevensc |
133 |
|
|
|
134 |
export default connect(null, mapDispatchToProps)(NotificationsOption)
|