-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-web.js
100 lines (88 loc) · 2.37 KB
/
client-web.js
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
/* eslint-disable no-console */
import { h, render, Component } from 'preact'
import { style } from 'typestyle'
import { HealthCheckPromiseClient } from './interfaces/healthcheck_grpc_web_pb'
import { PingPongReq } from './interfaces/healthcheck_pb'
const button = type => ({
$nest: {
'&:hover': {
background: type === 'ping' ? 'lightblue' : '#FE669F',
}
},
background: 'white',
boxShadow: '0 1px 5px rgba(0, 0, 0, .5)',
fontSize: type === 'ping' ? 20 : 10,
fontFamily: 'Menlo',
margin: 10,
padding: 10,
textAlign: 'center',
transition: 'background .5s, color .5s',
width: `${type === 'ping' ? 100 : 30}px`,
display: 'inline-block',
})
const pingBtn = style(button('ping'))
const resetBtn = style(button('reset'))
class App extends Component {
constructor (props) {
super(props)
this.resetData = this.resetData.bind(this)
this.sendReq = this.sendReq.bind(this)
this.state = {
data: null,
loading: false,
}
}
componentWillMount() {
this.client = new HealthCheckPromiseClient(`http://${process.env.ENVOY_CONTAINER_IP || 'localhost'}`)
}
resetData(ev) {
ev.preventDefault()
this.setState({ ...this.state, data: null })
}
async sendReq(ev) {
ev.preventDefault()
if ( !this.client ) return
this.setState({
...this.state,
data: null,
loading: !this.state.loading,
})
const req = new PingPongReq()
req.setMsg('PING')
let res
try {
res = await this.client.pingPong( req )
}
catch (err) {
res = new Error(err.message)
res.code = err.code
}
return this.setState({
...this.state,
data: res instanceof Error ? res.message : res.getMsg(),
loading: !this.state.loading,
})
}
render() {
return (
<div>
{this.state.loading
? <h1>Sending request ...</h1>
: (
<div>
<div className={pingBtn} onClick={this.sendReq}>PING</div>
<div className={resetBtn} onClick={this.resetData}>Reset</div>
{this.state.data &&
<p style={{fontFamily: 'Menlo'}}>
<em>Response from server: </em>
<h3 style={{display: 'inline-block'}}>{this.state.data}</h3>
</p>
}
</div>
)
}
</div>
)
}
}
render(<App />, document.getElementById('root'))