-
Notifications
You must be signed in to change notification settings - Fork 12
/
ConceptMapHome.jsx
202 lines (187 loc) · 6.96 KB
/
ConceptMapHome.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
import React from 'react';
import { get, isObject, find } from 'lodash';
import { Pagination } from '@mui/material';
import { CircularProgress } from '@mui/material';
import APIService from '../../services/APIService';
import { getAppliedServerConfig } from '../../common/utils';
import NotFound from '../common/NotFound';
import NavigationButtonGroup from '../search/NavigationButtonGroup';
import ContainerHomeHeader from './ContainerHomeHeader';
import ConceptMapGroups from './ConceptMapGroups';
import { CONCEPT_MAP_DEFAULT_CONFIG } from "../../common/defaultConfigs"
class ConceptMapHome extends React.Component {
constructor(props) {
super(props);
this.URLAttr = 'ConceptMap'
const config = CONCEPT_MAP_DEFAULT_CONFIG
const server = getAppliedServerConfig()
const isHAPI = get(server, 'hapi', false);
const currentURL = isHAPI ?
`/fhir/${this.URLAttr}/${props.match.params.id}` :
window.location.hash.split('?')[0].replace('#', '');
const serverURL = isHAPI ?
`${server.info.baseURI}${this.URLAttr}/${props.match.params.id}` :
window.location.hash.split('?')[0].replace('#/fhir', '');
this.state = {
server: server,
isHAPI: isHAPI,
notFound: false,
isLoading: true,
isLoadingGroups: true,
selectedConfig: config,
resource: {},
codes: {
next: null,
previous: null,
results: [],
pageNumber: 1,
total: 0,
pageCount: 100,
pages: 1,
},
url: currentURL,
serverUrl: serverURL,
}
}
componentDidMount() {
this.refreshDataByURL()
}
componentDidUpdate(prevProps) {
if(
(prevProps.location.pathname !== this.props.location.pathname) ||
(prevProps.location.search !== this.props.location.search)
) {
this.refreshDataByURL()
}
}
getCurrentPage = data => {
const selfLink = this.getLinkURL(data, 'self')
if(selfLink !== 'null') {
const query = new URLSearchParams(selfLink.split('?')[1])
const page = query.get('page')
if(page)
return parseInt(page)
}
return 1
}
getTotalPages = total => {
const pages = Math.ceil(total/100);
return pages < 1 ? 1 : pages
}
getLinkURL = (response, rel) => {
const url = get(find(get(response, 'link', []), {relation: rel}), 'url')
if(url !== 'null')
return url
}
getOwner = data => {
const selfLink = this.getLinkURL(data, 'self')
let ownerType;
let owner;
let ownerURL;
if(selfLink.match('/orgs/')){
ownerType = 'Organization'
owner = get(get(selfLink.split('/orgs/'), '1', '').split('/'), '0')
ownerURL = `/orgs/${owner}/`
}
else if (selfLink.match('/users/')) {
ownerType = 'User'
owner = get(get(selfLink.split('/users/'), '1', '').split('/'), '0')
ownerURL = `/users/${owner}/`
}
if(ownerType && owner)
return {ownerType: ownerType, owner: owner, ownerURL: ownerURL}
}
refreshDataByURL(loadingGroups = false, page) {
const { isHAPI, codes, server } = this.state;
this.setState({isLoading: !loadingGroups, isLoadingGroups: loadingGroups, notFound: false}, () => {
const queryParams = isHAPI ? {} : {page: page || codes.pageNumber}
APIService.new()
.overrideURL(this.state.serverUrl)
.get(null, null, queryParams)
.then(response => {
if(get(response, 'detail') === "Not found.")
this.setState({isLoading: false, notFound: true, resource: {}})
else if(!isObject(response))
this.setState({isLoading: false}, () => {throw response})
else {
const resource = isHAPI ? response.data : get(response, 'data.entry.0.resource');
const owner = isHAPI ? {owner: server.info.org.id} : this.getOwner(response.data)
const groups = get(resource, 'group') || [];
const total = get(groups, 'length') || 0;
this.setState({
isLoadingGroups: false,
isLoading: false,
resource: {...resource, ...owner},
codes: {
next: this.getLinkURL(response.data, 'next'),
previous: this.getLinkURL(response.data, 'previous'),
results: groups,
pageNumber: isHAPI ? 1 : this.getCurrentPage(response.data),
pages: isHAPI ? 1 : this.getTotalPages(total)
},
})
}
})
})
}
onPageChange = page => this.refreshDataByURL(true, page)
onNavClick = next => this.refreshDataByURL(
true, next ? this.state.codes.pageNumber + 1 : this.state.codes.pageNumber - 1
);
render() {
const {
resource, codes, isLoading, notFound, isHAPI, url, serverUrl, isLoadingGroups
} = this.state;
const source = {...resource, canonical_url: resource.url, release_date: resource.date};
return (
<div style={isLoading ? {textAlign: 'center', marginTop: '40px'} : {}}>
{
isLoading ?
<CircularProgress color='primary' /> :
(
notFound ?
<NotFound /> :
<div className='col-md-12 home-container no-side-padding'>
<ContainerHomeHeader
resource={this.URLAttr}
source={source}
url={`#${url}`}
parentURL={`/fhir/${this.URLAttr}`}
serverURL={serverUrl}
isHAPI={isHAPI}
/>
{
!isHAPI &&
<div className='col-md-12 flex-vertical-center' style={{justifyContent: 'flex-end'}}>
<span>
<NavigationButtonGroup onClick={this.onNavClick} next={Boolean(codes.next)} prev={Boolean(codes.previous)} />
</span>
</div>
}
<div className='col-md-12'>
<ConceptMapGroups resource={source} groups={codes.results} isHAPI={isHAPI} isLoading={isLoadingGroups} />
</div>
{
!isHAPI &&
<div className='col-md-12 no-side-padding pagination-center' style={{textAlign: 'center'}}>
<Pagination
onChange={(event, page) => this.onPageChange(page)}
variant="outlined"
shape="rounded"
color="primary"
showFirstButton
showLastButton={false}
page={codes.pageNumber}
count={codes.next ? codes.pageNumber + 1 : codes.pageNumber}
style={{display: 'inline-flex', marginTop: '10px'}}
/>
</div>
}
</div>
)
}
</div>
)
}
}
export default ConceptMapHome;