From d8aa06d999f6f58583e68787243f56248b1c8723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Mon, 2 May 2022 22:33:30 +0200 Subject: [PATCH] Allow to access the dashboard with a given site ID Closes #217 --- assets/src/js/components/DatePicker.js | 19 +++------- assets/src/js/components/SiteSwitcher.js | 15 +++++++- assets/src/js/lib/util.js | 36 +++++++++++++------ assets/src/js/pages/dashboard.js | 46 ++++++++++++------------ package-lock.json | 1 - 5 files changed, 67 insertions(+), 50 deletions(-) diff --git a/assets/src/js/components/DatePicker.js b/assets/src/js/components/DatePicker.js index cf3922c7..05e8823d 100644 --- a/assets/src/js/components/DatePicker.js +++ b/assets/src/js/components/DatePicker.js @@ -4,6 +4,7 @@ import { h, Component } from 'preact'; import { bind } from 'decko'; import Pikadayer from './Pikadayer.js'; import classNames from 'classnames'; +import {hashParams} from "../lib/util"; const padZero = (n) => n < 10 ? '0'+n : ''+n; @@ -81,19 +82,6 @@ const availablePeriods = { } } -function hashParams() { - var params = {}, - match, - matches = window.location.hash.substring(2).split("&"); - - for(var i=0; i s.charAt(Math.floor(Math.random() * s.length))).join(''); + var s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + return Array(n).join().split(',').map(() => s.charAt(Math.floor(Math.random() * s.length))).join(''); } -export { - randomString, - stringifyObject +function hashParams() { + var params = {}, + match, + matches = window.location.hash.substring(2).split("&"); + + for (var i = 0; i < matches.length; i++) { + match = matches[i].split('=') + params[match[0]] = decodeURIComponent(match[1]); + } + + return params; +} + +export { + randomString, + stringifyObject, + hashParams } diff --git a/assets/src/js/pages/dashboard.js b/assets/src/js/pages/dashboard.js index 0748613b..ac5f9189 100644 --- a/assets/src/js/pages/dashboard.js +++ b/assets/src/js/pages/dashboard.js @@ -13,17 +13,19 @@ import Chart from '../components/Chart.js'; import { bind } from 'decko'; import Client from '../lib/client.js'; import classNames from 'classnames'; +import {hashParams} from "../lib/util"; -const defaultSite = { - id: window.localStorage.getItem('site_id') || 1, - name: "", - unsaved: true, -}; +let defaultSite = {} class Dashboard extends Component { constructor(props) { super(props) - + let params = hashParams() + defaultSite = { + id: params.site || window.localStorage.getItem('site_id') || 1, + name: "", + unsaved: true, + }; this.state = { dateRange: [], groupBy: 'day', @@ -39,10 +41,10 @@ class Dashboard extends Component { this.fetchSites() } - @bind + @bind fetchSites() { Client.request(`sites`) - .then((sites) => { + .then((sites) => { // open site settings when there are no sites yet if(sites.length == 0) { this.showSiteSettings({ id: 1, name: "yoursite.com", unsaved: true }) @@ -55,7 +57,7 @@ class Dashboard extends Component { site = s ? s : site; this.setState({ - sites: sites, + sites: sites, site: site, }) }).catch((e) => { @@ -67,18 +69,18 @@ class Dashboard extends Component { @bind changeDateRange(s) { - this.setState({ + this.setState({ dateRange: [ s.startDate, s.endDate ], period: s.period, groupBy: s.groupBy, }) } - @bind + @bind showSiteSettings(site) { site = site && site.unsaved ? site : this.state.site; - this.setState({ - settingsOpen: true, + this.setState({ + settingsOpen: true, site: site, previousSite: this.state.site, }) @@ -87,14 +89,14 @@ class Dashboard extends Component { @bind closeSiteSettings() { this.setState({ - settingsOpen: false, + settingsOpen: false, // switch back to previous site if we were showing site settings to add a new site site: this.state.site.unsaved && this.state.previousSite ? this.state.previousSite : this.state.site, }) } - @bind + @bind changeSelectedSite(site) { let newState = { site: site, @@ -102,7 +104,7 @@ class Dashboard extends Component { if(!this.state.site.unsaved) { newState.previousSite = this.state.site - } + } this.setState(newState) window.localStorage.setItem('site_id', site.id) @@ -115,9 +117,9 @@ class Dashboard extends Component { if(s.id != site.id) { return s; } - + updated = true; - + // replace site in sites array with parameter return site; }) @@ -129,13 +131,13 @@ class Dashboard extends Component { this.setState({sites: newSites, site: site}) } - @bind + @bind deleteSite(site) { let newSites = this.state.sites.filter((s) => (s.id != site.id)) let newSelectedSite = newSites.length > 0 ? newSites[0] : defaultSite; - this.setState({ - sites: newSites, - site: newSelectedSite + this.setState({ + sites: newSites, + site: newSelectedSite }) } diff --git a/package-lock.json b/package-lock.json index 971f1c09..4566dffe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,7 +4,6 @@ "requires": true, "packages": { "": { - "name": "fathom", "license": "MIT", "dependencies": { "classnames": "^2.2.6",