From e8f22c0d19fa93c158574d1fcff70cbc32b879d6 Mon Sep 17 00:00:00 2001 From: Ariel Barreiro Date: Sun, 22 Aug 2021 18:10:34 -0300 Subject: [PATCH] Different approach to reset It was hard to really reset state, so I am simply clearing the localstorage and redirecting the iframe. Also provide a way to reset it on initialization through a querystring --- src/store/Store.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/store/Store.js b/src/store/Store.js index 73137c5c1..532f9d173 100644 --- a/src/store/Store.js +++ b/src/store/Store.js @@ -2,6 +2,7 @@ import { route } from 'preact-router'; import mitt from 'mitt'; import { parentCall } from '../lib/parentCall'; import { loadConfig } from '../lib/main'; +import queryString from 'query-string'; const { localStorage, sessionStorage } = window; @@ -15,7 +16,10 @@ export default class Store { let storedState; try { - storedState = JSON.parse(localStorage.getItem(this.localStorageKey)); + const reset = queryString.parse(window.location.search).reset === 'true'; + if (!reset) { + storedState = JSON.parse(localStorage.getItem(this.localStorageKey)); + } } catch (e) { storedState = {}; } finally { @@ -49,7 +53,7 @@ export default class Store { }); window.addEventListener('visibilitychange', () => { - !this._state.minimized && !this._state.triggered && parentCall('openWidget'); + !this._state.minimized && parentCall('openWidget'); this._state.iframe.visible ? parentCall('showWidget') : parentCall('hideWidget'); }); @@ -91,8 +95,9 @@ export default class Store { } resetState() { - this._state = this._initialState; - loadConfig(); - route(''); + window.addEventListener('beforeunload', () => { + localStorage.removeItem(this.localStorageKey); + }); + document.location.href = document.location.href; } }