Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7371 | Rev 7380 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7088 stevensc 1
import React, { useEffect, useRef, useState } from 'react'
6753 stevensc 2
import { axios } from '../../../utils'
6632 stevensc 3
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown'
4
 
6753 stevensc 5
import useOutsideClick from '../../../hooks/useOutsideClick'
7088 stevensc 6
import { Avatar } from '@mui/material'
7331 stevensc 7
import { Link } from 'react-router-dom'
8
import { logout } from '../../../redux/auth/auth.actions'
9
import { useDispatch } from 'react-redux'
10
import { addNotification } from '../../../redux/notification/notification.actions'
6753 stevensc 11
 
6632 stevensc 12
const UserOptions = ({
13
  image = '',
14
  name = '',
15
  adminUrl = '',
16
  impersonateUrl = '',
17
  defaultNetwork = 'y',
18
  knowledgeAuth,
7331 stevensc 19
  routeAdmin = '',
20
  routeImpersonate = '',
6632 stevensc 21
}) => {
7088 stevensc 22
  const [userImage, setUserImage] = useState(
23
    '/storage/type/user/code/f3df3718-37d9-4e4d-bd84-71f7924a858d/'
24
  )
6632 stevensc 25
  const [displayOptions, setDisplayOptions] = useState(false)
26
  const userDropdownContainer = useRef(null)
7331 stevensc 27
  const dispatch = useDispatch()
7088 stevensc 28
 
6753 stevensc 29
  useOutsideClick(userDropdownContainer, () => setDisplayOptions(false))
6632 stevensc 30
 
7088 stevensc 31
  const checkUserImage = () => {
32
    const session_image = sessionStorage.getItem('user_session_image')
33
 
34
    if (!session_image) {
35
      return
36
    }
37
 
38
    setUserImage(session_image)
39
    sessionStorage.removeItem('user_session_image')
40
  }
41
 
6632 stevensc 42
  const handleDisplay = async (e) => {
43
    e.preventDefault()
44
    setDisplayOptions(!displayOptions)
45
  }
46
 
47
  const getAdminUrl = async (e) => {
48
    e.preventDefault()
49
    try {
7331 stevensc 50
      const { data } = await axios.get(routeAdmin)
6632 stevensc 51
      if (data.success) return window.open(data.data)
52
    } catch (error) {
53
      console.log('>>: error > ', error)
54
    }
55
  }
56
 
7331 stevensc 57
  const handleLogout = (e) => {
58
    e.preventDefault()
7332 stevensc 59
    axios
7333 stevensc 60
      .get('/signout')
7331 stevensc 61
      .then((response) => {
7368 stevensc 62
        const { success } = response.data
63
        if (!success) {
64
          dispatch(
65
            addNotification({
66
              style: 'danger',
67
              msg: 'Error interno. Intente más tarde.',
68
            })
69
          )
7331 stevensc 70
        }
7369 stevensc 71
 
7368 stevensc 72
        dispatch(logout())
7331 stevensc 73
      })
7334 stevensc 74
      .catch((err) => {
7331 stevensc 75
        dispatch(
76
          addNotification({
77
            style: 'danger',
78
            message: 'Error interno. Intente más tarde.',
79
          })
80
        )
7334 stevensc 81
        throw new Error(err)
7331 stevensc 82
      })
83
  }
84
 
7088 stevensc 85
  useEffect(() => {
86
    const timer = setInterval(() => {
87
      checkUserImage()
88
    }, 2000)
89
 
90
    return () => {
91
      clearInterval(timer)
92
    }
93
  }, [])
94
 
95
  useEffect(() => {
96
    setUserImage(image)
97
  }, [image])
98
 
6632 stevensc 99
  return (
100
    <li>
101
      <a href="#" className="header__option mobile" onClick={handleDisplay}>
7101 stevensc 102
        <Avatar src={userImage} alt={`${name} profile image`} />
6632 stevensc 103
        <span>
104
          Me <ArrowDropDownIcon className="user__option-icon" />
105
        </span>
106
      </a>
107
      <div
108
        className={`user__options-dropdown ${
109
          displayOptions ? 'fadeIn' : 'fadeOut'
110
        }`}
111
        ref={userDropdownContainer}
112
      >
113
        <div className="user__options-description">
114
          <div className="user__options-info">
7101 stevensc 115
            <Avatar src={userImage} alt={`${name} profile image`} />
6632 stevensc 116
            <h3>{name}</h3>
117
          </div>
118
          {/* <a href='#' className='btn__profile'>
119
                        View profile
120
                    </a> */}
121
        </div>
122
        {(adminUrl || impersonateUrl || knowledgeAuth) && (
123
          <div className="user__options-item">
124
            <h3>Admin</h3>
125
            <ul>
126
              {adminUrl && (
127
                <li>
7331 stevensc 128
                  <Link to="/admin" onClick={getAdminUrl}>
6632 stevensc 129
                    Administración
7331 stevensc 130
                  </Link>
6632 stevensc 131
                </li>
132
              )}
133
              {impersonateUrl && (
134
                <li>
7379 stevensc 135
                  <Link to={routeImpersonate}>Personificar otro usuario</Link>
6632 stevensc 136
                </li>
137
              )}
138
            </ul>
139
          </div>
140
        )}
141
        <div className="user__options-item">
142
          <h3>Cuenta</h3>
143
          <ul>
144
            <li>
7379 stevensc 145
              <Link to="/account-settings">Configuración de la cuenta</Link>
6632 stevensc 146
            </li>
147
            {defaultNetwork === 'y' && (
148
              <>
149
                <li>
7379 stevensc 150
                  <Link to="/privacy-policy">Política de privacidad</Link>
6632 stevensc 151
                </li>
152
                <li>
7379 stevensc 153
                  <Link to="/cookies">Política de cookies</Link>
6632 stevensc 154
                </li>
155
              </>
156
            )}
157
          </ul>
158
        </div>
159
        <div className="user__options-item">
160
          <ul>
161
            <li className="logOutContainer">
7331 stevensc 162
              <Link
163
                className="logOutContainer__a"
164
                to="/signout"
165
                onClick={handleLogout}
166
              >
6632 stevensc 167
                Cerrar sesión
7331 stevensc 168
              </Link>
6632 stevensc 169
            </li>
170
          </ul>
171
        </div>
172
      </div>
173
    </li>
174
  )
175
}
176
 
177
export default UserOptions