fathom/assets/js/components/Pageviews.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-11-23 18:40:35 +00:00
'use strict';
import { h, render, Component } from 'preact';
import * as numbers from '../lib/numbers.js';
import Client from '../lib/client.js';
const dayInSeconds = 60 * 60 * 24;
2016-11-23 18:40:35 +00:00
class Pageviews extends Component {
constructor(props) {
super(props)
this.state = {
2016-12-10 17:19:44 +00:00
records: [],
loading: false
2016-11-23 18:40:35 +00:00
}
this.fetchRecords = this.fetchRecords.bind(this);
}
componentDidMount() {
2016-12-10 17:19:44 +00:00
this.fetchRecords(this.props.period)
2016-11-23 18:40:35 +00:00
}
2016-11-23 20:29:54 +00:00
componentWillReceiveProps(newProps) {
if(this.props.period != newProps.period) {
this.fetchRecords(newProps.period)
}
}
fetchRecords(period) {
const before = Math.round((+new Date() ) / 1000);
const after = before - ( period * dayInSeconds );
2016-12-10 17:19:44 +00:00
this.setState({ loading: true })
Client.request(`/pageviews?before=${before}&after=${after}`)
2016-12-10 17:19:44 +00:00
.then((d) => { this.setState({ loading: false, records: d })})
.catch((e) => { console.log(e) })
2016-11-23 18:40:35 +00:00
}
render() {
2016-12-10 17:19:44 +00:00
const loadingOverlay = this.state.loading ? <div class="loading-overlay"><div></div></div> : '';
2016-11-23 18:40:35 +00:00
const tableRows = this.state.records.map( (p, i) => (
<tr>
2016-11-24 15:47:33 +00:00
<td class="muted">{i+1}</td>
<td><a href={"//" + p.Hostname + p.Path}>{p.Path.substring(0, 50)}{p.Path.length > 50 ? '..' : ''}</a></td>
<td>{numbers.formatWithComma(p.Count)}</td>
<td>{numbers.formatWithComma(p.CountUnique)}</td>
2016-11-23 18:40:35 +00:00
</tr>
));
return (
2016-11-24 15:47:33 +00:00
<div class="block">
2016-12-10 17:19:44 +00:00
{loadingOverlay}
<h3>Pageviews</h3>
2016-11-23 18:40:35 +00:00
<table class="table pageviews">
<thead>
<tr>
<th>#</th>
<th>URL</th>
<th>Pageviews</th>
<th>Unique</th>
</tr>
</thead>
<tbody>{tableRows}</tbody>
</table>
</div>
)
}
}
export default Pageviews