Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7379 | 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'
7380 stevensc 7
import { Link, useHistory } from 'react-router-dom'
7331 stevensc 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()
7380 stevensc 28
  const history = useHistory()
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) => {
7380 stevensc 62
        const { success, data } = response.data
7368 stevensc 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
 
7380 stevensc 72
        if (data.url) {
73
          history.push(data.url)
74
          dispatch(
75
            addNotification({
76
              style: 'success',
77
              msg: data.message,
78
            })
79
          )
80
          return
81
        }
82
 
7368 stevensc 83
        dispatch(logout())
7331 stevensc 84
      })
7334 stevensc 85
      .catch((err) => {
7331 stevensc 86
        dispatch(
87
          addNotification({
88
            style: 'danger',
89
            message: 'Error interno. Intente más tarde.',
90
          })
91
        )
7334 stevensc 92
        throw new Error(err)
7331 stevensc 93
      })
94
  }
95
 
7088 stevensc 96
  useEffect(() => {
97
    const timer = setInterval(() => {
98
      checkUserImage()
99
    }, 2000)
100
 
101
    return () => {
102
      clearInterval(timer)
103
    }
104
  }, [])
105
 
106
  useEffect(() => {
107
    setUserImage(image)
108
  }, [image])
109
 
6632 stevensc 110
  return (
111
    <li>
112
      <a href="#" className="header__option mobile" onClick={handleDisplay}>
7101 stevensc 113
        <Avatar src={userImage} alt={`${name} profile image`} />
6632 stevensc 114
        <span>
115
          Me <ArrowDropDownIcon className="user__option-icon" />
116
        </span>
117
      </a>
118
      <div
119
        className={`user__options-dropdown ${
120
          displayOptions ? 'fadeIn' : 'fadeOut'
121
        }`}
122
        ref={userDropdownContainer}
123
      >
124
        <div className="user__options-description">
125
          <div className="user__options-info">
7101 stevensc 126
            <Avatar src={userImage} alt={`${name} profile image`} />
6632 stevensc 127
            <h3>{name}</h3>
128
          </div>
129
          {/* <a href='#' className='btn__profile'>
130
                        View profile
131
                    </a> */}
132
        </div>
133
        {(adminUrl || impersonateUrl || knowledgeAuth) && (
134
          <div className="user__options-item">
135
            <h3>Admin</h3>
136
            <ul>
137
              {adminUrl && (
138
                <li>
7331 stevensc 139
                  <Link to="/admin" onClick={getAdminUrl}>
6632 stevensc 140
                    Administración
7331 stevensc 141
                  </Link>
6632 stevensc 142
                </li>
143
              )}
144
              {impersonateUrl && (
145
                <li>
7379 stevensc 146
                  <Link to={routeImpersonate}>Personificar otro usuario</Link>
6632 stevensc 147
                </li>
148
              )}
149
            </ul>
150
          </div>
151
        )}
152
        <div className="user__options-item">
153
          <h3>Cuenta</h3>
154
          <ul>
155
            <li>
7379 stevensc 156
              <Link to="/account-settings">Configuración de la cuenta</Link>
6632 stevensc 157
            </li>
158
            {defaultNetwork === 'y' && (
159
              <>
160
                <li>
7379 stevensc 161
                  <Link to="/privacy-policy">Política de privacidad</Link>
6632 stevensc 162
                </li>
163
                <li>
7379 stevensc 164
                  <Link to="/cookies">Política de cookies</Link>
6632 stevensc 165
                </li>
166
              </>
167
            )}
168
          </ul>
169
        </div>
170
        <div className="user__options-item">
171
          <ul>
172
            <li className="logOutContainer">
7331 stevensc 173
              <Link
174
                className="logOutContainer__a"
175
                to="/signout"
176
                onClick={handleLogout}
177
              >
6632 stevensc 178
                Cerrar sesión
7331 stevensc 179
              </Link>
6632 stevensc 180
            </li>
181
          </ul>
182
        </div>
183
      </div>
184
    </li>
185
  )
186
}
187
 
188
export default UserOptions