/
SourceHomeHeader.jsx
258 lines (250 loc) · 10.7 KB
/
SourceHomeHeader.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
import React from 'react';
import alertifyjs from 'alertifyjs';
import {
List as ListIcon,
FileCopy as CopyIcon,
Edit as EditIcon,
Delete as DeleteIcon,
} from '@material-ui/icons';
import { Tooltip, ButtonGroup, Button, Collapse } from '@material-ui/core';
import { isEmpty, map, filter, get } from 'lodash';
import { toFullAPIURL, copyURL, nonEmptyCount, currentUserHasAccess } from '../../common/utils';
import { GREEN } from '../../common/constants';
import APIService from '../../services/APIService';
import OwnerButton from '../common/OwnerButton';
import SourceButton from '../common/SourceButton';
import VersionButton from '../common/VersionButton';
import LastUpdatedOnLabel from '../common/LastUpdatedOnLabel';
import ExternalIdLabel from '../common/ExternalIdLabel';
import LinkLabel from '../common/LinkLabel';
import AccessChip from '../common/AccessChip';
import CustomAttributesPopup from '../common/CustomAttributesPopup';
import CollapsibleAttributes from '../common/CollapsibleAttributes';
import HeaderAttribute from '../common/HeaderAttribute';
import HeaderLogo from '../common/HeaderLogo';
import CommonFormDrawer from '../common/CommonFormDrawer';
import SupportedLocales from '../common/SupportedLocales';
import DownloadButton from '../common/DownloadButton';
import SourceForm from './SourceForm';
import ReleasedChip from '../common/ReleasedChip';
import RetiredChip from '../common/RetiredChip';
import ProcessingChip from '../common/ProcessingChip';
import ConceptContainerDelete from '../common/ConceptContainerDelete';
import CollapsibleDivider from '../common/CollapsibleDivider';
import { SOURCE_DEFAULT_CONFIG } from '../../common/defaultConfigs';
const DEFAULT_VISIBLE_ATTRIBUTES = SOURCE_DEFAULT_CONFIG.config.header.visibleAttributes
const DEFAULT_INVISIBLE_ATTRIBUTES = SOURCE_DEFAULT_CONFIG.config.header.invisibleAttributes
const SourceHomeHeader = ({
source, isVersionedObject, versionedObjectURL, currentURL, config, splitView
}) => {
const downloadFileName = isVersionedObject ? `${source.type}-${source.short_code}` : `${source.type}-${source.short_code}-${source.id}`;
const hasAccess = currentUserHasAccess();
const [openHeader, setOpenHeader] = React.useState(!get(config, 'config.header.shrink', false));
const [deleteDialog, setDeleteDialog] = React.useState(false);
const [logoURL, setLogoURL] = React.useState(source.logo_url)
const [sourceForm, setSourceForm] = React.useState(false);
const onIconClick = () => copyURL(toFullAPIURL(currentURL))
const onLogoUpload = (base64, name) => {
APIService.new().overrideURL(versionedObjectURL).appendToUrl('logo/')
.post({base64: base64, name: name})
.then(response => {
if(get(response, 'status') === 200)
setLogoURL(get(response, 'data.logo_url', logoURL))
})
}
const getDefaultHiddenAttributes = () => {
return filter(DEFAULT_VISIBLE_ATTRIBUTES, (attr) => {
return !map(get(config, 'config.header.visibleAttributes'),(attr) => attr.value).includes(attr.value)
}
)
}
const getVisibleAttributes = ()=>{
if (get(config, 'config.header.visibleAttributes') === 'object'){
return get(config, 'config.header.visibleAttributes')
}
else if (get(config, 'config.header.visibleAttributes')) {
return DEFAULT_VISIBLE_ATTRIBUTES
}
else return []
}
const getHiddenAttributes = () => {
if (get(config, 'config.header.invisibleAttributes') === 'object'){
return {...get(config, 'config.header.invisibleAttributes'), ...getDefaultHiddenAttributes()}
}
else if (get(config, 'config.header.invisibleAttributes')) {
return { DEFAULT_INVISIBLE_ATTRIBUTES, ...getDefaultHiddenAttributes() }
}
else return []
}
const hasManyHiddenAttributes = nonEmptyCount(source, map(getHiddenAttributes(),(attr) => attr.value)) >= 4;
React.useEffect(
() => setOpenHeader(!get(config, 'config.header.shrink', false)),
[get(config, 'config.header.shrink')]
)
React.useEffect(() => {
if(splitView)
setOpenHeader(false)
}, [splitView])
const deleteSource = () => {
APIService.new().overrideURL(source.url).delete().then(response => {
if(get(response, 'status') === 204)
alertifyjs.success('Source Deleted', 1, () => window.location.hash = source.owner_url)
else
alertifyjs.error('Something bad happened!')
})
}
return (
<header className='home-header col-md-12'>
<div className='col-md-12 no-side-padding container' style={{paddingTop: '10px'}}>
<div className='no-side-padding col-md-1 home-icon'>
<HeaderLogo
logoURL={logoURL}
onUpload={onLogoUpload}
defaultIcon={<ListIcon className='default-svg' />}
shrink={!openHeader}
/>
</div>
<div className='col-md-11'>
<div className='col-md-12 no-side-padding flex-vertical-center'>
<OwnerButton {...source} href={versionedObjectURL} />
<span className='separator'>/</span>
<SourceButton label={source.short_code} href={`#${versionedObjectURL}`} />
{
!isVersionedObject &&
<React.Fragment>
<span className='separator'>/</span>
<VersionButton label={source.version} href={`#${currentURL}`} bgColor={GREEN} />
</React.Fragment>
}
{
source.retired &&
<span style={{marginLeft: '10px'}}>
<RetiredChip size='small' />
</span>
}
{
source.released &&
<span style={{marginLeft: '10px'}}>
<ReleasedChip size='small' />
</span>
}
{
source.is_processing &&
<span style={{marginLeft: '10px'}}>
<ProcessingChip size='small' />
</span>
}
<span style={{marginLeft: '15px'}}>
<ButtonGroup variant='text' size='large'>
<Tooltip arrow title="Copy URL">
<Button onClick={onIconClick}>
<CopyIcon fontSize="inherit" />
</Button>
</Tooltip>
{
hasAccess && isVersionedObject &&
<Tooltip arrow title='Edit Source'>
<Button onClick={() => setSourceForm(true)}>
<EditIcon fontSize='inherit' />
</Button>
</Tooltip>
}
{
hasAccess && isVersionedObject &&
<Tooltip arrow title='Delete Source'>
<Button onClick={() => setDeleteDialog(true) }>
<DeleteIcon fontSize='inherit' />
</Button>
</Tooltip>
}
<DownloadButton resource={source} filename={downloadFileName} includeCSV />
</ButtonGroup>
</span>
</div>
<div className='col-md-12 no-side-padding flex-vertical-center home-resource-full-name'>
<span style={{marginRight: '10px'}}>
{source.full_name}
</span>
<AccessChip publicAccess={source.public_access} />
</div>
<Collapse in={openHeader} className='col-md-12 no-side-padding' style={{padding: '0px', display: `${openHeader ? 'block' : 'none'}`}}>
{
source.description &&
<div className='col-md-12 no-side-padding flex-vertical-center resource-description'>
{source.description}
</div>
}
{map(getVisibleAttributes(), (attr) => {
if (attr.value === "supported_locales" || attr.value === "default_locale"){
return <HeaderAttribute key={attr.label} label="Supported Locales" value={<SupportedLocales {...source} />} gridClass="col-md-12" type="component" />
}
return <HeaderAttribute key={attr.label} label={attr.label} value={source[attr.value]} type={attr.type} gridClass="col-md-12"/>
})}
<HeaderAttribute label="Custom Attributes" value={!isEmpty(source.extras) && <CustomAttributesPopup attributes={source.extras} />} gridClass="col-md-12" />
{
hasManyHiddenAttributes ?
<div className='col-md-12 no-side-padding'>
<CollapsibleAttributes
object={source}
hiddenAttributes={getHiddenAttributes()}
/>
</div> :
<React.Fragment>
{
map(getHiddenAttributes(), (attr) => (
<HeaderAttribute key={attr.label} label={attr.label} value={get(source, attr.value)} gridClass="col-md-12" type={attr.type} />
))
}
</React.Fragment>
}
<div className='col-md-12 no-side-padding flex-vertical-center' style={{paddingTop: '10px'}}>
{
source.website &&
<span style={{marginRight: '10px'}}>
<LinkLabel link={source.website} iconSize='medium' noContainerClass />
</span>
}
<span>
<LastUpdatedOnLabel
date={source.updated_on}
by={source.updated_by}
iconSize='medium'
noContainerClass
/>
</span>
<span style={{marginLeft: '10px'}}>
<LastUpdatedOnLabel
label='Created'
date={source.created_on}
by={source.created_by}
iconSize='medium'
noContainerClass
/>
</span>
{
source.external_id &&
<span style={{marginLeft: '10px', marginTop: '-8px'}}>
<ExternalIdLabel externalId={source.external_id} iconSize='medium' />
</span>
}
</div>
</Collapse>
</div>
<CollapsibleDivider open={openHeader} onClick={() => setOpenHeader(!openHeader)} light />
</div>
<CommonFormDrawer
isOpen={sourceForm}
onClose={() => setSourceForm(false)}
formComponent={
isVersionedObject &&
<SourceForm edit reloadOnSuccess onCancel={() => setSourceForm(false)} source={source} parentURL={versionedObjectURL} />
}
/>
{
isVersionedObject && hasAccess && source &&
<ConceptContainerDelete open={deleteDialog} resource={source} onClose={() => setDeleteDialog(false)} onDelete={() => deleteSource() } />
}
</header>
)
}
export default SourceHomeHeader;