diff --git a/assets/js/components/CountWidget.js b/assets/js/components/CountWidget.js index eab6495..900ab5c 100644 --- a/assets/js/components/CountWidget.js +++ b/assets/js/components/CountWidget.js @@ -19,41 +19,33 @@ class CountWidget extends Component { this.state = { value: '-', - loading: false + loading: false, + before: props.before, + after: props.after, } - this.fetchData(props.period); } - componentWillReceiveProps(newProps) { - if(this.props.period != newProps.period) { - this.fetchData(newProps.period) - } + componentDidMount() { + this.fetchData(); + } + + componentWillReceiveProps(newProps, prevState) { + this.setState({ + before: newProps.before, + after: newProps.after, + }); + + if(newProps.before != prevState.before || newProps.after != prevState.after) { + this.fetchData(); + } } @bind - fetchData(period) { - let before, after; - let afterDate = new Date(); - afterDate.setHours(0, 0, 0, 0); - - switch(period) { - case "week": - afterDate.setDate(afterDate.getDate() - (afterDate.getDay() + 6) % 7); - break; - case "month": - afterDate.setDate(1); - break; - case "year": - afterDate.setDate(1); - afterDate.setMonth(0); - break; - } - - before = Math.round((+new Date() ) / 1000); - after = Math.round((+afterDate) / 1000); + fetchData() { + console.log(this.state); this.setState({ loading: true }) - Client.request(`${this.props.endpoint}/count?before=${before}&after=${after}`) + Client.request(`${this.props.endpoint}/count?before=${this.state.before}&after=${this.state.after}`) .then((d) => { this.setState({ loading: false, diff --git a/assets/js/components/DatePicker.js b/assets/js/components/DatePicker.js index aa3b215..23b6101 100644 --- a/assets/js/components/DatePicker.js +++ b/assets/js/components/DatePicker.js @@ -27,21 +27,54 @@ class DatePicker extends Component { super(props) this.state = { - period: this.props.value + period: this.props.value, + before: 0, + after: 0, } } + componentDidMount() { + this.setTimeRange(this.state.period) + } + + @bind + setTimeRange(period) { + let beforeDate = new Date(); + let afterDate = new Date(); + afterDate.setHours(0, 0, 0, 0); + switch(period) { + case "week": + afterDate.setDate(afterDate.getDate() - (afterDate.getDay() + 6) % 7); + break; + case "month": + afterDate.setDate(1); + break; + case "year": + afterDate.setDate(1); + afterDate.setMonth(0); + break; + } + + let before, after; + before = Math.round((+beforeDate ) / 1000); + after = Math.round((+afterDate) / 1000); + this.setState({ + before: before, + after: after, + }); + this.props.onChange(this.state); + } + @bind setPeriod(e) { e.preventDefault(); - var nextState = { - period: e.target.dataset.value - } - - if(this.state.period != nextState.period) { - this.setState(nextState) - this.props.onChange(nextState.period); + let period = e.target.dataset.value; + if( period !== this.state.period) { + this.setState({ + period: period, + }); + this.setTimeRange(this.state.period); } } diff --git a/assets/js/components/LogoutButton.js b/assets/js/components/LogoutButton.js index 64b99ee..f041d02 100644 --- a/assets/js/components/LogoutButton.js +++ b/assets/js/components/LogoutButton.js @@ -2,14 +2,11 @@ import { h, render, Component } from 'preact'; import Client from '../lib/client.js'; +import { bind } from 'decko'; class LogoutButton extends Component { - constructor(props) { - super(props) - this.handleSubmit = this.handleSubmit.bind(this); - } - + @bind handleSubmit(e) { e.preventDefault(); diff --git a/assets/js/components/Notification.js b/assets/js/components/Notification.js index 7860d72..596cc56 100644 --- a/assets/js/components/Notification.js +++ b/assets/js/components/Notification.js @@ -1,6 +1,7 @@ 'use strict' import { h, render, Component } from 'preact'; +import { bind } from 'decko'; class Notification extends Component { constructor(props) { @@ -13,27 +14,28 @@ class Notification extends Component { this.timeout = 0 } - clearMessage() { - this.setState({ message: '' }) - } - componentWillReceiveProps(newProps) { if(newProps.message != this.state.message) { this.setState({ message: newProps.message, kind: newProps.kind || 'error' }) window.clearTimeout(this.timeout) - this.timeout = window.setTimeout(this.clearMessage.bind(this), 5000) + this.timeout = window.setTimeout(this.clearMessage, 5000) } } - render() { - if(this.state.message === '') { + @bind + clearMessage() { + this.setState({ message: '' }) + } + + render(props, state) { + if(state.message === '') { return '' } return (