'use strict'; import { h, render, Component } from 'preact'; class CountWidget extends Component { constructor(props) { super(props) this.state = { count: 0 } this.fetchData = this.fetchData.bind(this); this.fetchData(props.period); } componentWillReceiveProps(newProps) { if(this.props.period != newProps.period) { this.fetchData(newProps.period) } } fetchData(period) { return fetch('/api/' + this.props.endpoint + '/count?period=' + period, { credentials: 'include' }).then((r) => { if( r.ok ) { return r.json(); } throw new Error(); }).then((data) => { this.setState({ count: data }) }); } render() { return (

{this.props.title}

{this.state.count}
last {this.props.period} days
) } } export default CountWidget