fathom/assets/js/components/DatePicker.js

147 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-11-23 21:29:54 +01:00
'use strict';
2018-05-03 10:54:22 +02:00
import { h, Component } from 'preact';
2018-05-01 16:06:17 +02:00
import { bind } from 'decko';
import Pikadayer from './Pikadayer.js';
2016-11-23 21:29:54 +01:00
const availablePeriods = [
{
2018-05-01 16:06:17 +02:00
id: 'day',
label: 'Today'
},
{
2018-05-01 16:06:17 +02:00
id: 'week',
label: 'This week'
},
{
2018-05-01 16:06:17 +02:00
id: 'month',
label: 'This month'
},
{
2018-05-01 16:06:17 +02:00
id: 'year',
label: 'This year'
}
]
2016-11-23 21:29:54 +01:00
class DatePicker extends Component {
constructor(props) {
super(props)
this.state = {
2018-05-03 10:54:22 +02:00
period: props.value,
before: 0,
after: 0,
2016-11-23 21:29:54 +01:00
}
this.updateDatesFromPeriod(this.state.period)
}
@bind
updateDatesFromPeriod(period) {
let startDate = new Date();
startDate.setHours(0);
startDate.setMinutes(0);
let endDate = new Date();
endDate.setHours(24);
endDate.setMinutes(0);
switch(period) {
case "week":
startDate.setDate(startDate.getDate() - (startDate.getDay() + 6) % 7);
endDate.setDate(startDate.getDate() + 7);
break;
case "month":
startDate.setDate(1);
endDate.setMonth(endDate.getMonth() + 1);
endDate.setDate(0);
break;
case "year":
startDate.setDate(1);
startDate.setMonth(0);
endDate.setYear(startDate.getFullYear() + 1);
endDate.setMonth(0);
endDate.setDate(0);
break;
}
this.setDateRange(startDate, endDate, period);
}
@bind
setDateRange(startDate, endDate, period) {
// don't update state if start > end. user may be busy picking dates.
// todo: show error
if(startDate > endDate) {
return;
}
const timezoneOffset = (new Date()).getTimezoneOffset() * 60;
let before, after;
before = Math.round(((+endDate) / 1000) - timezoneOffset);
after = Math.round(((+startDate) / 1000) - timezoneOffset);
this.setState({
period: period || '',
startDate: startDate,
endDate: endDate,
before: before,
after: after,
picking: '',
});
this.props.onChange(this.state);
}
2018-05-01 16:06:17 +02:00
@bind
2016-11-23 21:29:54 +01:00
setPeriod(e) {
2018-05-01 16:06:17 +02:00
e.preventDefault();
2018-05-03 10:54:22 +02:00
let newPeriod = e.target.dataset.value;
if( newPeriod === this.state.period) {
return;
}
2018-05-03 10:54:22 +02:00
this.updateDatesFromPeriod(newPeriod);
2016-11-23 21:29:54 +01:00
}
2018-05-16 12:57:03 +02:00
dateValue(date) {
const addZero = function(n){return n<10? '0'+n:''+n;}
return date.getFullYear() + '-' + addZero(date.getMonth() + 1) + '-' + addZero(date.getDate());
2018-05-16 12:57:03 +02:00
}
@bind
setStartDate(date) {
if(date) {
this.setDateRange(date, this.state.endDate, '')
}
2018-05-16 12:57:03 +02:00
}
@bind
setEndDate(date) {
if(date) {
this.setDateRange(this.state.startDate, date, '')
}
2018-05-16 12:57:03 +02:00
}
2018-05-01 16:06:17 +02:00
render(props, state) {
const links = availablePeriods.map((p) => {
let className = ( p.id == state.period ) ? 'active' : '';
return <li class={className} ><a href="#" data-value={p.id} onClick={this.setPeriod}>{p.label}</a></li>
2016-11-23 21:29:54 +01:00
});
return (
2018-05-01 16:06:17 +02:00
<ul>
{links}
<li class="custom">
<Pikadayer value={this.dateValue(state.startDate)} onSelect={this.setStartDate} />
<span style="margin: 0 8px"> to </span>
<Pikadayer value={this.dateValue(state.endDate)} onSelect={this.setEndDate} />
2018-05-16 12:57:03 +02:00
</li>
2018-05-01 16:06:17 +02:00
</ul>
2016-11-23 21:29:54 +01:00
)
}
}
export default DatePicker