'use strict';
import { h, render, Component } from 'preact';
class Table extends Component {
constructor(props) {
super(props)
this.state = {
records: []
}
this.tableHeaders = props.headers.map(heading =>
{heading} | );
this.fetchRecords = this.fetchRecords.bind(this);
this.fetchRecords(props.period);
}
componentWillReceiveProps(newProps) {
if(this.props.period != newProps.period) {
this.fetchRecords(newProps.period)
}
}
fetchRecords(period) {
return fetch('/api/'+this.props.endpoint+'?period=' + period, {
credentials: 'include'
}).then((r) => {
if( r.ok ) {
return r.json();
}
// TODO: do something with error
}).then((data) => {
this.setState({ records: data })
});
}
render() {
const tableRows = this.state.records.map( (p, i) => (
{i+1} |
{p.Label} |
{p.Count} |
{Math.round(p.Percentage)}% |
));
return (
{this.props.title}
{this.tableHeaders}
{tableRows}
)
}
}
export default Table