fix: wrong reset state on exit

This commit is contained in:
Stefano Brilli 2021-04-11 12:54:47 +02:00
parent 509ce2f62f
commit f4be9118e1
6 changed files with 30 additions and 22 deletions

View File

@ -88,7 +88,7 @@ export const TopMenu = function(props: { onClick?: () => void }) {
}, [dispatch, handleMenuClose, discTitle]); }, [dispatch, handleMenuClose, discTitle]);
const handleExit = useCallback(() => { const handleExit = useCallback(() => {
dispatch(appActions.setState('WELCOME')); dispatch(appActions.setMainView('WELCOME'));
handleMenuClose(); handleMenuClose();
}, [dispatch, handleMenuClose]); }, [dispatch, handleMenuClose]);
@ -196,7 +196,7 @@ export const TopMenu = function(props: { onClick?: () => void }) {
ref={helpLinkRef} ref={helpLinkRef}
onClick={handleHelpLink} onClick={handleHelpLink}
> >
Support and FAQs Support and FAQ
</Link> </Link>
</ListItemText> </ListItemText>
</MenuItem> </MenuItem>

View File

@ -68,7 +68,7 @@ export const W95App = () => {
const [isMenuOpen, setMenuOpen] = useState(false); const [isMenuOpen, setMenuOpen] = useState(false);
const handleExit = useCallback(() => { const handleExit = useCallback(() => {
dispatch(appActions.setState('WELCOME')); dispatch(appActions.setMainView('WELCOME'));
}, [dispatch]); }, [dispatch]);
const closeMenu = useCallback(() => { const closeMenu = useCallback(() => {

View File

@ -36,7 +36,7 @@ serviceRegistry.mediaRecorderService = new MediaRecorderService();
if (navigator && navigator.usb) { if (navigator && navigator.usb) {
navigator.usb.ondisconnect = function() { navigator.usb.ondisconnect = function() {
store.dispatch(appActions.setState('WELCOME')); store.dispatch(appActions.setMainView('WELCOME'));
}; };
} else { } else {
store.dispatch(appActions.setBrowserSupported(false)); store.dispatch(appActions.setBrowserSupported(false));

View File

@ -56,7 +56,7 @@ export function pair() {
try { try {
let connected = await serviceRegistry.netmdService!.connect(); let connected = await serviceRegistry.netmdService!.connect();
if (connected) { if (connected) {
dispatch(appStateActions.setState('MAIN')); dispatch(appStateActions.setMainView('MAIN'));
return; return;
} }
} catch (err) { } catch (err) {
@ -67,7 +67,7 @@ export function pair() {
try { try {
let paired = await serviceRegistry.netmdService!.pair(); let paired = await serviceRegistry.netmdService!.pair();
if (paired) { if (paired) {
dispatch(appStateActions.setState('MAIN')); dispatch(appStateActions.setMainView('MAIN'));
return; return;
} }
dispatch(batchActions([appStateActions.setPairingMessage(`Connection Failed`), appStateActions.setPairingFailed(true)])); dispatch(batchActions([appStateActions.setPairingMessage(`Connection Failed`), appStateActions.setPairingFailed(true)]));

View File

@ -17,24 +17,29 @@ export interface AppState {
hasNotificationSupport: boolean; hasNotificationSupport: boolean;
} }
const initialState: AppState = { export const buildInitialState = (): AppState => {
mainView: 'WELCOME', return {
loading: false, mainView: 'WELCOME',
pairingFailed: false, loading: false,
pairingMessage: ``, pairingFailed: false,
browserSupported: true, pairingMessage: ``,
darkMode: loadPreference('darkMode', false), browserSupported: true,
vintageMode: loadPreference('vintageMode', false), darkMode: loadPreference('darkMode', false),
aboutDialogVisible: false, vintageMode: loadPreference('vintageMode', false),
notifyWhenFinished: loadPreference('notifyWhenFinished', false), aboutDialogVisible: false,
hasNotificationSupport: true, notifyWhenFinished: loadPreference('notifyWhenFinished', false),
hasNotificationSupport: true,
};
}; };
const initialState: AppState = buildInitialState();
export const slice = createSlice({ export const slice = createSlice({
name: 'app', name: 'app',
initialState, initialState,
reducers: { reducers: {
setState: (state, action: PayloadAction<Views>) => { setMainView: (state, action: PayloadAction<Views>) => {
// CAVEAT: There's a middleware that resets the state when mainView is set to WELCOME
state.mainView = action.payload; state.mainView = action.payload;
}, },
setLoading: (state, action: PayloadAction<boolean>) => { setLoading: (state, action: PayloadAction<boolean>) => {
@ -62,7 +67,7 @@ export const slice = createSlice({
}, },
setVintageMode: (state, action: PayloadAction<boolean>) => { setVintageMode: (state, action: PayloadAction<boolean>) => {
state.vintageMode = action.payload; state.vintageMode = action.payload;
savePreference('vintageMode', state.vintageMode); savePreference('vintageMode', action.payload);
}, },
showAboutDialog: (state, action: PayloadAction<boolean>) => { showAboutDialog: (state, action: PayloadAction<boolean>) => {
state.aboutDialogVisible = action.payload; state.aboutDialogVisible = action.payload;

View File

@ -6,7 +6,7 @@ import panicDialog, { actions as panicDialogActions } from './panic-dialog-featu
import convertDialog from './convert-dialog-feature'; import convertDialog from './convert-dialog-feature';
import dumpDialog from './dump-dialog-feature'; import dumpDialog from './dump-dialog-feature';
import recordDialog from './record-dialog-feature'; import recordDialog from './record-dialog-feature';
import appState, { actions as appActions } from './app-feature'; import appState, { actions as appActions, buildInitialState as buildInitialAppState } from './app-feature';
import main from './main-feature'; import main from './main-feature';
const errorCatcher: Middleware = store => next => async action => { const errorCatcher: Middleware = store => next => async action => {
@ -30,12 +30,15 @@ let reducer = combineReducers({
main, main,
}); });
const resetStateAction = appActions.setState.toString(); const resetStateAction = appActions.setMainView.toString();
const resetStatePayoload = 'WELCOME'; const resetStatePayoload = 'WELCOME';
const resetStateReducer: typeof reducer = function(...args) { const resetStateReducer: typeof reducer = function(...args) {
const [state, action] = args; const [state, action] = args;
if (action.type === resetStateAction && action.payload === resetStatePayoload) { if (action.type === resetStateAction && action.payload === resetStatePayoload) {
return initialState; return {
...initialState,
appState: buildInitialAppState(),
};
} }
return reducer(...args); return reducer(...args);
}; };