| 3703 |
stevensc |
1 |
import React, { useEffect, useState } from 'react';
|
|
|
2 |
import { Link } from 'react-router-dom';
|
|
|
3 |
import { useDispatch } from 'react-redux';
|
|
|
4 |
import { Avatar, styled, Typography } from '@mui/material';
|
|
|
5 |
import ArrowDropDown from '@mui/icons-material/ArrowDropDown';
|
|
|
6 |
|
|
|
7 |
import { useOutsideClick } from '@hooks';
|
|
|
8 |
|
|
|
9 |
import { axios } from '@app/utils';
|
|
|
10 |
import { asyncLogout } from '@app/redux/auth/auth.actions';
|
|
|
11 |
import { addNotification } from '@app/redux/notification/notification.actions';
|
|
|
12 |
|
|
|
13 |
const NavItem = styled(Link)(({ theme }) => ({
|
|
|
14 |
position: 'relative',
|
|
|
15 |
display: 'flex',
|
|
|
16 |
alignItems: 'center',
|
|
|
17 |
padding: theme.spacing(1, 0),
|
|
|
18 |
height: '100%'
|
|
|
19 |
}));
|
|
|
20 |
|
|
|
21 |
const UserOptions = ({
|
|
|
22 |
image = '',
|
|
|
23 |
name = '',
|
|
|
24 |
adminUrl = '',
|
|
|
25 |
impersonateUrl = '',
|
|
|
26 |
defaultNetwork = 'y',
|
|
|
27 |
knowledgeAuth,
|
|
|
28 |
routeAdmin = '',
|
|
|
29 |
routeImpersonate = ''
|
|
|
30 |
}) => {
|
|
|
31 |
const [userImage, setUserImage] = useState(
|
|
|
32 |
'/storage/type/user/code/f3df3718-37d9-4e4d-bd84-71f7924a858d/'
|
|
|
33 |
);
|
|
|
34 |
const [displayOptions, setDisplayOptions] = useState(false);
|
|
|
35 |
const dispatch = useDispatch();
|
|
|
36 |
|
|
|
37 |
const [userDropdownContainer] = useOutsideClick(() => setDisplayOptions(false));
|
|
|
38 |
|
|
|
39 |
const checkUserImage = () => {
|
|
|
40 |
const session_image = sessionStorage.getItem('user_session_image');
|
|
|
41 |
if (!session_image) return;
|
|
|
42 |
setUserImage(session_image);
|
|
|
43 |
sessionStorage.removeItem('user_session_image');
|
|
|
44 |
};
|
|
|
45 |
|
|
|
46 |
const handleDisplay = async (e) => {
|
|
|
47 |
e.preventDefault();
|
|
|
48 |
setDisplayOptions(!displayOptions);
|
|
|
49 |
};
|
|
|
50 |
|
|
|
51 |
const getAdminUrl = async (e) => {
|
|
|
52 |
e.preventDefault();
|
|
|
53 |
try {
|
|
|
54 |
const { data } = await axios.get(routeAdmin);
|
|
|
55 |
if (data.success) return window.open(data.data);
|
|
|
56 |
} catch (error) {
|
|
|
57 |
console.log('>>: error > ', error);
|
|
|
58 |
}
|
|
|
59 |
};
|
|
|
60 |
|
|
|
61 |
const handleLogout = async (e) => {
|
|
|
62 |
e.preventDefault();
|
|
|
63 |
try {
|
|
|
64 |
await dispatch(asyncLogout());
|
|
|
65 |
dispatch(addNotification({ style: 'success', msg: 'Ha finalizado su sesión' }));
|
|
|
66 |
} catch (error) {
|
|
|
67 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
68 |
}
|
|
|
69 |
};
|
|
|
70 |
|
|
|
71 |
useEffect(() => {
|
|
|
72 |
const timer = setInterval(checkUserImage, 2000);
|
|
|
73 |
|
|
|
74 |
return () => {
|
|
|
75 |
clearInterval(timer);
|
|
|
76 |
};
|
|
|
77 |
}, []);
|
|
|
78 |
|
|
|
79 |
useEffect(() => {
|
|
|
80 |
setUserImage(image);
|
|
|
81 |
}, [image]);
|
|
|
82 |
|
|
|
83 |
return (
|
|
|
84 |
<li ref={userDropdownContainer}>
|
|
|
85 |
<NavItem to='/' onClick={handleDisplay}>
|
|
|
86 |
<Avatar src={userImage} alt={`${name} profile image`} />
|
|
|
87 |
|
|
|
88 |
<ArrowDropDown sx={{ fontSize: '1rem' }} />
|
|
|
89 |
</NavItem>
|
|
|
90 |
|
|
|
91 |
<div className={`user__options-dropdown ${displayOptions ? 'fadeIn' : 'fadeOut'}`}>
|
|
|
92 |
<div className='user__options-description'>
|
|
|
93 |
<div className='user__options-info'>
|
|
|
94 |
<Avatar src={userImage} alt={`${name} profile image`} />
|
|
|
95 |
<Typography variant='h3'>{name}</Typography>
|
|
|
96 |
</div>
|
|
|
97 |
</div>
|
|
|
98 |
{(adminUrl || impersonateUrl || knowledgeAuth) && (
|
|
|
99 |
<div className='user__options-item'>
|
|
|
100 |
<Typography variant='h3'>Admin</Typography>
|
|
|
101 |
<ul>
|
|
|
102 |
{adminUrl && (
|
|
|
103 |
<li>
|
|
|
104 |
<Link to='/admin' onClick={getAdminUrl}>
|
|
|
105 |
Administración
|
|
|
106 |
</Link>
|
|
|
107 |
</li>
|
|
|
108 |
)}
|
|
|
109 |
{impersonateUrl && (
|
|
|
110 |
<li>
|
|
|
111 |
<Link to={routeImpersonate}>Personificar otro usuario</Link>
|
|
|
112 |
</li>
|
|
|
113 |
)}
|
|
|
114 |
</ul>
|
|
|
115 |
</div>
|
|
|
116 |
)}
|
|
|
117 |
<div className='user__options-item'>
|
|
|
118 |
<Typography variant='h3'>Cuenta</Typography>
|
|
|
119 |
<ul>
|
|
|
120 |
<li>
|
|
|
121 |
<Link to='/account-settings'>Configuración de la cuenta</Link>
|
|
|
122 |
</li>
|
|
|
123 |
<li>
|
|
|
124 |
<Link to='/abuse-report'>Reportes</Link>
|
|
|
125 |
</li>
|
|
|
126 |
{defaultNetwork === 'y' && (
|
|
|
127 |
<>
|
|
|
128 |
<li>
|
|
|
129 |
<Link to='/privacy-policy'>Política de privacidad</Link>
|
|
|
130 |
</li>
|
|
|
131 |
<li>
|
|
|
132 |
<Link to='/cookies'>Política de cookies</Link>
|
|
|
133 |
</li>
|
|
|
134 |
</>
|
|
|
135 |
)}
|
|
|
136 |
</ul>
|
|
|
137 |
</div>
|
|
|
138 |
<div className='user__options-item'>
|
|
|
139 |
<ul>
|
|
|
140 |
<li className='logOutContainer'>
|
|
|
141 |
<Link className='logOutContainer__a' to='/signout' onClick={handleLogout}>
|
|
|
142 |
Cerrar sesión
|
|
|
143 |
</Link>
|
|
|
144 |
</li>
|
|
|
145 |
</ul>
|
|
|
146 |
</div>
|
|
|
147 |
</div>
|
|
|
148 |
</li>
|
|
|
149 |
);
|
|
|
150 |
};
|
|
|
151 |
|
|
|
152 |
export default UserOptions;
|