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

Commit e8f22c0

Browse files
committed
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
1 parent 2c7fabd commit e8f22c0

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/store/Store.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { route } from 'preact-router';
22
import mitt from 'mitt';
33
import { parentCall } from '../lib/parentCall';
44
import { loadConfig } from '../lib/main';
5+
import queryString from 'query-string';
56

67
const { localStorage, sessionStorage } = window;
78

@@ -15,7 +16,10 @@ export default class Store {
1516
let storedState;
1617

1718
try {
18-
storedState = JSON.parse(localStorage.getItem(this.localStorageKey));
19+
const reset = queryString.parse(window.location.search).reset === 'true';
20+
if (!reset) {
21+
storedState = JSON.parse(localStorage.getItem(this.localStorageKey));
22+
}
1923
} catch (e) {
2024
storedState = {};
2125
} finally {
@@ -49,7 +53,7 @@ export default class Store {
4953
});
5054

5155
window.addEventListener('visibilitychange', () => {
52-
!this._state.minimized && !this._state.triggered && parentCall('openWidget');
56+
!this._state.minimized && parentCall('openWidget');
5357
this._state.iframe.visible ? parentCall('showWidget') : parentCall('hideWidget');
5458
});
5559

@@ -91,8 +95,9 @@ export default class Store {
9195
}
9296

9397
resetState() {
94-
this._state = this._initialState;
95-
loadConfig();
96-
route('');
98+
window.addEventListener('beforeunload', () => {
99+
localStorage.removeItem(this.localStorageKey);
100+
});
101+
document.location.href = document.location.href;
97102
}
98103
}

0 commit comments

Comments
 (0)