Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Different approach to reset
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hanoii committed Aug 22, 2021
1 parent 2c7fabd commit e8f22c0
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/store/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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');
});

Expand Down Expand Up @@ -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;
}
}

0 comments on commit e8f22c0

Please sign in to comment.