Improve datepicker (#41)

* Update datepicker dropdown style

* Add custom range for datepicker
pull/43/head
Uku Taht 7 years ago committed by GitHub
parent f892562a92
commit a2ed7514d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -255,3 +255,8 @@ a {
opacity: 1;
transition: opacity 100ms ease-in;
}
.flatpickr-calendar.static.open {
right: 2px;
top: calc(100% - 12px);
}

@ -12,6 +12,8 @@ function serialize(obj) {
export function serializeQuery(query, extraQuery=[]) {
query = Object.assign({}, query, {
date: query.date ? formatISO(query.date) : undefined,
from: query.from ? formatISO(query.from) : undefined,
to: query.to ? formatISO(query.to) : undefined,
filters: query.filters ? JSON.stringify(query.filters) : undefined
}, ...extraQuery)

@ -36,6 +36,10 @@ export function formatDay(date) {
return `${date.getDate()} ${formatMonth(date)}`;
}
export function formatDayShort(date) {
return `${date.getDate()} ${formatMonth(date).substring(0, 3)}`;
}
export function formatFullDate(date) {
const shortDate = formatMonth(date).substring(0, 3)
return `${shortDate} ${date.getDate()}, ${date.getFullYear()}`;

@ -1,28 +1,37 @@
import React from 'react';
import { withRouter } from 'react-router-dom'
import { Link } from 'react-router-dom'
import {shiftDays, shiftMonths, formatDay, formatMonthYYYY, formatISO, isToday} from './date'
import { withRouter, Link } from 'react-router-dom'
import Flatpickr from "react-flatpickr";
import "flatpickr/dist/themes/airbnb.css";
import {shiftDays, shiftMonths, formatDay, formatDayShort, formatMonthYYYY, formatISO, isToday} from './date'
class DatePicker extends React.Component {
constructor(props) {
super(props)
this.handleKeyup = this.handleKeyup.bind(this)
this.handleClick = this.handleClick.bind(this)
this.state = {mode: 'closed'}
}
componentDidMount() {
document.addEventListener("keyup", this.handleKeyup);
document.addEventListener('keyup', this.handleKeyup);
document.addEventListener('mousedown', this.handleClick, false);
}
componentWillUnmount() {
document.removeEventListener("keyup", this.handleKeyup);
document.removeEventListener('mousedown', this.handleClick, false);
}
queryWithPeriod(period, date) {
queryWithPeriod(period, dates) {
const query = new URLSearchParams(window.location.search)
query.set('period', period)
query.delete('date'); query.delete('from'); query.delete('to')
if (date) {
query.set('date', date)
if (dates) {
for (const key of Object.keys(dates)) {
query.set(key, dates[key])
}
} else {
query.delete('date')
}
@ -36,22 +45,28 @@ class DatePicker extends React.Component {
if (e.key === 'ArrowLeft') {
if (query.period === 'day') {
const prevDate = formatISO(shiftDays(query.date, -1))
history.push({search: this.queryWithPeriod('day', prevDate)})
history.push({search: this.queryWithPeriod('day', {date: prevDate})})
} else if (query.period === 'month') {
const prevMonth = formatISO(shiftMonths(query.date, -1))
history.push({search: this.queryWithPeriod('month', prevMonth)})
history.push({search: this.queryWithPeriod('month', {date: prevMonth})})
}
} else if (e.key === 'ArrowRight') {
if (query.period === 'day') {
const nextDate = formatISO(shiftDays(query.date, 1))
history.push({search: this.queryWithPeriod('day', nextDate)})
history.push({search: this.queryWithPeriod('day', {date: nextDate})})
} else if (query.period === 'month') {
const nextMonth = formatISO(shiftMonths(query.date, 1))
history.push({search: this.queryWithPeriod('month', nextMonth)})
history.push({search: this.queryWithPeriod('month', {date: nextMonth})})
}
}
}
handleClick(e) {
if (this.dropDownNode && this.dropDownNode.contains(e.target)) return;
this.setState({mode: 'closed'})
}
timeFrameText() {
const {query, site} = this.props
@ -73,18 +88,20 @@ class DatePicker extends React.Component {
return 'Last 6 months'
} else if (query.period === '12mo') {
return 'Last 12 months'
} else if (query.period === 'custom') {
return `${formatDayShort(query.from)} - ${formatDayShort(query.to)}`
}
}
renderArrow(period, prevDate, nextDate) {
return (
<div className="flex rounded shadow bg-white mr-4 cursor-pointer">
<Link to={{search: this.queryWithPeriod(period, prevDate)}} className="flex items-center px-2 border-r border-grey-light">
<Link to={{search: this.queryWithPeriod(period, {date: prevDate})}} className="flex items-center px-2 border-r border-grey-light">
<svg className="fill-current h-4 w-4" style={{transform: 'translateY(-2px)'}}>
<use xlinkHref="#feather-chevron-left" />
</svg>
</Link>
<Link to={{search: this.queryWithPeriod(period, nextDate)}} className="flex items-center px-2">
<Link to={{search: this.queryWithPeriod(period, {date: nextDate})}} className="flex items-center px-2">
<svg className="fill-current h-4 w-4" style={{transform: 'translateY(-2px)'}}>
<use xlinkHref="#feather-chevron-right" />
</svg>
@ -109,28 +126,87 @@ class DatePicker extends React.Component {
}
}
open() {
this.setState({mode: 'open'})
}
renderDropDown() {
return (
<div className="relative" style={{height: '35.5px', width: '190px'}}>
<div data-dropdown-trigger className="flex items-center justify-between hover:bg-grey-lighter rounded bg-white shadow px-4 pr-3 py-2 leading-tight cursor-pointer text-sm font-bold text-grey-darker h-full">
<div className="relative" style={{height: '35.5px', width: '190px'}} ref={node => this.dropDownNode = node}>
<div onClick={this.open.bind(this)} className="flex items-center justify-between rounded bg-white shadow px-4 pr-3 py-2 leading-tight cursor-pointer text-sm font-medium text-grey-darkest h-full">
<span className="mr-2">{this.timeFrameText()}</span>
<svg className="text-pink fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<use xlinkHref="#feather-chevron-down" />
</svg>
</div>
<div data-dropdown style={{top: '42px', right: '0px', width: '225px'}} className="dropdown-content hidden absolute pin-r bg-white border border-grey-light rounded shadow z-10 font-bold text-sm text-grey-darker">
<Link to={{search: this.queryWithPeriod('day')}} className="block p-2 hover:bg-grey-lighter">Today</Link>
<Link to={{search: this.queryWithPeriod('7d')}} className="block p-2 hover:bg-grey-lighter">Last 7 days</Link>
<Link to={{search: this.queryWithPeriod('30d')}} className="block p-2 hover:bg-grey-lighter">Last 30 days</Link>
<Link to={{search: this.queryWithPeriod('60d')}} className="block p-2 hover:bg-grey-lighter">Last 60 days</Link>
<Link to={{search: this.queryWithPeriod('6mo')}} className="block p-2 hover:bg-grey-lighter">Last 6 months</Link>
<Link to={{search: this.queryWithPeriod('12mo')}} className="block p-2 hover:bg-grey-lighter">Last 12 months</Link>
</div>
{this.renderDropDownContent()}
</div>
)
}
close() {
this.setState({mode: 'closed'})
}
renderLink(period, text) {
const {query, site} = this.props
let boldClass;
if (query.period === 'day' && period === 'day') {
boldClass = isToday(site, query.date) ? 'font-bold' : ''
} else {
boldClass = query.period === period ? 'font-bold' : ''
}
return (
<Link to={{search: this.queryWithPeriod(period)}} onClick={this.close.bind(this)} className={boldClass + ' block px-4 py-2 text-sm leading-tight hover:bg-grey-lightest hover:text-black'}>
{text}
</Link>
)
}
renderDropDownContent() {
if (this.state.mode === 'open') {
return (
<div className="absolute mt-2 rounded shadow-md z-10" style={{width: '235px', right: '-14px'}}>
<div className="rounded bg-white shadow-xs font-medium text-grey-darkest">
<div className="py-1">
{ this.renderLink('day', 'Today') }
</div>
<div className="border-t border-grey-lighter"></div>
<div className="py-1">
{ this.renderLink('7d', 'Last 7 days') }
{ this.renderLink('30d', 'Last 30 days') }
{ this.renderLink('60d', 'Last 60 days') }
</div>
<div className="border-t border-grey-lighter"></div>
<div className="py-1">
{ this.renderLink('6mo', 'Last 6 months') }
{ this.renderLink('12mo', 'Last 12 months') }
</div>
<div className="border-t border-grey-lighter"></div>
<div className="py-1">
<span onClick={e => this.setState({mode: 'calendar'}, this.openCalendar.bind(this))} className="block px-4 py-2 text-sm leading-tight hover:bg-grey-lightest hover:text-black cursor-pointer">Custom range</span>
</div>
</div>
</div>
)
} else if (this.state.mode === 'calendar') {
return <Flatpickr options={{mode: 'range', maxDate: 'today', showMonths: 1, static: true, animate: false}} ref={calendar => this.calendar = calendar} className="invisible" onChange={this.setCustomDate.bind(this)} />
}
}
setCustomDate(dates) {
if (dates.length === 2) {
const [from, to] = dates
this.props.history.push({search: this.queryWithPeriod('custom', {from: formatISO(from), to: formatISO(to)})})
}
}
openCalendar() {
this.calendar && this.calendar.flatpickr.open()
}
render() {
return (
<div className="flex justify-between sm:justify-between">

@ -1,6 +1,6 @@
import {formatDay, formatMonthYYYY, nowInOffset, parseUTCDate} from './date'
const PERIODS = ['day', 'month', '7d', '30d', '60d', '6mo', '12mo']
const PERIODS = ['day', 'month', '7d', '30d', '60d', '6mo', '12mo', 'custom']
export function parseQuery(querystring, site) {
const q = new URLSearchParams(querystring)
@ -8,7 +8,7 @@ export function parseQuery(querystring, site) {
const periodKey = 'period__' + site.domain
if (PERIODS.includes(period)) {
window.localStorage[periodKey] = period
if (period !== 'custom') window.localStorage[periodKey] = period
} else {
if (window.localStorage[periodKey]) {
period = window.localStorage[periodKey]
@ -20,9 +20,9 @@ export function parseQuery(querystring, site) {
return {
period: period,
date: q.get('date') ? parseUTCDate(q.get('date')) : nowInOffset(site.offset),
filters: {
'goal': q.get('goal')
}
from: q.get('from') ? parseUTCDate(q.get('from')) : undefined,
to: q.get('to') ? parseUTCDate(q.get('to')) : undefined,
filters: {'goal': q.get('goal')}
}
}

@ -149,7 +149,7 @@ class ReferrerDrilldownModal extends React.Component {
render() {
return (
<Modal site={this.props.site} show={!this.state.loading}>
<Modal site={this.props.site}>
{ this.renderBody() }
</Modal>
)

@ -49,7 +49,7 @@ export default class Pages extends React.Component {
<React.Fragment>
<div className="flex items-center mt-4 mb-2 justify-between text-grey-dark text-xs font-bold tracking-wide">
<span>Page url</span>
<span>Visitors</span>
<span>Pageviews</span>
</div>
{ this.state.pages.map(this.renderPage.bind(this)) }

@ -4,7 +4,6 @@ import Chart from 'chart.js'
import FadeIn from '../fade-in'
import { eventName } from '../query'
import numberFormatter from '../number-formatter'
import { isToday, shiftMonths, formatMonth } from '../date'
import * as api from '../api'
function mainSet(plot, present_index, ctx) {
@ -223,30 +222,6 @@ class LineGraph extends React.Component {
}
}
comparisonTimeframe() {
const {query, site} = this.props
if (query.period === 'day') {
if (isToday(site, query.date)) {
return 'yesterday'
} else {
return 'previous day'
}
} else if (query.period === 'month') {
return formatMonth(shiftMonths(query.date, -1))
} else if (query.period === '7d') {
return 'last week'
} else if (query.period === '30d') {
return 'last month'
} else if (query.period === '60d') {
return 'prev 60 days'
} else if (query.period === '6mo') {
return 'prev 6 months'
} else if (query.period === '12mo') {
return 'last year'
}
}
renderComparison(name, comparison) {
const formattedComparison = numberFormatter(Math.abs(comparison))

@ -3751,6 +3751,11 @@
"resolve-dir": "^1.0.1"
}
},
"flatpickr": {
"version": "4.6.3",
"resolved": "https://registry.npmjs.org/flatpickr/-/flatpickr-4.6.3.tgz",
"integrity": "sha512-007VucCkqNOMMb9ggRLNuJowwaJcyOh4sKAFcdGfahfGc7JQbf94zSzjdBq/wVyHWUEs5o3+idhFZ0wbZMRmVQ=="
},
"flush-write-stream": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/flush-write-stream/-/flush-write-stream-1.1.1.tgz",
@ -8546,6 +8551,15 @@
"scheduler": "^0.18.0"
}
},
"react-flatpickr": {
"version": "3.10.0",
"resolved": "https://registry.npmjs.org/react-flatpickr/-/react-flatpickr-3.10.0.tgz",
"integrity": "sha512-eeU5OlUZgWDyp2XvIeZ4+rI4T4VkqU1MdZUb+cXtTHfHYfLV+gpP0lAhRrij+QXlbPXTJFAbPerihohlOeTJ0A==",
"requires": {
"flatpickr": "^4.5.7",
"prop-types": "^15.5.10"
}
},
"react-is": {
"version": "16.11.0",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.11.0.tgz",

@ -13,6 +13,7 @@
"phoenix_html": "file:../deps/phoenix_html",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-flatpickr": "^3.10.0",
"react-router-dom": "^5.1.2",
"url-search-params-polyfill": "^7.0.1"
},

@ -39,11 +39,12 @@ module.exports = (env, options) => ({
}
]
},
externals: { moment: 'moment' },
plugins: [
new MiniCssExtractPlugin({ filename: '../css/app.css' }),
new MiniCssExtractPlugin({filename: '../css/[name].css'}),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }]),
new DefinePlugin({
"BASE_URL": JSON.stringify(process.env.BASE_URL || "http://localtest.me:8000")
}),
})
]
});

@ -110,6 +110,18 @@ defmodule Plausible.Stats.Query do
}
end
def from(_tz, %{"period" => "custom", "from" => from, "to" => to} = params) do
from_date = Date.from_iso8601!(from)
to_date = Date.from_iso8601!(to)
%__MODULE__{
period: "custom",
date_range: Date.range(from_date, to_date),
step_type: "date",
filters: parse_filters(params)
}
end
def from(tz, _) do
__MODULE__.from(tz, %{"period" => "30d"})
end

@ -27,5 +27,6 @@
<symbol id="feather-copy" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-copy"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></symbol>
<symbol id="feather-trash-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></symbol>
<symbol id="feather-calendar" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-calendar"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></symbol>
</defs>
</svg>

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

@ -8,6 +8,7 @@
<link rel="icon" type="image/png" sizes="32x32" href="<%= PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_favicon.png") %>">
<title><%= assigns[:title] || "Plausible · Simple, open-source web analytics" %></title>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/dashboard.css") %>"/>
<%= render("_tracking.html", assigns) %>
</head>
<body class="flex flex-col h-full bg-grey-lightest">

@ -56,6 +56,13 @@ defmodule Plausible.Stats.QueryTest do
assert q.step_type == "date"
end
test "parses custom format" do
q = Query.from(@tz, %{"period" => "custom", "from" => "2019-01-01", "to" => "2019-01-15"})
assert q.date_range.first == ~D[2019-01-01]
assert q.date_range.last == ~D[2019-01-15]
assert q.step_type == "date"
end
describe "filters" do
test "parses goal filter" do

Loading…
Cancel
Save