diff --git a/assets/js/dashboard/stats/graph/interval-picker.js b/assets/js/dashboard/stats/graph/interval-picker.js
index c7bcf4153..d01f3610f 100644
--- a/assets/js/dashboard/stats/graph/interval-picker.js
+++ b/assets/js/dashboard/stats/graph/interval-picker.js
@@ -6,7 +6,7 @@ import * as storage from '../../util/storage'
import { isKeyPressed } from '../../keybinding.js'
import { monthsBetweenDates } from '../../util/date.js'
-export const INTERVAL_LABELS = {
+const INTERVAL_LABELS = {
'minute': 'Minutes',
'hour': 'Hours',
'date': 'Days',
@@ -14,11 +14,19 @@ export const INTERVAL_LABELS = {
'month': 'Months'
}
-export const getStoredInterval = function(period, domain) {
+function validIntervals(site, query) {
+ if (query.period === "custom" && monthsBetweenDates(query.from, query.to) > 12) {
+ return ["week", "month"]
+ } else {
+ return site.validIntervalsByPeriod[query.period]
+ }
+}
+
+function getStoredInterval(period, domain) {
return storage.getItem(`interval__${period}__${domain}`)
}
-export const storeInterval = function(period, domain, interval) {
+function storeInterval(period, domain, interval) {
storage.setItem(`interval__${period}__${domain}`, interval)
}
@@ -33,37 +41,47 @@ function subscribeKeybinding(element) {
}, [handleKeyPress])
}
-function DropdownItem({ option, currentInterval, updateInterval }) {
- return (
-
updateInterval(option)} key={option} disabled={option == currentInterval}>
- {({ active }) => (
-
- {INTERVAL_LABELS[option]}
-
- )}
-
- )
+export const getCurrentInterval = function(site, query) {
+ const options = validIntervals(site, query)
+
+ const storedInterval = getStoredInterval(query.period, site.domain)
+ const defaultInterval = [...options].pop()
+
+ if (storedInterval && options.includes(storedInterval)) {
+ return storedInterval
+ } else {
+ return defaultInterval
+ }
}
-export function IntervalPicker({ graphData, query, site, updateInterval }) {
+export function IntervalPicker({ query, site, onIntervalUpdate }) {
if (query.period == 'realtime') return null
-
+
const menuElement = React.useRef(null)
- subscribeKeybinding(menuElement)
+ const options = validIntervals(site, query)
+ const currentInterval = getCurrentInterval(site, query)
- let currentInterval = graphData?.interval
+ subscribeKeybinding(menuElement)
- let options = site.validIntervalsByPeriod[query.period]
- if (query.period === "custom" && monthsBetweenDates(query.from, query.to) > 12) {
- options = ["week", "month"]
+ function updateInterval(interval) {
+ storeInterval(query.period, site.domain, interval)
+ onIntervalUpdate(interval)
}
- if (!options.includes(currentInterval)) {
- currentInterval = [...options].pop()
+ function renderDropdownItem(option) {
+ return (
+ updateInterval(option)} key={option} disabled={option == currentInterval}>
+ {({ active }) => (
+
+ {INTERVAL_LABELS[option]}
+
+ )}
+
+ )
}
return (
@@ -85,7 +103,7 @@ export function IntervalPicker({ graphData, query, site, updateInterval }) {
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95">
- {options.map((option) => DropdownItem({ option, currentInterval, updateInterval }))}
+ {options.map(renderDropdownItem)}
>
diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js
index caf0eee1c..1a11e105f 100644
--- a/assets/js/dashboard/stats/graph/visitor-graph.js
+++ b/assets/js/dashboard/stats/graph/visitor-graph.js
@@ -5,10 +5,10 @@ import * as storage from '../../util/storage'
import LazyLoader from '../../components/lazy-loader'
import { METRIC_MAPPING, LoadingState } from './graph-util'
import TopStats from './top-stats';
-import { IntervalPicker, getStoredInterval, storeInterval } from './interval-picker';
+import { IntervalPicker, getCurrentInterval } from './interval-picker';
import FadeIn from '../../fade-in';
import * as url from '../../util/url'
-import { monthsBetweenDates, parseNaiveDate, isBefore } from '../../util/date'
+import { parseNaiveDate, isBefore } from '../../util/date'
import { isComparisonEnabled } from '../../comparison-input'
import LineGraphWithRouter from './line-graph'
@@ -25,37 +25,12 @@ export default class VisitorGraph extends React.Component {
this.updateMetric = this.updateMetric.bind(this)
this.fetchTopStatData = this.fetchTopStatData.bind(this)
this.fetchGraphData = this.fetchGraphData.bind(this)
- this.updateInterval = this.updateInterval.bind(this)
+ this.onIntervalUpdate = this.onIntervalUpdate.bind(this)
this.boundary = React.createRef()
}
- isIntervalValid(interval) {
- const { query, site } = this.props
- const validIntervals = site.validIntervalsByPeriod[query.period] || []
-
- return validIntervals.includes(interval)
- }
-
- getIntervalFromStorage() {
- const { query, site } = this.props
- let interval = getStoredInterval(query.period, site.domain)
-
- if (interval !== "week" && interval !== "month" && query.period === "custom" && monthsBetweenDates(query.from, query.to) > 12) {
- interval = "month"
- }
-
- if (this.isIntervalValid(interval)) {
- return interval
- } else {
- return null
- }
- }
-
- updateInterval(interval) {
- if (this.isIntervalValid(interval)) {
- storeInterval(this.props.query.period, this.props.site.domain, interval)
- this.setState({ mainGraphLoadingState: LoadingState.refreshing, graphData: null }, this.fetchGraphData)
- }
+ onIntervalUpdate(interval) {
+ this.setState({ mainGraphLoadingState: LoadingState.refreshing, graphData: null }, () => this.fetchGraphData(interval))
}
onVisible() {
@@ -114,11 +89,13 @@ export default class VisitorGraph extends React.Component {
this.setState({ metric: clickedMetric, graphData: null })
}
- fetchGraphData() {
- const url = `/api/stats/${encodeURIComponent(this.props.site.domain)}/main-graph`
- let params = { metric: this.state.metric }
- const interval = this.getIntervalFromStorage()
- if (interval) { params.interval = interval }
+ fetchGraphData(interval) {
+ const { site, query } = this.props
+ const url = `/api/stats/${encodeURIComponent(site.domain)}/main-graph`
+ const params = {
+ metric: this.state.metric,
+ interval: interval || getCurrentInterval(site, query)
+ }
api.get(url, this.props.query, params)
.then((res) => {
@@ -272,7 +249,7 @@ export default class VisitorGraph extends React.Component {
{this.downloadLink()}
{this.samplingNotice()}
{this.importedNotice()}
-
+