-
Notifications
You must be signed in to change notification settings - Fork 364
/
Sidebar.js
68 lines (55 loc) · 1.83 KB
/
Sidebar.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
'use strict';
import { h, Component } from 'preact';
import Client from '../lib/client.js';
import { bind } from 'decko';
import CountWidget from './CountWidget.js';
class Sidebar extends Component {
constructor(props) {
super(props)
this.state = {
data: {},
loading: false,
}
}
componentWillReceiveProps(newProps, newState) {
if(!this.paramsChanged(this.props, newProps)) {
return;
}
this.fetchData(newProps);
}
paramsChanged(o, n) {
return o.siteId != n.siteId || o.dateRange[0] != n.dateRange[0] || o.dateRange[1] != n.dateRange[1];
}
@bind
fetchData(props) {
this.setState({ loading: true })
let before = props.dateRange[1]/1000;
let after = props.dateRange[0]/1000;
Client.request(`/sites/${props.siteId}/stats/site/agg?before=${before}&after=${after}`)
.then((data) => {
// request finished; check if timestamp range is still the one user wants to see
if(this.paramsChanged(props, this.props)) {
return;
}
// Make sure we always show at least 1 visitor when there are pageviews
if ( data.Visitors == 0 && data.Pageviews > 0 ) {
data.Visitors = 1
}
this.setState({
loading: false,
data: data
})
})
}
render(props, state) {
return (
<div class="box box-totals">
<CountWidget title="Unique visitors" value={state.data.Visitors} loading={state.loading} />
<CountWidget title="Pageviews" value={state.data.Pageviews} loading={state.loading} />
<CountWidget title="Avg time on site" value={state.data.AvgDuration} format="duration" loading={state.loading} />
<CountWidget title="Bounce rate" value={state.data.BounceRate} format="percentage" loading={state.loading} />
</div>
)
}
}
export default Sidebar