-
Notifications
You must be signed in to change notification settings - Fork 13
/
ConceptsComparison.jsx
362 lines (333 loc) · 13.8 KB
/
ConceptsComparison.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import React from 'react';
import { Link } from 'react-router-dom';
import {
TableContainer, Table, TableHead, TableBody, TableCell, TableRow,
Tooltip, CircularProgress
} from '@material-ui/core';
import { Flag as FlagIcon } from '@material-ui/icons';
import {
get, startCase, map, isEmpty, includes, isEqual, size, filter, reject, isObject, keys, values,
sortBy, findIndex, uniqBy
} from 'lodash';
import APIService from '../../services/APIService';
import { formatDate, toObjectArray } from '../../common/utils';
import ReactDiffViewer from 'react-diff-viewer';
const ATTRIBUTES = {
text1: ['datatype', 'display_locale', 'external_id'],
textFormatted: ['owner'],
list: ['names', 'descriptions', 'extras', 'mappings'],
bool: ['retired'],
text2: ['created_by', 'updated_by'],
date: ['created_on', 'updated_on'],
}
const getLocaleLabelFormatted = locale => {
if(!locale)
return ''
const nameAttr = get(locale, 'name') ? 'name' : 'description';
const typeValue = get(locale, 'name_type') || get(locale, 'description_type');
return (
<React.Fragment key={locale.uuid}>
<div className='flex-vertical-center'>
{
(typeValue && typeValue.toLowerCase() !== 'description') &&
<span className='gray-italics' style={{marginRight: '5px'}}>
{typeValue}
</span>
}
<span>
{get(locale, 'name') || get(locale, 'description')}
</span>
<span className='gray-italics-small' style={{marginLeft: '5px'}}>
{`[${locale.locale}]`}
</span>
{
locale.locale_preferred &&
<span style={{marginLeft: '5px'}}>
<Tooltip title={`Preferred ${nameAttr} for this locale`} placement='top-start'>
<FlagIcon color='secondary' fontSize='small' style={{width: '18px', marginTop: '4px'}}/>
</Tooltip>
</span>
}
</div>
</React.Fragment>
)
}
const getLocaleLabel = locale => {
if(!locale)
return '';
let typeValue = get(locale, 'name_type') || get(locale, 'description_type') || '';
if(typeValue)
typeValue += ' | '
const nameValue = get(locale, 'name') || get(locale, 'description');
const preferredText = locale.locale_preferred ? '(preferred)' : '';
return `[${locale.locale}] ${typeValue}${nameValue} ${preferredText}`
}
const getMappingLabel = (mapping, formatted=false) => {
if(!mapping)
return '';
const label = [
`UID: ${mapping.id}`,
`Relationship: ${mapping.map_type}`,
`Source: ${mapping.owner} / ${mapping.source}`,
`From Concept Code: ${mapping.from_concept_code}`,
`From Concept Name: ${mapping.from_concept_name}`,
`To Concept Code: ${mapping.to_concept_code}`,
`To Concept Name: ${mapping.to_concept_name}`,
].join('\n')
if(formatted)
return <div style={{whiteSpace: 'pre'}}>{label}</div>;
return label
}
class ConceptsComparison extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoadingLHS: true,
isLoadingRHS: true,
lhs: {},
rhs: {},
}
}
componentDidMount() {
this.setObjectsForComparison()
}
componentDidUpdate(prevProps) {
if(prevProps.location.search !== this.props.location.search)
this.setObjectsForComparison()
}
setObjectsForComparison() {
const queryParams = new URLSearchParams(this.props.location.search)
this.fetchConcept(queryParams.get('lhs'), 'lhs', 'isLoadingLHS')
this.fetchConcept(queryParams.get('rhs'), 'rhs', 'isLoadingRHS')
}
fetchConcept(uri, attr, loadingAttr) {
if(uri && attr && loadingAttr) {
APIService.new().overrideURL(uri).get(null, null, {includeInverseMappings: true}).then(response => {
if(get(response, 'status') === 200)
this.setState({[attr]: this.formatConcept(response.data), [loadingAttr]: false}, this.sortMappings)
})
}
}
formatConcept(concept) {
concept.names = this.sortLocales(concept.names)
concept.descriptions = this.sortLocales(concept.descriptions)
concept.extras = toObjectArray(concept.extras)
return concept
}
sortMappings() {
if(!isEmpty(get(this.state.lhs, 'mappings')) && !isEmpty(get(this.state.rhs, 'mappings'))) {
const newState = {...this.state};
if(newState.lhs.mappings.length > newState.rhs.mappings.length) {
newState.lhs.mappings = uniqBy([...sortBy(
newState.rhs.mappings, m1 => findIndex(newState.lhs.mappings, m2 => m1.id === m2.id)
), ...newState.lhs.mappings], 'id')
} else {
newState.rhs.mappings = uniqBy([...sortBy(
newState.lhs.mappings, m1 => findIndex(newState.rhs.mappings, m2 => m1.id === m2.id)
), ...newState.rhs.mappings], 'id')
}
this.setState(newState)
}
}
sortLocales = locales => {
return [
...filter(locales, {name_type: 'FULLY_SPECIFIED', locale_preferred: true}),
...filter(reject(locales, {name_type: 'FULLY_SPECIFIED'}), {locale_preferred: true}),
...filter(locales, {name_type: 'FULLY_SPECIFIED', locale_preferred: false}),
...reject(reject(locales, {name_type: 'FULLY_SPECIFIED'}), {locale_preferred: true}),
]
}
getConceptHeader(concept) {
return (
<React.Fragment>
<div style={{margin: '5px 2px'}}>
<span>
<span className='gray-italics'>Source:</span>
<span>{concept.source}</span>
</span>
<span style={{marginLeft: '10px'}}>
<span className='gray-italics'>Type:</span>
<span>{concept.concept_class}</span>
</span>
<span style={{marginLeft: '10px'}}>
<span className='gray-italics'>UID:</span>
<span>{concept.id}</span>
</span>
</div>
</React.Fragment>
)
}
getValue(concept, attr, type, formatted=false) {
if(type === 'list') {
if(includes(['names', 'descriptions'], attr)) {
const locales = get(concept, attr)
if(isEmpty(locales)) return '';
if(formatted)
return map(locales, getLocaleLabelFormatted)
return map(locales, getLocaleLabel).join('\n')
} else if (attr === 'mappings') {
const mappings = get(concept, attr);
if(isEmpty(mappings)) return '';
return map(mappings, mapping => getMappingLabel(mapping, formatted));
}
} else if(type === 'date') {
let date = get(concept, attr);
if(attr === 'created_on')
date ||= get(concept, 'created_at')
if(attr === 'updated_on')
date ||= get(concept, 'updated_at')
return date ? formatDate(date) : '';
} else if (type === 'textFormatted') {
if(attr === 'owner')
return `${concept.owner_type}: ${concept.owner}`
} else if (type === 'bool') {
return get(concept, attr) ? 'True' : 'False'
} else {
if(includes(['created_by', 'updated_by'], attr))
return get(concept, attr) || get(concept, `version_${attr}`) || ''
return get(concept, attr, '')
}
}
maxArrayElement(v1, v2) {
const v1Length = isObject(v1) ? size(v1) : 0
const v2Length = isObject(v2) ? size(v2) : 0
return v1Length > v2Length ? v1 : v2;
}
getListAttrValue(attr, val, formatted=false) {
if(includes(['names', 'descriptions'], attr))
return formatted ? getLocaleLabelFormatted(val) : getLocaleLabel(val)
if(includes(['mappings'], attr))
return getMappingLabel(val, formatted)
if(includes(['extras'], attr))
return this.getExtraAttributeLabel(val)
}
getExtraAttributeLabel(val) {
if(!val)
return ''
return `${keys(val)[0]}: ${JSON.stringify(values(val)[0])}`
}
render() {
const { lhs, rhs, isLoadingLHS, isLoadingRHS } = this.state;
const isLoading = isLoadingLHS || isLoadingRHS;
return (
<React.Fragment>
{
isLoading ?
<div style={{textAlign: 'center'}}>
<CircularProgress color='primary' />
</div> :
<div className='col-md-12' style={{paddingTop: '10px', paddingBottom: '10px'}}>
<TableContainer style={{borderRadius: '4px', border: '1px solid lightgray'}}>
<Table size='small'>
<TableHead>
<TableRow colSpan="12">
<TableCell colSpan="2" style={{width: '10%'}}></TableCell>
<TableCell colSpan="5" style={{width: '45%'}}>
<div style={{fontSize: '14px'}}>
{this.getConceptHeader(lhs)}
</div>
<div style={{fontSize: '18px'}}>
<Link to={lhs.url}>{get(lhs, 'display_name')}</Link>
</div>
</TableCell>
<TableCell colSpan="5" style={{width: '45%'}}>
<div style={{fontSize: '14px'}}>
{this.getConceptHeader(rhs)}
</div>
<div style={{fontSize: '18px'}}>
<Link to={rhs.url}>{get(rhs, 'display_name')}</Link>
</div>
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{
map(ATTRIBUTES, (attrs, type) => {
return map(attrs, attr => {
const lhsValue = this.getValue(lhs, attr, type);
const rhsValue = this.getValue(rhs, attr, type);
const isDiff = !isEqual(lhsValue, rhsValue);
const maxLengthAttr = type === 'list' ? this.maxArrayElement(get(lhs, attr), get(rhs, attr)) : [];
const rowSpan = size(maxLengthAttr);
return (
<React.Fragment key={attr}>
{
type === 'list' ?
map(maxLengthAttr, (_attr, index) => {
const _lhsVal = get(lhs, `${attr}.${index}`, '')
const _rhsVal = get(rhs, `${attr}.${index}`, '')
const _lhsValCleaned = this.getListAttrValue(attr, _lhsVal)
const _rhsValCleaned = this.getListAttrValue(attr, _rhsVal)
const _isDiff = !isEqual(_lhsValCleaned, _rhsValCleaned);
return (
<TableRow key={_attr.uuid || index} colSpan='12'>
{
index === 0 &&
<TableCell colSpan='2' rowSpan={rowSpan} style={{width: '10%', fontWeight: 'bold', verticalAlign: 'top'}}>
{startCase(attr)}
</TableCell>
}
{
_isDiff ?
<TableCell colSpan='10' style={{width: '90%'}} className='diff-row'>
<ReactDiffViewer
oldValue={_lhsValCleaned}
newValue={_rhsValCleaned}
showDiffOnly={false}
splitView
hideLineNumbers
/>
</TableCell> :
<React.Fragment>
<TableCell colSpan='5' style={{width: '45%'}}>
{this.getListAttrValue(attr, _lhsVal, true)}
</TableCell>
<TableCell colSpan='5' style={{width: '45%'}}>
{this.getListAttrValue(attr, _rhsVal, true)}
</TableCell>
</React.Fragment>
}
</TableRow>
)
}) :
<TableRow key={attr} colSpan='12'>
<TableCell colSpan='2' style={{width: '10%', fontWeight: 'bold', verticalAlign: 'top'}}>
{startCase(attr)}
</TableCell>
{
isDiff ?
<TableCell colSpan='10' style={{width: '90%'}} className='diff-row'>
<ReactDiffViewer
oldValue={lhsValue}
newValue={rhsValue}
showDiffOnly={false}
splitView
hideLineNumbers
/>
</TableCell> :
<React.Fragment>
<TableCell colSpan='5' style={{width: '45%'}}>
{this.getValue(lhs, attr, type, true)}
</TableCell>
<TableCell colSpan='5' style={{width: '45%'}}>
{this.getValue(rhs, attr, type, true)}
</TableCell>
</React.Fragment>
}
</TableRow>
}
</React.Fragment>
)
})
})
}
</TableBody>
</Table>
</TableContainer>
</div>
}
</React.Fragment>
)
}
}
export default ConceptsComparison;