-
Notifications
You must be signed in to change notification settings - Fork 12
/
SourceHomeHeader.jsx
200 lines (190 loc) · 8.71 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
import React from 'react';
import alertifyjs from 'alertifyjs';
import {
List as ListIcon,
} from '@mui/icons-material';
import { Collapse } from '@mui/material';
import { isEmpty, map, filter, get, reject } from 'lodash';
import { nonEmptyCount, currentUserHasAccess } from '../../common/utils';
import { WHITE } from '../../common/constants';
import APIService from '../../services/APIService';
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 SourceForm from './SourceForm';
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, config, shrink
}) => {
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 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 = () => {
const visibleAttributes = getVisibleAttributes()
let attributes = []
if (get(config, 'config.header.invisibleAttributes') === 'object')
attributes = {...get(config, 'config.header.invisibleAttributes'), ...getDefaultHiddenAttributes()}
else if (get(config, 'config.header.invisibleAttributes'))
attributes = { ...DEFAULT_INVISIBLE_ATTRIBUTES, ...getDefaultHiddenAttributes() }
const visibleAttributeKeys = visibleAttributes.map(attr => attr.value)
return reject(attributes, attr => visibleAttributeKeys.includes(attr.value))
}
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(
() => setOpenHeader(!shrink),
[shrink]
)
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 if(get(response, 'status') === 202)
alertifyjs.success('Source Delete Accepted. This may take few minutes.')
else if(get(response, 'status') === 400)
alertifyjs.error(get(response, 'data.detail', 'Source Delete Failed'))
else
alertifyjs.error('Something bad happened!')
})
}
return (
<header className='home-header col-xs-12 no-side-padding' style={{backgroundColor: WHITE}}>
<div className='col-xs-12 no-side-padding container'>
<div className='col-xs-12 no-side-padding' style={openHeader ? {} : {display: 'flex', alignItems: 'center'}}>
<div className='col-xs-1 home-icon' style={{textAlign: 'left', paddingRight: '0px'}}>
<HeaderLogo
logoURL={logoURL}
onUpload={onLogoUpload}
defaultIcon={<ListIcon className='default-svg' />}
/>
</div>
<div className='col-xs-11'>
<div className='col-xs-12 no-side-padding home-resource-full-name' style={{paddingTop: '0px', display: 'block'}}>
<span style={{marginRight: '5px'}}>
{source.full_name}
</span>
<AccessChip publicAccess={source.public_access} />
</div>
<Collapse in={openHeader} className='col-xs-12 no-side-padding' style={{padding: '0px', display: `${openHeader ? 'block' : 'none'}`}}>
{
source.description &&
<div className='col-xs-12 no-side-padding flex-vertical-center resource-description'>
{source.description}
</div>
}
{
map(getVisibleAttributes(), (attr, index) => {
if (attr.value === "supported_locales" || attr.value === "default_locale")
return <HeaderAttribute key={attr.label + index} label="Supported Locales" value={<SupportedLocales {...source} />} gridClass="col-xs-12" type="component" />;
return <HeaderAttribute key={attr.label + index} label={attr.label} value={source[attr.value]} type={attr.type} gridClass="col-xs-12"/>;
})
}
<HeaderAttribute label="Custom Attributes" value={!isEmpty(source.extras) && <CustomAttributesPopup attributes={source.extras} />} gridClass="col-xs-12" />
{
hasManyHiddenAttributes ?
<div className='col-xs-12 no-side-padding'>
<CollapsibleAttributes
object={source}
hiddenAttributes={getHiddenAttributes()}
/>
</div> :
<React.Fragment>
{
map(getHiddenAttributes(), (attr, index) => (
<HeaderAttribute key={attr.label + index} label={attr.label} value={get(source, attr.value)} gridClass="col-xs-12" type={attr.type} />
))
}
</React.Fragment>
}
<div className='col-xs-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>
</div>
<CollapsibleDivider open={openHeader} onClick={() => setOpenHeader(!openHeader)} light />
</div>
<CommonFormDrawer
style={{zIndex: 1202}}
isOpen={sourceForm}
onClose={() => setSourceForm(false)}
formComponent={
isVersionedObject &&
<SourceForm edit reloadOnSuccess onCancel={() => setSourceForm(false)} source={source} parentURL={versionedObjectURL} />
}
/>
{
isVersionedObject && hasAccess && !isEmpty(source) &&
<ConceptContainerDelete open={deleteDialog} resource={source} onClose={() => setDeleteDialog(false)} onDelete={() => deleteSource() } />
}
</header>
)
}
export default SourceHomeHeader;