Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6698 | Rev 6700 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React, { useState, useEffect } from 'react'
import DefaultNavbar from './default/Navbar'
import { axios } from '../../utils'
import { getBackendVars } from '../../services/backendVars'

const results = {
  menu: [
    {
      label: 'Inicio',
      href: 'dashboard',
      img: '/images/navbar/home.svg',
      ajax: 0,
      childs: [],
    },
    {
      label: 'Perfil',
      href: 'profile',
      img: '/images/navbar/perfil.svg',
      ajax: 0,
      childs: [
        {
          label: 'Conexiones',
          href: 'connection',
          childs: [
            {
              label: 'Mis Conexiones',
              href: 'connection/my-connections',
            },
            {
              label: 'Invitaciones enviadas',
              href: 'connection/invitations-sent',
            },
            {
              label: 'Invitaciones recibidas',
              href: 'connection/invitations-received',
            },
            {
              label: 'Personas que quizás conozcas',
              href: 'connection/people-you-may-know',
            },
            {
              label: 'Personas bloqueadas',
              href: 'connection/people-blocked',
            },
          ],
        },
        {
          label: 'Mis perfiles',
          href: 'profile/my-profiles',
          childs: [],
        },
        {
          label: 'Empleos',
          href: 'job',
          childs: [
            {
              label: 'Que he aplicado',
              href: 'job/applied-jobs',
            },
            {
              label: 'Guardados',
              href: 'job/saved-jobs',
            },
          ],
        },
        {
          label: 'Quién ha visto mi perfil',
          href: 'profile/people-viewed-profile',
          childs: [],
        },
      ],
    },
    {
      label: 'Empresas',
      href: 'company',
      img: '/images/navbar/empresa.svg',
      ajax: 0,
      childs: [
        {
          label: 'Mis empresas',
          href: 'company/my-companies',
          childs: [],
        },
        {
          label: 'Empresas que sigo',
          href: 'company/following-companies',
          childs: [],
        },
        {
          label: 'Empresas donde trabajo',
          href: 'company/i-work-with',
          childs: [],
        },
        {
          label: 'Solicitudes enviadas',
          href: 'company/requests-sent',
          childs: [],
        },
        {
          label: 'Invitaciones recibidas',
          href: 'company/invitations-received',
          childs: [],
        },
      ],
    },
    {
      label: 'Grupos',
      href: 'group',
      img: '/images/navbar/grupos.svg',
      ajax: 0,
      childs: [
        {
          label: 'Mis Grupos',
          href: 'group/my-groups',
          childs: [],
        },
        {
          label: 'Grupos unidos',
          href: 'group/joined-groups',
          childs: [],
        },
        {
          label: 'Solicitudes enviadas',
          href: 'group/requests-sent',
          childs: [],
        },
        {
          label: 'Invitaciones recibidas',
          href: 'group/invitations-received',
          childs: [],
        },
      ],
    },
    {
      label: 'Tienda',
      href: 'marketplace',
      img: '/images/navbar/market-place.svg',
      ajax: 0,
      childs: [],
    },
    {
      label: 'Calendario',
      href: 'calendar',
      img: '/images/navbar/calendar.svg',
      ajax: 0,
      childs: [],
    },
  ],
  isChatPage: false,
  routeCheckSession: '/check-session',
  linkAdmin: false,
  linkImpersonate: true,
  image:
    '/storage/type/user/code/e85129fa-18eb-4149-8640-fea9ae916cdc/filename/user-profile-63d3c94c2b1a4.png/',
  fullName: 'Santiago Olivera',
  country: 'México',
  visits: '64',
  connections: 6,
  logoForNavbar: 'https://dev.leaderslinked.com/storage-network/type/navbar',
  defaultNetwork: 'y',
  linkKnowledgeArea: true,
  routeKnowledgeArea: '/knowledge-area',
  linkMyCoach: true,
  routeMyCoach: '/my-coach',
}

const Header = ({ theme = 'default' }) => {
  const [backendVars, setBackendVars] = useState(null)
  const [notificationsCount, setNotificationsCount] = useState(0)
  const [messagesCount, setMessagesCount] = useState(0)
  const [userImage, setUserImage] = useState('')
  const [loading, setLoading] = useState(false)

  const checkUserImage = () => {
    const session_image = sessionStorage.getItem('user_session_image')

    if (!session_image) {
      return
    }

    setUserImage(session_image)
    sessionStorage.removeItem('user_session_image')
  }

  const checkSession = async () => {
    try {
      setLoading(true)
      const { data: response } = await axios.get(backendVars?.routeCheckSession)
      const { total_messages, total_notifications } = response.data

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

  useEffect(() => {
    getBackendVars('/helpers/menu')
      .then((data) => {
        const knowledgeRoutes = {
          label: 'Conocimiento',
          href: '/knowledge',
          img: '',
          childs: [],
          ajax: 0,
        }

        const comunicationRoutes = {
          label: 'Comunicación',
          href: '/comunication',
          img: '',
          childs: [
            { label: 'Calendario', href: '/calendar' },
            { label: 'Inmail', href: '/inmail', count: messagesCount },
            { label: 'Chat', href: '/chat' },
            {
              label: 'Notificaciones',
              href: '/notifications',
              count: notificationsCount,
            },
          ],
          ajax: 0,
        }

        const menuItems = [...data.menu, knowledgeRoutes, comunicationRoutes]

        if (results.linkKnowledgeArea) {
          knowledgeRoutes.childs.push({
            label: 'Área de conocimiento',
            href: results.routeKnowledgeArea,
          })
        }

        if (results.linkMyCoach) {
          knowledgeRoutes.childs.push({ label: 'Mi Coach', href: '/my-coach' })
        }

        setUserImage(data.image)
        setBackendVars({ ...data, menu: menuItems })
      })
      .catch((err) => {
        console.log(err)
        throw new Error(err)
      })
  }, [])

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

  switch (theme) {
    case 'default':
      return (
        <DefaultNavbar
          menu={backendVars?.menu}
          image={userImage}
          logo={backendVars?.logoForNavbar}
          name={backendVars?.fullName}
          linkAdmin={backendVars?.linkAdmin}
          linkImpersonate={backendVars?.linkImpersonate}
        />
      )
    default:
      return <DefaultNavbar />
  }
}

export default Header