Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear the redux store when disconnected #1234

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/src/app/base/reducers/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createStandardReducer } from "app/utils/redux";

import { user as userActions } from "app/base/actions";

const initialState = {
export const initialState = {
auth: {},
errors: {},
items: [],
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/base/sagas/websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export function* handleMessage(socketChannel, socketClient) {
yield put({ type: "WEBSOCKET_DISCONNECTED" });
} else if (response.type === "open") {
yield put({ type: "WEBSOCKET_CONNECTED" });
resetLoaded();
} else {
// This is a response message, fetch the corresponding action for the
// message that was sent.
Expand Down
18 changes: 18 additions & 0 deletions ui/src/root-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
zone,
} from "./app/base/reducers";
import { config } from "./app/settings/reducers";
import { initialState as userInitialState } from "./app/base/reducers/user/user";
import { token, sshkey, sslkey } from "./app/preferences/reducers";

const createAppReducer = (history) =>
Expand Down Expand Up @@ -65,6 +66,23 @@ const createRootReducer = (history) => (state, action) => {
{ status: { authenticating: false } },
action
);
} else if (
[
"WEBSOCKET_ERROR",
"WEBSOCKET_DISCONNECTED",
"CHECK_AUTHENTICATED_ERROR",
].includes(action.type)
) {
return createAppReducer(history)(
{
status: state.status,
user: {
...userInitialState,
auth: state.user.auth,
},
},
action
);
}
return createAppReducer(history)(state, action);
};
Expand Down
23 changes: 23 additions & 0 deletions ui/src/root-reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,27 @@ describe("rootReducer", () => {
expect(newState.status.authenticating).toBe(false);
expect(newState).toMatchSnapshot();
});

it("it should clear the state when disconnected from the websocket", () => {
const initialState = {
machine: {
items: [1, 2, 3],
},
status: { authenticating: true },
user: {
items: [1, 2, 3],
auth: {
user: { id: 1 },
},
},
};
const newState = createRootReducer({})(initialState, {
type: "WEBSOCKET_DISCONNECTED",
});

expect(newState.machine.items.length).toBe(0);
expect(newState.status.authenticating).toBe(true);
expect(newState.user.items.length).toBe(0);
expect(newState.user.auth.user).toStrictEqual({ id: 1 });
});
});