-
Notifications
You must be signed in to change notification settings - Fork 182
/
smart-services.tsx
80 lines (62 loc) · 1.71 KB
/
smart-services.tsx
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
import React, {Component, HTMLAttributes} from 'react';
import PropTypes from 'prop-types';
import Auth from '../auth/auth';
import HTTP from '../http/http';
import Services from './services';
import {Service} from './services-link';
function noop() {}
export interface SmartServicesProps extends HTMLAttributes<HTMLElement> {
auth: Auth
}
export default class SmartServices extends Component<SmartServicesProps> {
static propTypes = {
auth: PropTypes.instanceOf(Auth).isRequired
};
state = {
visible: true,
loading: false,
services: null
};
componentDidMount() {
const {auth} = this.props;
this.http = new HTTP(auth, auth.getAPIPath());
this.getServices(SmartServices.countFields)?.then(services => {
if (!services.length) {
this.setState({visible: false});
}
}).catch(noop);
}
static allFields = 'id,name,applicationName,homeUrl,iconUrl';
static countFields = 'key';
http?: HTTP;
stopLoading = () => {
this.setState({loading: false});
};
getServicesContent = () => {
this.setState({loading: true});
this.getServices(SmartServices.allFields)?.then(services => {
this.setState({services});
this.stopLoading();
}).catch(this.stopLoading);
};
getServices(fields: string) {
return this.http?.get<Service[]>(`services/header?fields=${fields}`);
}
render() {
const {services, visible, loading} = this.state;
const {auth, ...props} = this.props;
if (!visible) {
return null;
}
return (
<Services
{...props}
clientId={auth.config.clientId}
initShown
loading={loading}
onClick={this.getServicesContent}
services={services}
/>
);
}
}