refactor date logic for preset periods. switch out this week and this month for last-7 and last-30. closes #43

This commit is contained in:
Danny van Kooten 2018-08-07 09:20:22 +02:00
parent 720fa4a5da
commit c09fda89a3
2 changed files with 50 additions and 50 deletions

View File

@ -4,24 +4,47 @@ import { h, Component } from 'preact';
import { bind } from 'decko'; import { bind } from 'decko';
import Pikadayer from './Pikadayer.js'; import Pikadayer from './Pikadayer.js';
const availablePeriods = [ const now = new Date();
{
id: 'day', // today, yesterday, this week, last 7 days, last 30 days
label: 'Today' const availablePeriods = {
'today': {
label: 'Today',
start: function() {
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
}, },
{ end: function() {
id: 'week', return new Date(now.getFullYear(), now.getMonth(), now.getDate());
label: 'This week'
}, },
{
id: 'month',
label: 'This month'
}, },
{ 'last-7-days': {
id: 'year', label: 'Last 7 days',
label: 'This year' start: function() {
} return new Date(now.getFullYear(), now.getMonth(), now.getDate()-7);
]; },
end: function() {
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
},
},
'last-30-days': {
label: 'Last 30 days',
start: function() {
return new Date(now.getFullYear(), now.getMonth(), now.getDate()-30);
},
end: function() {
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
},
},
'this-year': {
label: 'This year',
start: function() {
return new Date(now.getFullYear(), 0, 1);
},
end: function() {
return new Date(this.start().getFullYear() + 1, 0, 0);
},
},
}
const timezoneOffset = (new Date()).getTimezoneOffset() * 60; const timezoneOffset = (new Date()).getTimezoneOffset() * 60;
const padZero = function(n){return n<10? '0'+n:''+n;} const padZero = function(n){return n<10? '0'+n:''+n;}
@ -43,35 +66,11 @@ class DatePicker extends Component {
@bind @bind
updateDatesFromPeriod(period) { updateDatesFromPeriod(period) {
let now = new Date(); if(typeof(availablePeriods[period]) !== "object") {
let startDate, endDate; return;
switch(period) {
case "day":
startDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
endDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
break;
case "week":
startDate = new Date(now.getFullYear(), now.getMonth(), (now.getDate() - (now.getDay() + 6) % 7));
endDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + 6);
break;
case "month":
startDate = new Date(now.getFullYear(), now.getMonth(), 1);
endDate = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 0);
break;
case "year":
startDate = new Date(now.getFullYear(), 0, 1);
endDate = new Date(startDate.getFullYear() + 1, 0, 0);
break;
case "":
default:
return; // empty period, don't update state
break;
} }
let p = availablePeriods[period];
this.setDateRange(startDate, endDate, period); this.setDateRange(p.start(), p.end(), period);
} }
@bind @bind
@ -135,9 +134,10 @@ class DatePicker extends Component {
} }
render(props, state) { render(props, state) {
const links = availablePeriods.map((p) => { const links = Object.keys(availablePeriods).map((id) => {
let className = ( p.id == state.period ) ? 'active' : ''; let p = availablePeriods[id];
return <li class={className} ><a href="#" data-value={p.id} onClick={this.setPeriod}>{p.label}</a></li> let className = ( id == state.period ) ? 'active' : '';
return <li class={className} ><a href="#" data-value={id} onClick={this.setPeriod}>{p.label}</a></li>
}); });
return ( return (

View File

@ -14,7 +14,7 @@ class Dashboard extends Component {
super(props) super(props)
this.state = { this.state = {
period: (window.location.hash.substring(2) || 'week'), period: (window.location.hash.substring(2) || 'last-7-days'),
before: 0, before: 0,
after: 0, after: 0,
} }