UNPKG

1.55 kBJavaScriptView Raw
1import React, {Component} from 'react';
2import PropTypes from 'prop-types';
3
4import Auth from '../auth/auth';
5import HTTP from '../http/http';
6
7import Services from './services';
8
9function noop() {}
10
11export default class SmartServices extends Component {
12 static allFields = 'id,name,applicationName,homeUrl,iconUrl';
13 static countFields = 'key';
14
15 static propTypes = {
16 auth: PropTypes.instanceOf(Auth).isRequired
17 };
18
19 state = {
20 visible: true,
21 loading: false,
22 services: null
23 };
24
25 componentDidMount() {
26 const {auth} = this.props;
27
28 this.http = new HTTP(auth, auth.getAPIPath());
29
30 this.getServices(SmartServices.countFields).then(services => {
31 if (!services.length) {
32 this.setState({visible: false});
33 }
34 }).catch(noop);
35 }
36
37 stopLoading = () => {
38 this.setState({loading: false});
39 };
40
41 getServicesContent = () => {
42 this.setState({loading: true});
43
44 this.getServices(SmartServices.allFields).then(services => {
45 this.setState({services});
46 this.stopLoading();
47 }).catch(this.stopLoading);
48 };
49
50 getServices(fields) {
51 return this.http.get(`services/header?fields=${fields}`);
52 }
53
54 render() {
55 const {services, visible, loading} = this.state;
56 const {auth, ...props} = this.props;
57
58 if (!visible) {
59 return null;
60 }
61
62 return (
63 <Services
64 {...props}
65 clientId={auth.config.clientId}
66 initShown
67 loading={loading}
68 onClick={this.getServicesContent}
69 services={services}
70 />
71 );
72 }
73}