2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
2020-09-09 11:13:55 +03:00
|
|
|
import Transition from "../transition.js";
|
2020-03-04 18:24:18 +03:00
|
|
|
import { withRouter, Link } from 'react-router-dom'
|
|
|
|
import Flatpickr from "react-flatpickr";
|
2020-12-23 11:42:22 +03:00
|
|
|
import {shiftDays, shiftMonths, formatDay, formatDayShort, formatMonthYYYY, formatISO, isToday, lastMonth, nowForSite, isSameMonth, isThisMonth} from './date'
|
2020-11-13 03:26:53 +03:00
|
|
|
import { navigateToQuery, QueryLink } from './query.js'
|
2020-03-04 18:24:18 +03:00
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
class DatePicker extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.handleKeyup = this.handleKeyup.bind(this)
|
2020-03-04 18:24:18 +03:00
|
|
|
this.handleClick = this.handleClick.bind(this)
|
2020-09-09 11:13:55 +03:00
|
|
|
this.state = {mode: 'menu', open: false}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-03-04 18:24:18 +03:00
|
|
|
document.addEventListener('keyup', this.handleKeyup);
|
|
|
|
document.addEventListener('mousedown', this.handleClick, false);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener("keyup", this.handleKeyup);
|
2020-03-04 18:24:18 +03:00
|
|
|
document.removeEventListener('mousedown', this.handleClick, false);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyup(e) {
|
|
|
|
const {query, history} = this.props
|
|
|
|
|
2020-10-19 10:56:03 +03:00
|
|
|
if (e.ctrlKey || e.ctrlKey || e.altKey) return
|
|
|
|
|
2020-11-11 13:30:29 +03:00
|
|
|
const newSearch = {
|
|
|
|
period: false,
|
|
|
|
from: false,
|
|
|
|
to: false,
|
|
|
|
date: false
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
if (e.key === 'ArrowLeft') {
|
|
|
|
if (query.period === 'day') {
|
|
|
|
const prevDate = formatISO(shiftDays(query.date, -1))
|
2020-11-11 13:30:29 +03:00
|
|
|
newSearch.period = 'day'
|
|
|
|
newSearch.date = prevDate
|
2019-11-19 07:30:42 +03:00
|
|
|
} else if (query.period === 'month') {
|
|
|
|
const prevMonth = formatISO(shiftMonths(query.date, -1))
|
2020-11-11 13:30:29 +03:00
|
|
|
newSearch.period = 'month'
|
|
|
|
newSearch.date = prevMonth
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
} else if (e.key === 'ArrowRight') {
|
|
|
|
if (query.period === 'day') {
|
|
|
|
const nextDate = formatISO(shiftDays(query.date, 1))
|
2020-11-11 13:30:29 +03:00
|
|
|
newSearch.period = 'day'
|
|
|
|
newSearch.date = nextDate
|
2019-11-19 07:30:42 +03:00
|
|
|
} else if (query.period === 'month') {
|
|
|
|
const nextMonth = formatISO(shiftMonths(query.date, 1))
|
2020-11-11 13:30:29 +03:00
|
|
|
newSearch.period = 'month'
|
|
|
|
newSearch.date = nextMonth
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 13:30:29 +03:00
|
|
|
|
|
|
|
if (newSearch.date) {
|
|
|
|
navigateToQuery(
|
|
|
|
history,
|
|
|
|
query,
|
|
|
|
newSearch
|
|
|
|
)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
handleClick(e) {
|
|
|
|
if (this.dropDownNode && this.dropDownNode.contains(e.target)) return;
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
this.setState({open: false})
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
timeFrameText() {
|
|
|
|
const {query, site} = this.props
|
|
|
|
|
|
|
|
if (query.period === 'day') {
|
|
|
|
if (isToday(site, query.date)) {
|
|
|
|
return 'Today'
|
|
|
|
} else {
|
|
|
|
return formatDay(query.date)
|
|
|
|
}
|
|
|
|
} else if (query.period === '7d') {
|
|
|
|
return 'Last 7 days'
|
|
|
|
} else if (query.period === '30d') {
|
|
|
|
return 'Last 30 days'
|
|
|
|
} else if (query.period === 'month') {
|
2020-12-23 11:42:22 +03:00
|
|
|
if (isThisMonth(site, query.date)) {
|
|
|
|
return 'Month to Date'
|
|
|
|
} else {
|
|
|
|
return formatMonthYYYY(query.date)
|
|
|
|
}
|
2020-01-08 12:25:46 +03:00
|
|
|
} else if (query.period === '6mo') {
|
|
|
|
return 'Last 6 months'
|
2020-01-08 12:23:03 +03:00
|
|
|
} else if (query.period === '12mo') {
|
2020-01-08 12:31:58 +03:00
|
|
|
return 'Last 12 months'
|
2020-07-14 16:52:26 +03:00
|
|
|
} else if (query.period === 'realtime') {
|
|
|
|
return 'Realtime'
|
2020-03-04 18:24:18 +03:00
|
|
|
} else if (query.period === 'custom') {
|
|
|
|
return `${formatDayShort(query.from)} - ${formatDayShort(query.to)}`
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderArrow(period, prevDate, nextDate) {
|
|
|
|
return (
|
2020-12-29 12:00:41 +03:00
|
|
|
<div className="flex rounded shadow bg-white dark:bg-gray-800 mr-2 cursor-pointer">
|
2020-12-16 12:57:28 +03:00
|
|
|
<QueryLink to={{date: prevDate}} query={this.props.query} className="flex items-center px-2 border-r border-gray-300 dark:border-gray-500 dark:text-gray-100">
|
2020-07-21 10:30:03 +03:00
|
|
|
<svg className="feather h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="15 18 9 12 15 6"></polyline></svg>
|
2020-11-13 03:26:53 +03:00
|
|
|
</QueryLink>
|
2020-12-16 12:57:28 +03:00
|
|
|
<QueryLink to={{date: nextDate}} query={this.props.query} className="flex items-center px-2 dark:text-gray-100">
|
2020-07-21 10:30:03 +03:00
|
|
|
<svg className="feather h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="9 18 15 12 9 6"></polyline></svg>
|
2020-11-13 03:26:53 +03:00
|
|
|
</QueryLink>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderArrows() {
|
|
|
|
const {query} = this.props
|
|
|
|
|
|
|
|
if (query.period === 'month') {
|
|
|
|
const prevDate = formatISO(shiftMonths(query.date, -1))
|
|
|
|
const nextDate = formatISO(shiftMonths(query.date, 1))
|
|
|
|
|
|
|
|
return this.renderArrow('month', prevDate, nextDate)
|
|
|
|
} else if (query.period === 'day') {
|
|
|
|
const prevDate = formatISO(shiftDays(query.date, -1))
|
|
|
|
const nextDate = formatISO(shiftDays(query.date, 1))
|
|
|
|
|
|
|
|
return this.renderArrow('day', prevDate, nextDate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
open() {
|
2020-09-09 11:13:55 +03:00
|
|
|
this.setState({mode: 'menu', open: true})
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
renderDropDown() {
|
|
|
|
return (
|
2020-12-29 12:00:41 +03:00
|
|
|
<div className="relative" style={{height: '35.5px', width: '170px'}} ref={node => this.dropDownNode = node}>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div onClick={this.open.bind(this)} className="flex items-center justify-between rounded bg-white dark:bg-gray-800 shadow px-4 pr-3 py-2 leading-tight cursor-pointer text-sm font-medium text-gray-800 dark:text-gray-200 h-full">
|
2019-11-19 07:30:42 +03:00
|
|
|
<span className="mr-2">{this.timeFrameText()}</span>
|
2020-09-09 11:13:55 +03:00
|
|
|
<svg className="text-pink-500 h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
|
|
|
<polyline points="6 9 12 15 18 9"></polyline>
|
|
|
|
</svg>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
<Transition
|
|
|
|
show={this.state.open}
|
|
|
|
enter="transition ease-out duration-100 transform"
|
|
|
|
enterFrom="opacity-0 scale-95"
|
|
|
|
enterTo="opacity-100 scale-100"
|
|
|
|
leave="transition ease-in duration-75 transform"
|
|
|
|
leaveFrom="opacity-100 scale-100"
|
|
|
|
leaveTo="opacity-0 scale-95"
|
|
|
|
>
|
|
|
|
{this.renderDropDownContent()}
|
|
|
|
</Transition>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
close() {
|
2020-09-09 11:13:55 +03:00
|
|
|
this.setState({open: false})
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
renderLink(period, text, opts = {}) {
|
2020-03-04 18:24:18 +03:00
|
|
|
const {query, site} = this.props
|
|
|
|
let boldClass;
|
|
|
|
if (query.period === 'day' && period === 'day') {
|
|
|
|
boldClass = isToday(site, query.date) ? 'font-bold' : ''
|
2020-09-09 11:13:55 +03:00
|
|
|
} else if (query.period === 'month' && period === 'month') {
|
|
|
|
const linkDate = opts.date || nowForSite(site)
|
|
|
|
boldClass = isSameMonth(linkDate, query.date) ? 'font-bold' : ''
|
2020-03-04 18:24:18 +03:00
|
|
|
} else {
|
|
|
|
boldClass = query.period === period ? 'font-bold' : ''
|
|
|
|
}
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
if (opts.date) { opts.date = formatISO(opts.date) }
|
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
return (
|
2020-12-29 12:00:41 +03:00
|
|
|
<QueryLink to={{from: false, to: false, date: false, period, ...opts}} onClick={this.close.bind(this)} query={this.props.query} className={boldClass + ' block px-4 py-2 md:text-sm leading-tight hover:bg-gray-100 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100'}>
|
2020-03-04 18:24:18 +03:00
|
|
|
{text}
|
2020-11-13 03:26:53 +03:00
|
|
|
</QueryLink>
|
2020-03-04 18:24:18 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDropDownContent() {
|
2020-09-09 11:13:55 +03:00
|
|
|
if (this.state.mode === 'menu') {
|
2020-03-04 18:24:18 +03:00
|
|
|
return (
|
2020-12-29 12:00:41 +03:00
|
|
|
<div className="absolute mt-2 rounded shadow-md z-10" style={{width: '235px', right: '-5px'}}>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="rounded bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 font-medium text-gray-800 dark:text-gray-200">
|
2020-03-04 18:24:18 +03:00
|
|
|
<div className="py-1">
|
|
|
|
{ this.renderLink('day', 'Today') }
|
2020-07-14 16:52:26 +03:00
|
|
|
{ this.renderLink('realtime', 'Realtime') }
|
2020-03-04 18:24:18 +03:00
|
|
|
</div>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-03-04 18:24:18 +03:00
|
|
|
<div className="py-1">
|
|
|
|
{ this.renderLink('7d', 'Last 7 days') }
|
|
|
|
{ this.renderLink('30d', 'Last 30 days') }
|
2020-09-09 11:13:55 +03:00
|
|
|
</div>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-09-09 11:13:55 +03:00
|
|
|
<div className="py-1">
|
2020-12-23 11:42:22 +03:00
|
|
|
{ this.renderLink('month', 'Month to Date') }
|
2020-09-09 11:13:55 +03:00
|
|
|
{ this.renderLink('month', 'Last month', {date: lastMonth(this.props.site)}) }
|
2020-03-04 18:24:18 +03:00
|
|
|
</div>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-03-04 18:24:18 +03:00
|
|
|
<div className="py-1">
|
|
|
|
{ this.renderLink('6mo', 'Last 6 months') }
|
|
|
|
{ this.renderLink('12mo', 'Last 12 months') }
|
|
|
|
</div>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-03-04 18:24:18 +03:00
|
|
|
<div className="py-1">
|
2020-12-29 12:00:41 +03:00
|
|
|
<span onClick={e => this.setState({mode: 'calendar'}, this.openCalendar.bind(this))} className="block px-4 py-2 md:text-sm leading-tight hover:bg-gray-100 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100 cursor-pointer">Custom range</span>
|
2020-03-04 18:24:18 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
} else if (this.state.mode === 'calendar') {
|
2020-12-16 12:52:47 +03:00
|
|
|
const insertionDate = new Date(this.props.site.insertedAt);
|
|
|
|
const dayBeforeCreation = insertionDate - 86400000;
|
2020-12-29 12:00:41 +03:00
|
|
|
return <Flatpickr options={{mode: 'range', maxDate: 'today', minDate: dayBeforeCreation, showMonths: 1, static: true, animate: true}} ref={calendar => this.calendar = calendar} className="invisible" onChange={this.setCustomDate.bind(this)} />
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setCustomDate(dates) {
|
|
|
|
if (dates.length === 2) {
|
|
|
|
const [from, to] = dates
|
2020-12-21 16:26:37 +03:00
|
|
|
if (formatISO(from) === formatISO(to)) {
|
|
|
|
navigateToQuery(
|
|
|
|
this.props.history,
|
|
|
|
this.props.query,
|
|
|
|
{
|
|
|
|
period: 'day',
|
|
|
|
date: formatISO(from),
|
|
|
|
from: false,
|
|
|
|
to: false,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
navigateToQuery(
|
|
|
|
this.props.history,
|
|
|
|
this.props.query,
|
|
|
|
{
|
|
|
|
period: 'custom',
|
|
|
|
date: false,
|
|
|
|
from: formatISO(from),
|
|
|
|
to: formatISO(to),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-09-09 11:13:55 +03:00
|
|
|
this.close()
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openCalendar() {
|
|
|
|
this.calendar && this.calendar.flatpickr.open()
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
render() {
|
|
|
|
return (
|
2020-12-29 12:00:41 +03:00
|
|
|
<div className="flex justify-end ml-auto pl-2">
|
2019-11-19 07:30:42 +03:00
|
|
|
{ this.renderArrows() }
|
|
|
|
{ this.renderDropDown() }
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(DatePicker)
|