/
UserHomeDetails.jsx
234 lines (227 loc) · 9.07 KB
/
UserHomeDetails.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Divider, CircularProgress, IconButton, Tooltip, Chip } from '@mui/material';
import {
Person as PersonIcon,
FileCopy as CopyIcon,
Edit as EditIcon,
Business as CompanyIcon,
LocationOn as LocationIcon,
Link as WebsiteIcon,
MailOutline as MailIcon,
EventAvailable as DateJoinedIcon,
AdminPanelSettings as AdminIcon,
PersonOff as UserDisabledIcon,
PrivacyTip as UnverifiedIcon,
VerifiedUser as VerifiedIcon,
SafetyCheck as VerificationPendingIcon,
} from '@mui/icons-material';
import { includes, startCase, get, merge, isEmpty } from 'lodash';
import APIService from '../../services/APIService';
import {
formatDate, formatWebsiteLink, copyToClipboard, getCurrentUserUsername, isAdminUser, isSuperuser, isSSOEnabled
} from '../../common/utils';
import HeaderLogo from '../common/HeaderLogo';
import CommonFormDrawer from '../common/CommonFormDrawer';
import UserForm from './UserForm';
import UserManagement from './UserManagement';
const UserHomeDetails = ({ user, isLoading, apiToken }) => {
const { t } = useTranslation()
const [logoURL, setLogoURL] = React.useState(user.logo_url)
const [uploading, setUploading] = React.useState(false);
const [editForm, setEditForm] = React.useState(false);
const onEditClick = () => setEditForm(true);
const onEditClose = () => setEditForm(false);
React.useEffect(() => {
if(user.logo_url)
setLogoURL(user.logo_url)
}, [user.logo_url])
let name = user.name || '';
if(includes(['none none', '- -'], name.trim()))
name = '';
const onLogoUpload = (base64, name) => {
setUploading(true)
APIService.new().overrideURL(user.url).appendToUrl('logo/')
.post({base64: base64, name: name})
.then(response => {
if(get(response, 'status') === 200){
const url = get(response, 'data.logo_url', logoURL)
setLogoURL(url)
localStorage.setItem(
'user',
JSON.stringify(merge(JSON.parse(localStorage.user), {logo_url: url}))
)
}
setUploading(false)
})
}
const currentUserUsername = getCurrentUserUsername()
const isSameAsCurrentUser = user.username === currentUserUsername;
const isSelectedUserSuperuser = user.is_superuser
const isAdmin = isAdminUser()
return (
<div className="col-xs-12 no-side-padding">
{
(isLoading || uploading) ?
<CircularProgress color='primary' style={{marginTop: '50px', marginLeft: '50px'}} /> :
<div className="col-xs-12 no-side-padding">
<div className='home-icon' style={{display: 'flex'}}>
<HeaderLogo
isCircle
logoURL={logoURL}
onUpload={onLogoUpload}
defaultIcon={<PersonIcon style={{width: '120px', height: '120px'}} />}
/>
</div>
<h2 style={{marginBottom: '-2px'}}>
{startCase(name)}
</h2>
<div className='user-home-username flex-vertical-center'>
<span style={{marginRight: '5px'}}>{user.username}</span>
{
isAdmin &&
<React.Fragment>
{
user.is_superuser &&
<Tooltip title={t('user.home.profile.tooltip.superuser')}>
<AdminIcon color='success' />
</Tooltip>
}
{
user.is_staff && !user.is_superuser &&
<Tooltip title={t('user.home.profile.tooltip.staff')}>
<AdminIcon color='success' />
</Tooltip>
}
{
user.status === 'deactivated' && !user.is_staff && !user.is_superuser &&
<Tooltip title={t('user.home.profile.tooltip.deactivated')}>
<UserDisabledIcon color='error' style={{width: '20px'}} />
</Tooltip>
}
{
user.status === 'unverified' && !user.is_staff && !user.is_superuser &&
<Tooltip title={t('user.home.profile.tooltip.unverified')}>
<UnverifiedIcon color='warning' style={{width: '20px'}} />
</Tooltip>
}
{
user.status === 'verified' && !user.is_staff && !user.is_superuser &&
<Tooltip title={t('user.home.profile.tooltip.verified')}>
<VerifiedIcon color='success' style={{width: '20px'}} />
</Tooltip>
}
{
user.status === 'verification_pending' && !user.is_staff && !user.is_superuser &&
<Tooltip title={t('user.home.profile.tooltip.verification_pending')}>
<VerificationPendingIcon color='warning' style={{width: '20px'}} />
</Tooltip>
}
</React.Fragment>
}
</div>
{
(apiToken || isAdmin) &&
<div>
<Chip
className='underline-text'
icon={<EditIcon fontSize='small' style={{width: '14px'}} />}
size='small'
label={t('user.home.profile.edit')}
style={{border: 'none'}}
variant='outlined'
color='primary'
onClick={onEditClick}
/>
</div>
}
<Divider style={{width: '100%', margin: '5px 0'}} />
{
user.company &&
<div style={{marginTop: '5px'}}>
<Tooltip title={t('common.company')} arrow placement='right'>
<span className='flex-vertical-center' placement='right'>
<span style={{marginRight: '5px'}}><CompanyIcon fontSize='small' style={{marginTop: '4px'}} /></span>
<span>{user.company}</span>
</span>
</Tooltip>
</div>
}
{
user.location &&
<div>
<Tooltip title={t('common.location')} arrow placement='right'>
<span className='flex-vertical-center'>
<span style={{marginRight: '5px'}}><LocationIcon fontSize='small' style={{marginTop: '4px'}} /></span>
<span>{user.location}</span>
</span>
</Tooltip>
</div>
}
{
user.website &&
<div>
<Tooltip title={t('common.website')} arrow placement='right'>
<span className='flex-vertical-center'>
<span style={{marginRight: '5px'}}><WebsiteIcon fontSize='small' style={{marginTop: '4px', transform: 'rotate(-30deg)'}} /></span>
<span>{formatWebsiteLink(user.website)}</span>
</span>
</Tooltip>
</div>
}
<div>
<Tooltip title='Email' arrow placement='right'>
<span className='flex-vertical-center'>
<span style={{marginRight: '5px'}}><MailIcon fontSize='small' style={{marginTop: '4px'}} /></span>
<span>{user.email}</span>
</span>
</Tooltip>
</div>
<div>
<Tooltip title={t('user.home.profile.date_joined')} arrow placement='right'>
<span className='flex-vertical-center'>
<span style={{marginRight: '5px'}}><DateJoinedIcon fontSize='small' style={{marginTop: '4px'}} /></span>
<span>{formatDate(user.created_on)}</span>
</span>
</Tooltip>
</div>
{
apiToken &&
<React.Fragment>
<Divider style={{width: '100%', margin: '5px 0'}} />
<p>
<strong>{t('user.home.profile.api_token')}</strong>
<Tooltip arrow title={t('user.home.profile.tooltip.copy_token')} placement='right'>
<IconButton style={{marginLeft: '10px'}} size="small" onClick={() => copyToClipboard(apiToken, 'Token copied to clipboard!')}>
<CopyIcon fontSize="small" />
</IconButton>
</Tooltip>
</p>
</React.Fragment>
}
{
isSuperuser() && !isSameAsCurrentUser && !isEmpty(user) && !isSelectedUserSuperuser && !isSSOEnabled() &&
<React.Fragment>
<Divider style={{width: '100%', margin: '5px 0'}} />
<UserManagement user={user} />
</React.Fragment>
}
</div>
}
<CommonFormDrawer
style={{zIndex: 1202}}
isOpen={editForm}
onClose={onEditClose}
formComponent={
<UserForm
loggedIn={user.username === getCurrentUserUsername()}
edit
reloadOnSuccess
onCancel={onEditClose} user={user}
/>
}
/>
</div>
)
}
export default UserHomeDetails;