-
Notifications
You must be signed in to change notification settings - Fork 13
/
UserHome.jsx
124 lines (106 loc) · 3.08 KB
/
UserHome.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
import React from 'react';
import { isEmpty } from 'lodash';
import APIService from '../../services/APIService';
import UserHomeDetails from './UserHomeDetails';
import UserHomeTabs from './UserHomeTabs';
class UserHome extends React.Component {
constructor(props) {
super(props);
this.url = this.getURLFromPath(props);
this.state = {
isLoading: true,
user: {},
sources: [],
collections: [],
orgs: [],
isLoadingSources: false,
isLoadingCollections: true,
isLoadingOrgs: false,
tab: this.getDefaultTabIndex()
}
}
getURLFromPath(props) {
props = props || this.props;
const { location } = props;
return location.pathname.split('/').slice(0, 3).join('/') + '/';
}
getDefaultTabIndex() {
const { location } = this.props;
if(location.pathname.indexOf('/organizations') > -1)
return 1;
if(location.pathname.indexOf('/orgs') > -1)
return 1;
return 0;
}
componentDidMount() {
this.refreshDataByURL()
}
componentDidUpdate(prevProps) {
if(prevProps.location.pathname !== this.props.location.pathname) {
this.url = this.getURLFromPath()
this.refreshDataByURL()
this.onTabChange(null, this.getDefaultTabIndex())
}
}
refreshDataByURL() {
this.setState(
{ isLoading: true },
() => APIService.new().overrideURL(this.url).get().then(
response => this.setState({ isLoading: false, user: response.data }, this.fetchTabContent)
))
}
fetchTabContent() {
if(this.state.tab === 0) {
this.getSources()
this.getCollections()
} else
this.getOrgs()
}
getOrgs() {
if(!isEmpty(this.state.orgs))
return
this.setState({isLoadingOrgs: true}, () =>
APIService.new()
.overrideURL(this.url + 'orgs/')
.get()
.then(response => this.setState({orgs: response.data, isLoadingOrgs: false}))
)
}
getSources() {
if(!isEmpty(this.state.sources))
return
this.setState({isLoadingSources: true}, () =>
APIService.new()
.overrideURL(this.url + 'sources/')
.get()
.then(response => this.setState({sources: response.data, isLoadingSources: false}))
)
}
getCollections() {
if(!isEmpty(this.state.collections))
return
this.setState({isLoadingCollections: true}, () =>
APIService.new()
.overrideURL(this.url + 'collections/')
.get()
.then(response => this.setState({collections: response.data, isLoadingCollections: false}))
)
}
onTabChange = (event, value) => {
this.setState({tab: value}, this.fetchTabContent)
}
render() {
const {user, isLoading} = this.state;
return (
<div className="col-md-12">
<div className="col-md-3">
<UserHomeDetails user={user} isLoading={isLoading} />
</div>
<div className='col-md-9' style={{marginTop: '15px'}}>
<UserHomeTabs {...this.state} onChange={this.onTabChange} />
</div>
</div>
)
}
}
export default UserHome;