diff --git a/assets/css/app.css b/assets/css/app.css index d091e34c2..bb89467fa 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -255,3 +255,8 @@ a { opacity: 1; transition: opacity 100ms ease-in; } + +.flatpickr-calendar.static.open { + right: 2px; + top: calc(100% - 12px); +} diff --git a/assets/js/dashboard/api.js b/assets/js/dashboard/api.js index 7e52eca30..b21d3b0c3 100644 --- a/assets/js/dashboard/api.js +++ b/assets/js/dashboard/api.js @@ -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) diff --git a/assets/js/dashboard/date.js b/assets/js/dashboard/date.js index 5def1013f..cc8b518a3 100644 --- a/assets/js/dashboard/date.js +++ b/assets/js/dashboard/date.js @@ -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()}`; diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index 44e85a229..62b65ee59 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -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 (
- + - + @@ -109,28 +126,87 @@ class DatePicker extends React.Component { } } + open() { + this.setState({mode: 'open'}) + } + renderDropDown() { return ( -
-
+
this.dropDownNode = node}> +
{this.timeFrameText()}
-
- Today - Last 7 days - Last 30 days - Last 60 days - Last 6 months - Last 12 months -
+ {this.renderDropDownContent()}
) } + 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 ( + + {text} + + ) + } + + renderDropDownContent() { + if (this.state.mode === 'open') { + return ( +
+
+
+ { this.renderLink('day', 'Today') } +
+
+
+ { this.renderLink('7d', 'Last 7 days') } + { this.renderLink('30d', 'Last 30 days') } + { this.renderLink('60d', 'Last 60 days') } +
+
+
+ { this.renderLink('6mo', 'Last 6 months') } + { this.renderLink('12mo', 'Last 12 months') } +
+
+
+ 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 +
+
+
+ ) + } else if (this.state.mode === 'calendar') { + return 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 (
diff --git a/assets/js/dashboard/query.js b/assets/js/dashboard/query.js index 7a3040a73..047e045e4 100644 --- a/assets/js/dashboard/query.js +++ b/assets/js/dashboard/query.js @@ -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')} } } diff --git a/assets/js/dashboard/stats/modals/referrer-drilldown.js b/assets/js/dashboard/stats/modals/referrer-drilldown.js index 1468561ba..61fd17519 100644 --- a/assets/js/dashboard/stats/modals/referrer-drilldown.js +++ b/assets/js/dashboard/stats/modals/referrer-drilldown.js @@ -149,7 +149,7 @@ class ReferrerDrilldownModal extends React.Component { render() { return ( - + { this.renderBody() } ) diff --git a/assets/js/dashboard/stats/pages.js b/assets/js/dashboard/stats/pages.js index 54eaa254b..532897a3f 100644 --- a/assets/js/dashboard/stats/pages.js +++ b/assets/js/dashboard/stats/pages.js @@ -49,7 +49,7 @@ export default class Pages extends React.Component {
Page url - Visitors + Pageviews
{ this.state.pages.map(this.renderPage.bind(this)) } diff --git a/assets/js/dashboard/stats/visitor-graph.js b/assets/js/dashboard/stats/visitor-graph.js index 3c6e31b5d..b292abf80 100644 --- a/assets/js/dashboard/stats/visitor-graph.js +++ b/assets/js/dashboard/stats/visitor-graph.js @@ -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)) diff --git a/assets/package-lock.json b/assets/package-lock.json index c67e2866d..07a54cee9 100644 --- a/assets/package-lock.json +++ b/assets/package-lock.json @@ -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", diff --git a/assets/package.json b/assets/package.json index 9d0051f9a..19ee46e0b 100644 --- a/assets/package.json +++ b/assets/package.json @@ -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" }, diff --git a/assets/webpack.config.js b/assets/webpack.config.js index 440c89afd..ae97513a8 100644 --- a/assets/webpack.config.js +++ b/assets/webpack.config.js @@ -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") - }), + }) ] }); diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex index 665244b28..cc3537344 100644 --- a/lib/plausible/stats/query.ex +++ b/lib/plausible/stats/query.ex @@ -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 diff --git a/lib/plausible_web/templates/layout/_svg_icons.html.eex b/lib/plausible_web/templates/layout/_svg_icons.html.eex index 2f0c42eab..48a05b1cc 100644 --- a/lib/plausible_web/templates/layout/_svg_icons.html.eex +++ b/lib/plausible_web/templates/layout/_svg_icons.html.eex @@ -27,5 +27,6 @@ + diff --git a/lib/plausible_web/templates/layout/app.html.eex b/lib/plausible_web/templates/layout/app.html.eex index 9c5cc3ddd..93a414508 100644 --- a/lib/plausible_web/templates/layout/app.html.eex +++ b/lib/plausible_web/templates/layout/app.html.eex @@ -8,6 +8,7 @@ "> <%= assigns[:title] || "Plausible ยท Simple, open-source web analytics" %> "/> + "/> <%= render("_tracking.html", assigns) %> diff --git a/test/plausible/stats/query_test.exs b/test/plausible/stats/query_test.exs index 0c304a7af..300515adb 100644 --- a/test/plausible/stats/query_test.exs +++ b/test/plausible/stats/query_test.exs @@ -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