-
Notifications
You must be signed in to change notification settings - Fork 12
/
MappingHome.jsx
151 lines (134 loc) · 5.04 KB
/
MappingHome.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
import React from 'react';
import { CircularProgress } from '@mui/material';
import { includes, get, isObject, has } from 'lodash';
import APIService from '../../services/APIService';
import MappingHomeHeader from './MappingHomeHeader';
import MappingHomeTabs from './MappingHomeTabs';
import NotFound from '../common/NotFound';
import AccessDenied from '../common/AccessDenied';
import PermissionDenied from '../common/PermissionDenied';
const TABS = ['details', 'history'];
class MappingHome extends React.Component {
constructor(props) {
super(props);
this.state = {
notFound: false,
accessDenied: false,
permissionDenied: false,
isLoading: true,
mapping: {},
versions: [],
tab: this.getDefaultTabIndex(),
}
}
componentDidMount() {
this.refreshDataByURL()
}
componentDidUpdate(prevProps) {
if(prevProps.location.pathname !== this.props.location.pathname) {
this.refreshDataByURL()
this.onTabChange(null, this.getDefaultTabIndex())
}
}
getDefaultTabIndex() {
const { location } = this.props;
if(location.pathname.indexOf('/history') > -1)
return 1;
return 0;
}
getMappingURLFromPath() {
const { location, match } = this.props;
if(location.pathname.indexOf('/details') > -1)
return location.pathname.split('/details')[0] + '/'
if(location.pathname.indexOf('/history') > -1)
return location.pathname.split('/history')[0] + '/'
if(match.params.mappingVersion)
return location.pathname.split('/').slice(0, 8).join('/') + '/';
return this.getVersionedObjectURLFromPath();
}
getVersionedObjectURLFromPath() {
const { location } = this.props;
return location.pathname.split('/').slice(0, 7).join('/') + '/';
}
refreshDataByURL() {
this.setState({isLoading: true, notFound: false, accessDenied: false, permissionDenied: false}, () => {
APIService.new()
.overrideURL(this.getMappingURLFromPath())
.get()
.then(response => {
if(get(response, 'detail') === "Not found.")
this.setState({isLoading: false, mapping: {}, notFound: true, accessDenied: false, permissionDenied: false})
else if(get(response, 'detail') === "Authentication credentials were not provided.")
this.setState({isLoading: false, notFound: false, mapping: {}, accessDenied: true, permissionDenied: false})
else if(get(response, 'detail') === "You do not have permission to perform this action.")
this.setState({isLoading: false, notFound: false, mapping: {}, accessDenied: false, permissionDenied: true})
else if(!isObject(response))
this.setState({isLoading: false}, () => {throw response})
else
this.setState({isLoading: false, mapping: response.data}, () => {
if(this.state.tab === 1)
this.getVersions()
})
})
})
}
getVersions() {
APIService.new()
.overrideURL(this.getVersionedObjectURLFromPath() + 'versions/')
.get(null, null, {includeCollectionVersions: true, includeSourceVersions: true})
.then(response => {
this.setState({versions: response.data})
})
}
onTabChange = (event, value) => {
this.setState({tab: value}, () => {
if(value === 1)
this.getVersions()
})
}
isVersionedObject() {
const version = this.props.match.params.mappingVersion;
if(version)
return includes(TABS, version)
return true
}
render() {
const {
mapping, versions, isLoading, tab, notFound, accessDenied, permissionDenied
} = this.state;
const currentURL = this.getMappingURLFromPath()
const isVersionedObject = this.isVersionedObject()
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'>
<MappingHomeHeader
mapping={mapping}
isVersionedObject={isVersionedObject}
versionedObjectURL={this.getVersionedObjectURLFromPath()}
currentURL={currentURL}
header={has(this.props, 'header') ? this.props.header : true}
tab={tab}
/>
<MappingHomeTabs
tab={tab}
onChange={this.onTabChange}
mapping={mapping}
versions={versions}
isVersionedObject={isVersionedObject}
noRedirect={this.props.noRedirect}
onTabChange={this.onTabChange}
/>
</div>
}
</div>
)
}
}
export default MappingHome;