fathom/assets/js/components/Table.js

72 lines
1.8 KiB
JavaScript
Raw Normal View History

'use strict';
import { h, render, Component } from 'preact';
import * as numbers from '../lib/numbers.js';
import Client from '../lib/client.js';
2018-05-01 14:06:17 +00:00
import { bind } from 'decko';
const dayInSeconds = 60 * 60 * 24;
class Table extends Component {
constructor(props) {
super(props)
this.state = {
2016-12-07 21:00:55 +00:00
records: [],
2018-05-01 14:06:17 +00:00
limit: 100,
2016-12-10 17:19:44 +00:00
loading: true
}
}
componentDidMount() {
2016-12-08 11:43:13 +00:00
this.fetchRecords(this.props.period, this.state.limit)
}
componentWillReceiveProps(newProps) {
if(this.props.period != newProps.period) {
2016-12-07 21:00:55 +00:00
this.fetchRecords(newProps.period, this.state.limit)
}
}
2018-05-01 14:06:17 +00:00
@bind
2016-12-07 21:00:55 +00:00
fetchRecords(period, limit) {
2016-12-10 17:19:44 +00:00
this.setState({ loading: true });
const before = Math.round((+new Date() ) / 1000);
const after = before - ( period * dayInSeconds );
Client.request(`${this.props.endpoint}?before=${before}&after=${after}&limit=${limit}`)
.then((d) => {
this.setState({ loading: false, records: d }
2018-05-01 14:06:17 +00:00
)})
}
2018-05-01 14:06:17 +00:00
render(props, state) {
const tableRows = state.records !== null ? state.records.map((p, i) => (
<div class="table-row">
<div class="cell main-col"><a href="#">/about-us/</a></div>
<div class="cell">445.2k</div>
<div class="cell">5,456</div>
</div>
)) : <div class="table-row">Nothing here, yet.</div>;
2018-05-01 14:06:17 +00:00
const loadingOverlay = state.loading ? <div class="loading-overlay"><div></div></div> : '';
2016-12-10 17:19:44 +00:00
return (
2018-05-01 14:06:17 +00:00
<div class="box box-pages animated fadeInUp delayed_04s">
<div class="table-row header">
{props.headers.map((header, i) => {
let classes = i === 0 ? 'main-col cell' : 'cell';
return (<div class={classes}>{header}</div>)
})}
</div>
{tableRows}
</div>
)
}
}
export default Table