Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Implement QueryLink to make it easier to transition from Link

birjolaxewccc89ab4b12b
diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js
index c9a0ffb6f3..b53959ba26 100644
--- a/assets/js/dashboard/datepicker.js
+++ b/assets/js/dashboard/datepicker.js
@@ -3,7 +3,7 @@ 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 } from './query.js'
+import { navigateToQuery, QueryLink } from './query.js'
class DatePicker extends React.Component {
@@ -116,25 +116,14 @@ class DatePicker extends React.Component {
}
renderArrow(period, prevDate, nextDate) {
- const navigateGenerator = data => {
- return e => {
- e.preventDefault()
- navigateToQuery(
- this.props.history,
- this.props.query,
- data
- )
- }
- }
-
return (
<div className="flex rounded shadow bg-white mr-4 cursor-pointer">
- <Link to={{search: this.queryWithPeriod(period, {date: prevDate})}} onClick={navigateGenerator({date: prevDate})} className="flex items-center px-2 border-r border-gray-300">
+ <QueryLink history={this.props.history} query={this.props.query} data={{date: prevDate}} className="flex items-center px-2 border-r border-gray-300">
<svg className="feather h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="15 18 9 12 15 6"></polyline></svg>
- </Link>
- <Link to={{search: this.queryWithPeriod(period, {date: nextDate})}} onClick={navigateGenerator({date: nextDate})} className="flex items-center px-2">
+ </QueryLink>
+ <QueryLink history={this.props.history} query={this.props.query} data={{date: nextDate}} className="flex items-center px-2">
<svg className="feather h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="9 18 15 12 9 6"></polyline></svg>
- </Link>
+ </QueryLink>
</div>
)
}
@@ -202,23 +191,10 @@ class DatePicker extends React.Component {
if (opts.date) { opts.date = formatISO(opts.date) }
- const onClick = e => {
- e.preventDefault()
- navigateToQuery(
- this.props.history,
- this.props.query,
- {
- period,
- ...opts
- }
- )
- this.close()
- }
-
return (
- <Link to={{search: this.queryWithPeriod(period, opts)}} onClick={onClick} className={boldClass + ' block px-4 py-2 text-sm leading-tight hover:bg-gray-100 hover:text-gray-900'}>
+ <QueryLink history={this.props.history} query={this.props.query} data={{period, ...opts}} onClick={this.close.bind(this)} className={boldClass + ' block px-4 py-2 text-sm leading-tight hover:bg-gray-100 hover:text-gray-900'}>
{text}
- </Link>
+ </QueryLink>
)
}
diff --git a/assets/js/dashboard/query.js b/assets/js/dashboard/query.js
index 5c0f607457..4ed981cd28 100644
--- a/assets/js/dashboard/query.js
+++ b/assets/js/dashboard/query.js
@@ -1,3 +1,5 @@
+import React from 'react'
+import { Link } from 'react-router-dom'
import {formatDay, formatMonthYYYY, nowForSite, parseUTCDate} from './date'
const PERIODS = ['realtime', 'day', 'month', '7d', '30d', '6mo', '12mo', 'custom']
@@ -41,6 +43,19 @@ export function parseQuery(querystring, site) {
}
}
+function generateQueryString(data) {
+ const query = new URLSearchParams(window.location.search)
+ Object.keys(data).forEach(key => {
+ if (!data[key]) {
+ query.delete(key)
+ return
+ }
+
+ query.set(key, data[key])
+ })
+ return query.toString()
+}
+
export function navigateToQuery(history, queryFrom, newData) {
// if we update any data that we store in localstorage, make sure going back in history will revert them
if (newData.period && newData.period !== queryFrom.period) {
@@ -50,16 +65,25 @@ export function navigateToQuery(history, queryFrom, newData) {
}
// then push the new query to the history
- const query = new URLSearchParams(window.location.search)
- Object.keys(newData).forEach(key => {
- if (!newData[key]) {
- query.delete(key)
- return
- }
+ history.push({ search: generateQueryString(newData) })
+}
- query.set(key, newData[key])
- })
- history.push({ search: query.toString() })
+export class QueryLink extends React.Component {
+ constructor() {
+ super()
+ this.onClick = this.onClick.bind(this)
+ }
+
+ onClick(e) {
+ e.preventDefault()
+ navigateToQuery(this.props.history, this.props.query, this.props.data)
+ if (this.props.onClick) this.props.onClick(e)
+ }
+
+ render() {
+ const { history, query, data, ...props } = this.props
+ return <Link {...props} to={generateQueryString(data)} onClick={this.onClick} />
+ }
}
export function toHuman(query) {