/
HomeHeader.jsx
311 lines (298 loc) · 14 KB
/
HomeHeader.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import React from 'react';
import alertifyjs from 'alertifyjs';
import {
Home as HomeIcon,
FileCopy as CopyIcon,
Edit as EditIcon,
Delete as DeleteIcon
} from '@mui/icons-material';
import { Tooltip, ButtonGroup, Button } from '@mui/material';
import { isEmpty, get, has, merge, isBoolean, map, includes } from 'lodash';
import { toFullAPIURL, copyURL, currentUserHasAccess, getCurrentUser, isAdminUser } from '../../common/utils';
import { HEADER_GRAY } from '../../common/constants';
import APIService from '../../services/APIService';
import OwnerButton from '../common/OwnerButton';
import LastUpdatedOnLabel from '../common/LastUpdatedOnLabel';
import ExternalIdLabel from '../common/ExternalIdLabel';
import LocationLabel from '../common/LocationLabel';
import LinkLabel from '../common/LinkLabel';
import CustomAttributesPopup from '../common/CustomAttributesPopup';
import AccessChip from '../common/AccessChip';
import HeaderAttribute from '../common/HeaderAttribute';
import HeaderLogo from '../common/HeaderLogo';
import CommonFormDrawer from '../common/CommonFormDrawer';
import DownloadButton from '../common/DownloadButton';
import OrgForm from './OrgForm';
import HomeTabs from './HomeTabs';
import { ORG_DEFAULT_CONFIG } from "../../common/defaultConfigs"
import ConceptContainerDelete from '../common/ConceptContainerDelete';
const DEFAULT_VISIBLE_ATTRIBUTES = ORG_DEFAULT_CONFIG.config.header.attributes
const HomeHeader = ({
org, url, fhir, extraComponents, config, ...rest
}) => {
const downloadFileName = `Org-${get(org, 'id')}`;
const [openHeader, setOpenHeader] = React.useState(!get(config, 'config.shrinkHeader', false));
const [logoURL, setLogoURL] = React.useState(org.logo_url)
const [orgForm, setOrgForm] = React.useState(false);
const [deleteDialog, setDeleteDialog] = React.useState(false);
const hasAccess = currentUserHasAccess();
const currentUser = getCurrentUser()
const onIconClick = () => copyURL(toFullAPIURL(url));
React.useEffect(
() => setOpenHeader(!get(config, 'config.shrinkHeader', false)),
[get(config, 'config.shrinkHeader')]
)
const onLogoUpload = (base64, name) => {
APIService.new().overrideURL(url).appendToUrl('logo/')
.post({base64: base64, name: name})
.then(response => {
if(get(response, 'status') === 200)
setLogoURL(get(response, 'data.logo_url', logoURL))
})
}
const tab = rest.tab
const selectedTabConfig = config.config.tabs[tab];
const isExpandedHeader = tab === 0 || includes(['text', 'about'], get(selectedTabConfig, 'type'));
const showLogo = has(config, 'config.header.logo') ?
config.config.header.logo : true;
const showControls = !isExpandedHeader || has(config, 'config.header.controls') ? config.config.header.controls : true;
const showAttributes = has(config, 'config.header.attributes') ? config.config.header.attributes : true
const showSignatures = has(config, 'config.header.signatures') ? config.config.header.signatures : true;
const customTitle = get(config, 'config.header.foreground.title')
const customDescription = get(config, 'config.header.foreground.description')
const foregroundTextColor = isExpandedHeader ? get(config, 'config.header.foreground.color') : get(config, 'config.header.shrunkHeader.foreground.color')
const customTitleColor = isExpandedHeader ? (get(config, 'config.header.foreground.titleColor') || foregroundTextColor) : get(config, 'config.header.shrunkHeader.foreground.color')
const customDescriptionColor = isExpandedHeader && (get(config, 'config.header.foreground.descriptionColor') || foregroundTextColor)
const hasBackgroundImage = Boolean(get(config, 'config.header.background.image'))
const getBackgroundStyles = () => {
const defaultStyles = {backgroundColor: HEADER_GRAY}
let styles = {...defaultStyles}
const headerBackgroundStyles = get(config, 'config.header.background')
const backgroundColor = get(headerBackgroundStyles, 'backgroundColor')
const image = get(headerBackgroundStyles, 'image')
const shrunkHeaderBackgroundStyles = get(config, 'config.header.shrunkHeader.background')
if(isExpandedHeader) {
if(image) {
styles['backgroundImage'] = `url(${image})`;
styles['backgroundSize'] = 'cover';
styles['backgroundAttachment'] = 'fixed';
} else if(backgroundColor) {
styles['backgroundColor'] = backgroundColor
}
} else {
if(get(shrunkHeaderBackgroundStyles, 'image')) {
styles['backgroundImage'] = `url(${shrunkHeaderBackgroundStyles['image']})`;
styles['backgroundSize'] = 'cover';
styles['backgroundAttachment'] = 'fixed';
} else if (get(shrunkHeaderBackgroundStyles, 'backgroundColor'))
styles['backgroundColor'] = shrunkHeaderBackgroundStyles['backgroundColor']
}
return styles
}
const getTitleStyles = () => customTitleColor ? {color: customTitleColor} : {}
const getDescriptionStyles = () => {
const style = {
alignItems: 'start',
width: get(config, 'config.header.foreground.descriptionWidth') || '40%'
}
if(customDescriptionColor)
style['color'] = customDescriptionColor
return style;
}
const getVisibleAttributes = () => {
const attributes = get(config, 'config.header.attributes')
if(attributes)
return isBoolean(attributes) ? DEFAULT_VISIBLE_ATTRIBUTES : attributes
}
const shouldShowOverlay = Boolean(isExpandedHeader && get(config, 'config.header.background.imageOverlay') && hasBackgroundImage);
const canDelete = org.id !== 'OCL' && (isAdminUser() || (get(org, 'created_by') === get(currentUser, 'username')))
const onDelete = () => {
APIService.orgs(org.id).delete().then(response => {
if(response.status === 202)
alertifyjs.success('Organization delete is queued, this may take few minutes.', () => window.location.reload())
else if(response.status === 204)
alertifyjs.success('Organization is deleted', 3, () => window.location.reload())
else if(response.status === 409)
alertifyjs.warning('Organization delete is already in queue', 0)
else
alertifyjs.error('Something bad might have happened!')
})
}
return (
<header className='home-header col-xs-12' style={merge({marginBottom: tab === 0 ? 0 : '5px', padding: 0}, getBackgroundStyles())}>
<div className='col-xs-12 no-side-padding' style={shouldShowOverlay ? {paddingBottom: '2px', backgroundColor: 'rgba(0,0,0,0.6)'} : {}}>
<div className='col-xs-12 no-side-padding container' style={{paddingTop: '10px'}}>
{
showLogo &&
<div className='no-side-padding col-xs-1 home-icon'>
<HeaderLogo
logoURL={logoURL}
onUpload={onLogoUpload}
defaultIcon={<HomeIcon className='default-svg' />}
className={(!openHeader || !isExpandedHeader) ? 'xsmall' : ''}
/>
</div>
}
<div className='col-xs-11' style={isExpandedHeader ? {minHeight: get(config, 'config.header.height') || '140px'} : {}}>
{
(showControls || !isExpandedHeader) &&
<div className='col-xs-12 no-side-padding flex-vertical-center'>
<OwnerButton owner={org.id} ownerType='Organization' href={url} />
{
!fhir &&
<span style={{marginLeft: '15px'}}>
<ButtonGroup variant='text' size='large'>
<Tooltip arrow title="Copy URL">
<Button onClick={onIconClick} color='secondary'>
<CopyIcon fontSize="inherit" style={customTitleColor ? {color: customTitleColor} : {}} />
</Button>
</Tooltip>
{
hasAccess &&
<Tooltip arrow title='Edit Organization'>
<Button onClick={() => setOrgForm(true)} color='secondary'>
<EditIcon fontSize='inherit' style={customTitleColor ? {color: customTitleColor} : {}} />
</Button>
</Tooltip>
}
<DownloadButton resource={org} filename={downloadFileName} includeCSV iconStyle={customTitleColor ? {color: customTitleColor} : {}} />
{
canDelete &&
<Tooltip arrow title='Delete Organization'>
<Button onClick={() => setDeleteDialog(true)} color='secondary'>
<DeleteIcon fontSize='inherit' style={customTitleColor ? {color: customTitleColor} : {}} />
</Button>
</Tooltip>
}
</ButtonGroup>
</span>
}
</div>
}
{
isExpandedHeader &&
<div className='col-xs-12 no-side-padding flex-vertical-center home-resource-full-name large' style={{paddingTop: (showControls || !isExpandedHeader) ? '10px' : '0'}}>
<span style={merge({marginRight: '10px'}, getTitleStyles())}>
{
customTitle ? (<h3 style={{margin: 0}}>{customTitle}</h3>) : org.name
}
</span>
{
!fhir && showAttributes &&
<AccessChip publicAccess={org.public_access} />
}
</div>
}
{
(customDescription && isExpandedHeader) ?
<div className='col-xs-12 no-side-padding header-custom-html resource-description large' dangerouslySetInnerHTML={{__html: customDescription}} style={getDescriptionStyles()} /> : (
org.description && isExpandedHeader &&
<div className='col-xs-12 no-side-padding flex-vertical-center resource-description large' style={getDescriptionStyles()}>
{org.description}
</div>
)
}
{
showAttributes && isExpandedHeader &&
<React.Fragment>
{
map(
getVisibleAttributes(),
(attr, index) => <HeaderAttribute
key={index}
label={attr.label}
value={org[attr.value]}
type={attr.type}
gridClass="col-xs-12"
color={customDescriptionColor}
/>
)
}
<HeaderAttribute label="Custom Attributes" value={!isEmpty(org.extras) && <CustomAttributesPopup attributes={org.extras} color={customDescriptionColor} />} gridClass="col-xs-12" color={customDescriptionColor} />
</React.Fragment>
}
{
showSignatures && isExpandedHeader &&
<div className='col-xs-12 no-side-padding flex-vertical-center' style={{paddingTop: '10px'}}>
{
org.location &&
<span style={{marginRight: '10px'}}>
<LocationLabel location={org.location} noContainerClass iconSize="medium" containerStyle={customDescriptionColor ? {color: customDescriptionColor} : {}} />
</span>
}
{
org.website &&
<span style={{marginRight: '10px'}}>
<LinkLabel link={org.website} iconSize='medium' noContainerClass containerStyle={customDescriptionColor ? {color: customDescriptionColor} : {}} />
</span>
}
{
org.created_on &&
<span>
<LastUpdatedOnLabel
label='Created'
date={org.created_on}
by={org.created_by}
iconSize='medium'
noContainerClass
containerStyle={customDescriptionColor ? {color: customDescriptionColor} : {}}
/>
</span>
}
{
org.updated_on &&
<span style={{marginLeft: '10px'}}>
<LastUpdatedOnLabel
date={org.updated_on}
by={org.updated_by}
iconSize='medium'
noContainerClass
containerStyle={customDescriptionColor ? {color: customDescriptionColor} : {}}
/>
</span>
}
{
org.external_id &&
<span style={{marginLeft: '10px', marginTop: '-8px'}}>
<ExternalIdLabel
externalId={org.external_id}
iconSize='medium'
containerStyle={customDescriptionColor ? {color: customDescriptionColor} : {}}
/>
</span>
}
</div>
}
{extraComponents}
</div>
<HomeTabs
org={org}
url={url}
selectedConfig={config}
tabColor={foregroundTextColor}
{...rest}
/>
</div>
<CommonFormDrawer
style={{zIndex: 1202}}
isOpen={orgForm}
onClose={() => setOrgForm(false)}
formComponent={
<OrgForm edit reloadOnSuccess onCancel={() => setOrgForm(false)} org={org} />
}
/>
</div>
{
canDelete && deleteDialog &&
<ConceptContainerDelete
open={deleteDialog}
resource={org}
onClose={() => setDeleteDialog(false)}
onDelete={onDelete}
associatedResources={['sources', 'collections', 'its content']}
/>
}
</header>
)
}
export default HomeHeader;