commit 53417f00f4d524592ef1f91199854adf0105bbc7
parent cfddeab63e1ad7f7becd2f4d80e3777774a34652
Author: MTRNord <mtrnord1@gmail.com>
Date: Sat, 22 Apr 2023 18:08:17 +0200
chore: further redux cleanup and adding client to store
Diffstat:
3 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/src/app/api/reducers.ts b/src/app/api/reducers.ts
@@ -5,6 +5,7 @@ import { MatrixClient } from "matrix-js-sdk";
interface ApiLoginStatus {
loginPending: boolean
error?: string
+ client?: MatrixClient
}
export const LOGIN_ACTION = "api/LOGIN";
@@ -15,7 +16,7 @@ export const LOGIN_FAILURE_ACTION = createAction<string>('api/LOGIN_FAILURE');
export interface LOGIN extends Action { type: typeof LOGIN_ACTION, baseUrl: string, username: string, password: string };
-const initialLoginState: ApiLoginStatus = { loginPending: false, error: undefined };
+const initialLoginState: ApiLoginStatus = { loginPending: false, error: undefined, client: undefined };
export const apiLoginReducer = createReducer(initialLoginState, (builder) => {
@@ -25,8 +26,9 @@ export const apiLoginReducer = createReducer(initialLoginState, (builder) => {
state.error = undefined;
})
// @ts-ignore: Somehow this state is causing issues here since its written but not read. TS6133
- .addCase(LOGIN_SUCCESS_ACTION, (state, _) => {
+ .addCase(LOGIN_SUCCESS_ACTION, (state, action) => {
state = initialLoginState;
+ state.client = action.payload
})
.addCase(LOGIN_FAILURE_ACTION, (state, action) => {
state.loginPending = false;
diff --git a/src/app/protectedRoute.tsx b/src/app/protectedRoute.tsx
@@ -3,8 +3,10 @@ import { Navigate } from "react-router-dom";
import { useAppSelector } from "./hooks";
export const ProtectedRoute = ({ children }: { children: ReactElement<any, any> }) => {
- const accessToken = useAppSelector(state => state.api.accessToken);
- if (!accessToken) {
+ const client = useAppSelector(state => state.login.client);
+
+ // TODO: relaunch a client if we can and skip login
+ if (!client) {
// user is not authenticated
return <Navigate to="login" />;
}
diff --git a/src/app/store.ts b/src/app/store.ts
@@ -1,14 +1,13 @@
import { configureStore, ThunkAction, Action } from "@reduxjs/toolkit";
import createSagaMiddleware from "redux-saga";
import rootSaga from "./sagas/rootSaga";
-import { apiDataReducer, apiLoginReducer } from "./api/reducers";
+import { apiLoginReducer } from "./api/reducers";
const sagaMiddleware = createSagaMiddleware();
const middlewares = [sagaMiddleware];
export const store = configureStore({
reducer: {
- api: apiDataReducer,
login: apiLoginReducer,
},
middleware: (getDefaultMiddleware) => [