import React from 'react'; import Transition from "../transition.js"; import { withRouter, Link } from 'react-router-dom' import Flatpickr from "react-flatpickr"; import {shiftDays, shiftMonths, formatDay, formatDayShort, formatMonthYYYY, formatISO, isToday, lastMonth, nowForSite, isSameMonth} from './date' import { navigateToQuery, QueryLink } from './query.js' class DatePicker extends React.Component { constructor(props) { super(props) this.handleKeyup = this.handleKeyup.bind(this) this.handleClick = this.handleClick.bind(this) this.state = {mode: 'menu', open: false} } componentDidMount() { document.addEventListener('keyup', this.handleKeyup); document.addEventListener('mousedown', this.handleClick, false); } componentWillUnmount() { document.removeEventListener("keyup", this.handleKeyup); document.removeEventListener('mousedown', this.handleClick, false); } handleKeyup(e) { const {query, history} = this.props if (e.ctrlKey || e.ctrlKey || e.altKey) return const newSearch = { period: false, from: false, to: false, date: false } if (e.key === 'ArrowLeft') { if (query.period === 'day') { const prevDate = formatISO(shiftDays(query.date, -1)) newSearch.period = 'day' newSearch.date = prevDate } else if (query.period === 'month') { const prevMonth = formatISO(shiftMonths(query.date, -1)) newSearch.period = 'month' newSearch.date = prevMonth } } else if (e.key === 'ArrowRight') { if (query.period === 'day') { const nextDate = formatISO(shiftDays(query.date, 1)) newSearch.period = 'day' newSearch.date = nextDate } else if (query.period === 'month') { const nextMonth = formatISO(shiftMonths(query.date, 1)) newSearch.period = 'month' newSearch.date = nextMonth } } if (newSearch.date) { navigateToQuery( history, query, newSearch ) } } handleClick(e) { if (this.dropDownNode && this.dropDownNode.contains(e.target)) return; this.setState({open: false}) } 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') { return formatMonthYYYY(query.date) } else if (query.period === '6mo') { return 'Last 6 months' } else if (query.period === '12mo') { return 'Last 12 months' } else if (query.period === 'realtime') { return 'Realtime' } else if (query.period === 'custom') { return `${formatDayShort(query.from)} - ${formatDayShort(query.to)}` } } renderArrow(period, prevDate, nextDate) { return (