-
Notifications
You must be signed in to change notification settings - Fork 12
/
SourceHome.jsx
237 lines (209 loc) · 8.08 KB
/
SourceHome.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
import React from 'react';
import { CircularProgress } from '@material-ui/core';
import { includes, isEmpty, get, findIndex, isEqual, find, isObject } from 'lodash';
import APIService from '../../services/APIService';
import SourceHomeHeader from './SourceHomeHeader';
import SourceHomeTabs from './SourceHomeTabs';
import NotFound from '../common/NotFound';
import AccessDenied from '../common/AccessDenied';
import PermissionDenied from '../common/PermissionDenied';
const TABS = ['details', 'concepts', 'mappings', 'versions', 'about']
const DEFAULT_CONFIG = {
name: 'OCL Default (Source)',
web_default: true,
is_default: false,
config: {
tabs: [
{type: "concepts", label: "Concepts", page_size: 25, "default": true, layout: 'table'},
{type: "mappings", label: "Mappings", page_size: 25, layout: 'table'},
{type: "versions", label: "Versions", page_size: 25, layout: 'table'},
{type: "about", label: "About"},
]
}
}
class SourceHome extends React.Component {
constructor(props) {
super(props);
this.state = {
notFound: false,
accessDenied: false,
permissionDenied: false,
isLoading: true,
source: {},
versions: [],
tab: this.getDefaultTabIndex(),
selectedConfig: null,
customConfigs: [],
}
}
customConfigFeatureApplicable() {
return this.props.location.search.indexOf('configs=true') > -1;
}
getDefaultTabIndexFromConfig() {
const index = findIndex(this.state.selectedConfig.config.tabs, {"default": true});
return index > -1 ? index : 0;
}
setTab() {
this.setState({tab: this.getDefaultTabIndexFromConfig()});
}
getDefaultTabIndex() {
const { location } = this.props;
if(location.pathname.indexOf('/about') > -1 && this.shouldShowAboutTab())
return 3;
if(location.pathname.indexOf('/versions') > -1)
return 2;
if(location.pathname.indexOf('/mappings') > -1)
return 1;
if(location.pathname.indexOf('/concepts') > -1)
return 0;
return 0;
}
componentDidMount() {
this.refreshDataByURL()
}
componentDidUpdate(prevProps) {
if(
(prevProps.location.pathname !== this.props.location.pathname) ||
(prevProps.location.search !== this.props.location.search)
) {
this.refreshDataByURL()
this.onTabChange(null, this.getDefaultTabIndex())
}
}
getVersionedObjectURLFromPath() {
const { location } = this.props;
return location.pathname.split('/').slice(0, 5).join('/') + '/';
}
getURLFromPath() {
const { location, match } = this.props;
if(location.pathname.indexOf('/versions') > -1)
return location.pathname.split('/versions')[0] + '/'
if(location.pathname.indexOf('/mappings') > -1)
return location.pathname.split('/mappings')[0] + '/'
if(location.pathname.indexOf('/concepts') > -1)
return location.pathname.split('/concepts')[0] + '/'
if(location.pathname.indexOf('/about') > -1)
return location.pathname.split('/about')[0] + '/'
if(location.pathname.indexOf('/details') > -1)
return location.pathname.split('/details')[0] + '/'
if(match.params.version)
return location.pathname.split('/').slice(0, 6).join('/') + '/';
return this.getVersionedObjectURLFromPath();
}
getVersions() {
APIService.new()
.overrideURL(this.getVersionedObjectURLFromPath() + 'versions/')
.get(null, null, {verbose: true, includeSummary: true})
.then(response => {
this.setState({versions: response.data})
})
}
onTabChange = (event, value) => {
this.setState({tab: value}, () => {
if(isEmpty(this.state.versions))
this.getVersions()
})
}
refreshDataByURL() {
this.setState({isLoading: true, notFound: false, accessDenied: false, permissionDenied: false}, () => {
APIService.new()
.overrideURL(this.getURLFromPath())
.get(null, null, {includeSummary: true, includeClientConfigs: true})
.then(response => {
if(get(response, 'detail') === "Not found.")
this.setState({isLoading: false, notFound: true, source: {}, accessDenied: false, permissionDenied: false})
else if(get(response, 'detail') === "Authentication credentials were not provided.")
this.setState({isLoading: false, notFound: false, source: {}, accessDenied: true, permissionDenied: false})
else if(get(response, 'detail') === "You do not have permission to perform this action.")
this.setState({isLoading: false, notFound: false, source: {}, accessDenied: false, permissionDenied: true})
else if(!isObject(response))
this.setState({isLoading: false}, () => {throw response})
else {
const source = response.data;
const customConfigs = get(source, 'client_configs', [])
const defaultCustomConfig = find(customConfigs, {is_default: true});
this.setState({
isLoading: false,
source: source,
selectedConfig: defaultCustomConfig || DEFAULT_CONFIG,
customConfigs: customConfigs,
}, () => {
if(isEmpty(this.state.versions))
this.getVersions()
})
}
})
})
}
onConfigChange = config => this.setState({selectedConfig: config})
isVersionedObject() {
const version = this.props.match.params.version;
if(version)
return includes(TABS, version)
return true
}
getCurrentVersion() {
let version = this.props.match.params.version;
if(!includes(TABS, version))
return version
}
shouldShowAboutTab() {
return !isEmpty(get(this, 'state.source.text'));
}
onVersionUpdate = updatedVersion => {
const newState = {...this.state}
const oldVersion = find(newState.versions, {uuid: updatedVersion.uuid})
const index = findIndex(newState.versions, {uuid: updatedVersion.uuid})
if(!updatedVersion.summary)
updatedVersion.summary = oldVersion.summary
newState.versions.splice(index, 1, updatedVersion)
this.setState(newState)
}
render() {
const {
source, versions, isLoading, tab, selectedConfig, customConfigs,
notFound, accessDenied, permissionDenied
} = this.state;
const currentURL = this.getURLFromPath()
const versionedObjectURL = this.getVersionedObjectURLFromPath()
const showAboutTab = this.shouldShowAboutTab();
const hasError = notFound || accessDenied || permissionDenied;
return (
<div style={isLoading ? {textAlign: 'center', marginTop: '40px'} : {}}>
{ isLoading && <CircularProgress color='primary' /> }
{ notFound && <NotFound /> }
{ accessDenied && <AccessDenied /> }
{ permissionDenied && <PermissionDenied /> }
{
!isLoading && !hasError &&
<div className='col-md-12 home-container no-side-padding'>
<SourceHomeHeader
source={source}
isVersionedObject={this.isVersionedObject()}
versionedObjectURL={versionedObjectURL}
currentURL={currentURL}
/>
<SourceHomeTabs
tab={tab}
onTabChange={this.onTabChange}
source={source}
versions={versions}
location={this.props.location}
match={this.props.match}
versionedObjectURL={versionedObjectURL}
currentVersion={this.getCurrentVersion()}
aboutTab={showAboutTab}
onVersionUpdate={this.onVersionUpdate}
customConfigs={[...customConfigs, DEFAULT_CONFIG]}
onConfigChange={this.onConfigChange}
selectedConfig={selectedConfig}
showConfigSelection={this.customConfigFeatureApplicable()}
isOCLDefaultConfigSelected={isEqual(selectedConfig, DEFAULT_CONFIG)}
/>
</div>
}
</div>
)
}
}
export default SourceHome;