4679 |
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 |
|
|
|
7 |
const ComunicationOptions = ({ sessionLink, Icon, title, url, childs }) => {
|
|
|
8 |
const [messagesCount, setMessagesCount] = useState(0)
|
|
|
9 |
const [loading, setLoading] = useState(false);
|
|
|
10 |
|
|
|
11 |
useEffect(() => {
|
|
|
12 |
let timer;
|
|
|
13 |
if (!loading) {
|
|
|
14 |
timer = setTimeout(() => checkSession(), 1000);
|
|
|
15 |
}
|
|
|
16 |
return () => {
|
|
|
17 |
clearTimeout(timer);
|
|
|
18 |
};
|
|
|
19 |
}, [loading])
|
|
|
20 |
|
|
|
21 |
const checkSession = async () => {
|
|
|
22 |
try {
|
|
|
23 |
setLoading(true)
|
|
|
24 |
const { data: response } = await axios.get(sessionLink)
|
|
|
25 |
const { total_messages } = response.data
|
|
|
26 |
|
|
|
27 |
if (response.success) {
|
|
|
28 |
setMessagesCount(Number(total_messages))
|
|
|
29 |
}
|
|
|
30 |
setLoading(false)
|
|
|
31 |
} catch (error) {
|
|
|
32 |
console.log(error)
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
return (
|
|
|
37 |
<li>
|
|
|
38 |
<a href={url} className="header__option">
|
|
|
39 |
{Icon && <Icon className="header__option-icon" />}
|
|
|
40 |
<span>{title}</span>
|
|
|
41 |
<span className={`badge ${messagesCount ? 'd-block' : 'd-none'}`} style={{ top: '10px' }}>
|
|
|
42 |
{messagesCount}
|
|
|
43 |
</span>
|
|
|
44 |
</a>
|
|
|
45 |
{!!childs.length &&
|
|
|
46 |
<nav className='nav__options-dropdown'>
|
|
|
47 |
<ul>{childs.map((linkOption, index) =>
|
|
|
48 |
<li key={index}>
|
|
|
49 |
{linkOption.childs?.length
|
|
|
50 |
? <a href='/' onClick={(e) => e.preventDefault()}>{linkOption.label}</a>
|
|
|
51 |
: <a href={linkOption.href}>{linkOption.label}</a>
|
|
|
52 |
}
|
|
|
53 |
{!!linkOption.childs?.length &&
|
|
|
54 |
<nav className='navLinkLevelThree'>
|
|
|
55 |
<ul>
|
|
|
56 |
{linkOption.childs?.map((optionsChild, index) =>
|
|
|
57 |
<li className='d-flex align-items-center justify-content-between' key={index}>
|
|
|
58 |
<a href={optionsChild.href}>{optionsChild.label}</a>
|
|
|
59 |
{(optionsChild.label === 'Inmail' && messagesCount) && <span>{messagesCount}</span>}
|
|
|
60 |
</li>
|
|
|
61 |
)}
|
|
|
62 |
</ul>
|
|
|
63 |
</nav>
|
|
|
64 |
}
|
|
|
65 |
</li>
|
|
|
66 |
)}
|
|
|
67 |
</ul>
|
|
|
68 |
</nav>
|
|
|
69 |
}
|
|
|
70 |
</li>
|
|
|
71 |
)
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
const mapDispatchToProps = {
|
|
|
75 |
addNotification: (notification) => addNotification(notification),
|
|
|
76 |
};
|
|
|
77 |
|
|
|
78 |
export default connect(null, mapDispatchToProps)(ComunicationOptions)
|