Fix date picker shifting day bug (#2848)

* Fix date picker shifting day bug

This commit fixes a bug where picking a specific date in custom range
takes you to the stats of the day before.

This was caused because Flatpickr returns a Date instance, and we were
trying to parse this Date instance into day.js using the `utc` function,
assuming it was a ISO-8601 string.

This commit fixes it by casting Date into dayjs using the `dayjs`
function instead of `utc`.

* Fix invalid date Flatpickr bug

This commit fixes a bug where the Flatpickr was not setting the minDate
correctly based on statsBegin. This was failing because parseUTCDate is
now returning a day.js instance, and Flatpickr cannot handle that type.

* Hide dayjs usage in util/date
pull/2877/head
Vini Brasil 4 years ago committed by GitHub
parent 70dc8d71c0
commit 0cfbcedd84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,7 +6,7 @@ import { ChevronDownIcon } from '@heroicons/react/20/solid'
import classNames from 'classnames'
import * as storage from './util/storage'
import Flatpickr from 'react-flatpickr'
import { formatISO, parseUTCDate, formatDateRange } from './util/date.js'
import { parseNaiveDate, formatISO, formatDateRange } from './util/date.js'
const COMPARISON_MODES = {
'off': 'Disable comparison',
@ -140,12 +140,16 @@ const ComparisonInput = function({ site, query, history }) {
mode: 'range',
showMonths: 1,
maxDate: 'today',
minDate: parseUTCDate(site.statsBegin),
minDate: site.statsBegin,
animate: true,
static: true,
onClose: ([from, to], _dateStr, _instance) => {
setUiMode("menu")
if (from && to) updateMode("custom", formatISO(parseUTCDate(from)), formatISO(parseUTCDate(to)))
if (from && to) {
[from, to] = [parseNaiveDate(from), parseNaiveDate(to)]
updateMode("custom", formatISO(from), formatISO(to))
}
}
}

@ -17,6 +17,7 @@ import {
isThisMonth,
isThisYear,
parseUTCDate,
parseNaiveDate,
isBefore,
isAfter,
formatDateRange
@ -254,34 +255,18 @@ function DatePicker({query, site, history}) {
return () => { document.removeEventListener("mousedown", handleClick, false); }
}, [])
function setCustomDate(dates) {
if (dates.length === 2) {
const [from, to] = dates.map(parseUTCDate)
if (formatISO(from) === formatISO(to)) {
navigateToQuery(
history,
query,
{
period: 'day',
date: formatISO(from),
from: false,
to: false
}
)
function setCustomDate([from, to], _dateStr, _instance) {
if (from && to) {
[from, to] = [parseNaiveDate(from), parseNaiveDate(to)]
if (from.isSame(to)) {
navigateToQuery( history, query, { period: 'day', date: formatISO(from), from: false, to: false })
} else {
navigateToQuery(
history,
query,
{
period: 'custom',
date: false,
from: formatISO(from),
to: formatISO(to),
}
)
navigateToQuery( history, query, { period: 'custom', date: false, from: formatISO(from), to: formatISO(to) })
}
setOpen(false)
}
setOpen(false)
}
function toggle() {
@ -400,13 +385,13 @@ function DatePicker({query, site, history}) {
options={{
mode: 'range',
maxDate: 'today',
minDate: parseUTCDate(site.statsBegin),
minDate: site.statsBegin,
showMonths: 1,
static: true,
animate: true}}
ref={calendar}
className="invisible"
onChange={setCustomDate}
onClose={setCustomDate}
/>
</div>
)

@ -63,6 +63,10 @@ export function parseUTCDate(dateString) {
return dayjs.utc(dateString)
}
export function parseNaiveDate(dateString) {
return dayjs(dateString)
}
export function nowForSite(site) {
return dayjs.utc().utcOffset(site.offset / 60)
}

Loading…
Cancel
Save