fathom/assets/js/components/Pageviews.js

69 lines
1.6 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 = {
records: []
}
this.fetchRecords = this.fetchRecords.bind(this);
}
componentDidMount() {
2016-12-08 11:43:13 +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 );
Client.request(`/pageviews?before=${before}&after=${after}`)
.then((d) => { this.setState({ records: d })})
.catch((e) => { console.log(e) })
2016-11-23 18:40:35 +00:00
}
render() {
const tableRows = this.state.records.map( (p, i) => (
<tr>
2016-11-24 15:47:33 +00:00
<td class="muted">{i+1}</td>
2016-12-09 09:12:44 +00:00
<td><a href={p.Url + 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">
<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