Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

storing project view preferences in the local storage

Blue Mouseda31081b0f76
diff --git a/src/redux/actions/auth/index.js b/src/redux/actions/auth/index.js
index e6d490dad..d9a37e47e 100644
--- a/src/redux/actions/auth/index.js
+++ b/src/redux/actions/auth/index.js
@@ -1,5 +1,6 @@
import { removeItem } from 'utils/localstorage'
import { types } from './types'
+import { TOKEN, LS_VIEW_PREFS_SETTING } from 'redux/constants'
export const authActions = {
// Synchronous
@@ -31,8 +32,8 @@ export const authActions = {
},
logout() {
- removeItem('access_token')
- removeItem('user_info')
+ removeItem(TOKEN)
+ removeItem(LS_VIEW_PREFS_SETTING)
return {
type: types.LOGOUT
diff --git a/src/redux/constants/index.js b/src/redux/constants/index.js
index f2f17ebcb..91c239278 100644
--- a/src/redux/constants/index.js
+++ b/src/redux/constants/index.js
@@ -48,6 +48,7 @@ export const TOKEN = 'access_token'
export const TITLE_SUFFIX = '| Swetrix'
export const LS_THEME_SETTING = 'colour-theme'
+export const LS_VIEW_PREFS_SETTING = 'proj-view-preferences'
export const DONATE_URL = 'https://ko-fi.com/andriir'
export const FIREFOX_ADDON_URL = 'https://addons.mozilla.org/en-US/firefox/addon/swetrix/'
diff --git a/src/redux/reducers/ui/cache.js b/src/redux/reducers/ui/cache.js
index e907c0ad9..1eaa02e18 100644
--- a/src/redux/reducers/ui/cache.js
+++ b/src/redux/reducers/ui/cache.js
@@ -2,7 +2,22 @@ import { types } from 'redux/actions/ui/types'
import _filter from 'lodash/filter'
import _isEmpty from 'lodash/isEmpty'
-import { getProjectCacheKey } from 'redux/constants'
+import { getProjectCacheKey, LS_VIEW_PREFS_SETTING } from 'redux/constants'
+
+export const getInitialViewPrefs = () => {
+ if (typeof window !== 'undefined' && window.localStorage) {
+ const storedPrefs = window.localStorage.getItem(LS_VIEW_PREFS_SETTING)
+ if (typeof storedPrefs === 'string') {
+ try {
+ return JSON.parse(storedPrefs)
+ } catch (e) {
+ window.localStorage.removeItem(LS_VIEW_PREFS_SETTING)
+ }
+ }
+ }
+
+ return {}
+}
const getInitialState = () => {
return {
@@ -11,7 +26,7 @@ const getInitialState = () => {
analytics: {},
// { pid: { period: '7d', timeBucket: 'day' }, ... }
- projectViewPrefs: {},
+ projectViewPrefs: getInitialViewPrefs(),
}
}
diff --git a/src/redux/sagas/ui/index.js b/src/redux/sagas/ui/index.js
index da572f20d..b9f92bad5 100644
--- a/src/redux/sagas/ui/index.js
+++ b/src/redux/sagas/ui/index.js
@@ -6,6 +6,7 @@ import loadProjects from './load_projects'
import initialise from './initialise'
import liveVisitors from './worker_live_visitors'
// import generalStats from './worker_general_stats'
+import setProjectViewPrefs from './set_project_view_prefs'
import isEventsAvailable from './worker_is_events_available'
import logout from './logout'
@@ -14,6 +15,7 @@ function* mainUISaga() {
yield fork(liveVisitors)
yield fork(isEventsAvailable)
// yield fork(generalStats)
+ yield takeEvery(types.SET_PROJECT_VIEW_PREFS, setProjectViewPrefs)
yield takeEvery(types.LOAD_PROJECTS, loadProjects)
yield takeEvery(authTypes.LOGOUT, logout)
}
diff --git a/src/redux/sagas/ui/set_project_view_prefs.js b/src/redux/sagas/ui/set_project_view_prefs.js
new file mode 100644
index 000000000..7c00efda5
--- /dev/null
+++ b/src/redux/sagas/ui/set_project_view_prefs.js
@@ -0,0 +1,16 @@
+import _isEmpty from 'lodash/isEmpty'
+import { LS_VIEW_PREFS_SETTING } from 'redux/constants'
+import { getInitialViewPrefs } from 'redux/reducers/ui/cache'
+
+export default function* setProjectViewPrefs({ payload: { pid, period, timeBucket } }) {
+ if (typeof window !== 'undefined' && window.localStorage) {
+ const viewPrefs = {
+ ...getInitialViewPrefs(),
+ [pid]: {
+ period, timeBucket,
+ },
+ }
+
+ window.localStorage.setItem(LS_VIEW_PREFS_SETTING, JSON.stringify(viewPrefs))
+ }
+}