diff --git a/assets/.eslintrc.json b/assets/.eslintrc.json index 072c84aff..7ee18d54e 100644 --- a/assets/.eslintrc.json +++ b/assets/.eslintrc.json @@ -1,4 +1,7 @@ { + "env": { + "browser": true + }, "extends": ["airbnb", "prettier"], "plugins": ["prettier"], "rules": { @@ -9,6 +12,7 @@ "react/prop-types": [0], "max-classes-per-file": [0], "react/jsx-one-expression-per-line": [0], - "react/self-closing-comp": [0] + "react/self-closing-comp": [0], + "no-unused-expressions": [1, { "allowShortCircuit": true }] } } diff --git a/assets/js/dashboard/date.js b/assets/js/dashboard/date.js index f3f462a34..0470381c3 100644 --- a/assets/js/dashboard/date.js +++ b/assets/js/dashboard/date.js @@ -71,3 +71,31 @@ export function isToday(site, date) { export function isThisMonth(site, date) { return formatMonthYYYY(date) === formatMonthYYYY(nowForSite(site)) } + +export function isBefore(date1, date2, period) { + /* assumes 'day' and 'month' are the only valid periods */ + if (date1.getFullYear() !== date2.getFullYear()) { + return date1.getFullYear() < date2.getFullYear(); + } + if (date1.getMonth() !== date2.getMonth()) { + return date1.getMonth() < date2.getMonth(); + } + if (period === "month") { + return false; + } + return date1.getDate() < date2.getDate() +} + +export function isAfter(date1, date2, period) { + /* assumes 'day' and 'month' are the only valid periods */ + if (date1.getFullYear() !== date2.getFullYear()) { + return date1.getFullYear() > date2.getFullYear(); + } + if (date1.getMonth() !== date2.getMonth()) { + return date1.getMonth() > date2.getMonth(); + } + if (period === "month") { + return false; + } + return date1.getDate() > date2.getDate() +} diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index 181e5f3b6..151dd5436 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -1,147 +1,253 @@ -import React from 'react'; -import Transition from "../transition.js"; -import { withRouter, Link } from 'react-router-dom' +import React from "react"; +import { withRouter } from "react-router-dom"; import Flatpickr from "react-flatpickr"; -import {shiftDays, shiftMonths, formatDay, formatDayShort, formatMonthYYYY, formatISO, isToday, lastMonth, nowForSite, isSameMonth, isThisMonth} from './date' -import { navigateToQuery, QueryLink } from './query.js' - +import { + shiftDays, + shiftMonths, + formatDay, + formatDayShort, + formatMonthYYYY, + formatISO, + isToday, + lastMonth, + nowForSite, + isSameMonth, + isThisMonth, + parseUTCDate, + isBefore, + isAfter, +} from "./date"; +import Transition from "../transition"; +import { navigateToQuery, QueryLink, QueryButton } from "./query"; 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} + 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); + document.addEventListener("keyup", this.handleKeyup); + document.addEventListener("mousedown", this.handleClick, false); } componentWillUnmount() { document.removeEventListener("keyup", this.handleKeyup); - document.removeEventListener('mousedown', this.handleClick, false); + document.removeEventListener("mousedown", this.handleClick, false); } handleKeyup(e) { - const {query, history} = this.props + const { query, history } = this.props; - if (e.ctrlKey || e.ctrlKey || e.altKey) return + if (e.ctrlKey || e.ctrlKey || e.altKey) return; const newSearch = { period: false, from: false, to: false, - date: false - } + date: false, + }; + + const insertionDate = parseUTCDate(this.props.site.insertedAt); - 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 + if (e.key === "ArrowLeft") { + const prevDate = formatISO(shiftDays(query.date, -1)); + const prevMonth = formatISO(shiftMonths(query.date, -1)); + + if ( + query.period === "day" && + !isBefore(parseUTCDate(prevDate), insertionDate, query.period) + ) { + newSearch.period = "day"; + newSearch.date = prevDate; + } else if ( + query.period === "month" && + !isBefore(parseUTCDate(prevMonth), insertionDate, query.period) + ) { + 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 + } else if (e.key === "ArrowRight") { + const nextDate = formatISO(shiftDays(query.date, 1)); + const nextMonth = formatISO(shiftMonths(query.date, 1)); + + if ( + query.period === "day" && + !isAfter( + parseUTCDate(nextDate), + nowForSite(this.props.site), + query.period + ) + ) { + newSearch.period = "day"; + newSearch.date = nextDate; + } else if ( + query.period === "month" && + !isAfter( + parseUTCDate(nextMonth), + nowForSite(this.props.site), + query.period + ) + ) { + newSearch.period = "month"; + newSearch.date = nextMonth; } } if (newSearch.date) { - navigateToQuery( - history, - query, - newSearch - ) + navigateToQuery(history, query, newSearch); } } handleClick(e) { if (this.dropDownNode && this.dropDownNode.contains(e.target)) return; - this.setState({open: false}) + this.setState({ open: false }); } timeFrameText() { - const {query, site} = this.props + const { query, site } = this.props; - if (query.period === 'day') { + if (query.period === "day") { if (isToday(site, query.date)) { - return 'Today' - } else { - return formatDay(query.date) + return "Today"; } - } else if (query.period === '7d') { + return formatDay(query.date); + } if (query.period === '7d') { return 'Last 7 days' - } else if (query.period === '30d') { + } if (query.period === '30d') { return 'Last 30 days' - } else if (query.period === 'month') { + } if (query.period === 'month') { if (isThisMonth(site, query.date)) { return 'Month to Date' - } else { - return formatMonthYYYY(query.date) } - } else if (query.period === '6mo') { + return formatMonthYYYY(query.date) + } if (query.period === '6mo') { return 'Last 6 months' - } else if (query.period === '12mo') { + } if (query.period === '12mo') { return 'Last 12 months' - } else if (query.period === 'realtime') { - return 'Realtime' - } else if (query.period === 'custom') { + } if (query.period === 'custom') { return `${formatDayShort(query.from)} - ${formatDayShort(query.to)}` } + return 'Realtime' } renderArrow(period, prevDate, nextDate) { + const insertionDate = parseUTCDate(this.props.site.insertedAt); + const disabledLeft = isBefore( + parseUTCDate(prevDate), + insertionDate, + period + ); + const disabledRight = isAfter( + parseUTCDate(nextDate), + nowForSite(this.props.site), + period + ); + + const leftClasses = `flex items-center px-2 border-r border-gray-300 rounded-l + dark:border-gray-500 dark:text-gray-100 ${ + disabledLeft ? "bg-gray-200 dark:bg-gray-900" : "" + }`; + const rightClasses = `flex items-center px-2 rounded-r dark:text-gray-100 ${ + disabledRight ? "bg-gray-200 dark:bg-gray-900" : "" + }`; return ( -