Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4683 | Autoría | Ultima modificación | Ver Log |

/* eslint-disable react/prop-types */
import React, { useEffect, useState } from 'react'
import axios from '../../../utils/axios'
import { connect } from 'react-redux'
import { addNotification } from '../../../redux/notification/notification.actions'

const ComunicationOptions = ({ sessionLink, Icon, title, url, childs }) => {
    const [messagesCount, setMessagesCount] = useState(0)
    const [loading, setLoading] = useState(false);

    useEffect(() => {
        let timer;
        if (!loading) {
            timer = setTimeout(() => checkSession(), 1000);
        }
        return () => {
            clearTimeout(timer);
        };
    }, [loading])

    const checkSession = async () => {
        try {
            setLoading(true)
            const { data: response } = await axios.get(sessionLink)
            const { total_messages } = response.data

            if (response.success) {
                setMessagesCount(Number(total_messages))
            }
            setLoading(false)
        } catch (error) {
            console.log(error)
        }
    }

    return (
        <li>
            <a href={url} className="header__option mobile">
                {Icon && <Icon className="header__option-icon" />}
                <span>{title}</span>
                <span className={`badge ${messagesCount ? 'd-block' : 'd-none'}`} style={{ top: '10px' }}>
                    {messagesCount}
                </span>
            </a>
            {!!childs.length &&
                <nav className='nav__options-dropdown'>
                    <ul>{childs.map((linkOption, index) =>
                        <li key={index}>
                            <li className='d-flex align-items-center justify-content-between' key={index}>
                                <a href={linkOption.href}>{linkOption.label}</a>
                                {(linkOption.label === 'Inmail' && messagesCount) &&
                                    <span className={`badge ${messagesCount ? 'd-block position-relative' : 'd-none'}`} style={{left: 0, transform: 'none'}}>
                                        {messagesCount}
                                    </span>}
                            </li>
                        </li>
                    )}
                    </ul>
                </nav>
            }
        </li>
    )
}

const mapDispatchToProps = {
    addNotification: (notification) => addNotification(notification),
};

export default connect(null, mapDispatchToProps)(ComunicationOptions)