Fixing logout

This commit is contained in:
Mitchell Magro 2025-07-25 19:06:14 +02:00
parent 7851a6fa24
commit cfd796c1e9
4 changed files with 55 additions and 45 deletions

View File

@ -1,9 +1,9 @@
// app/redux/ReduxProvider.tsx // app/redux/ReduxProvider.tsx
"use client"; "use client";
import React, { useEffect } from "react"; // Import useEffect import React, { useEffect, useRef } from "react";
import { Provider } from "react-redux"; import { Provider } from "react-redux";
import { store } from "./store"; import { makeStore } from "./store";
import { initializeAuth } from "./auth/authSlice"; import { initializeAuth } from "./auth/authSlice";
export default function ReduxProvider({ export default function ReduxProvider({
@ -11,14 +11,16 @@ export default function ReduxProvider({
}: { }: {
children: React.ReactNode; children: React.ReactNode;
}) { }) {
// Get the dispatch function directly from the store for initial dispatch // Create a store instance for this client-side session
const dispatch = store.dispatch; const storeRef = useRef<ReturnType<typeof makeStore>>(makeStore());
useEffect(() => { useEffect(() => {
// Dispatch initializeAuth when the ReduxProvider component mounts on the client. // Dispatch initializeAuth when the ReduxProvider component mounts on the client.
// This ensures your Redux isLoggedIn state is synced with localStorage after a page refresh. // This ensures your Redux isLoggedIn state is synced with localStorage after a page refresh.
dispatch(initializeAuth()); if (storeRef.current) {
}, [dispatch]); // Dependency array ensures it runs only once on mount storeRef.current.dispatch(initializeAuth() as any);
}
}, []);
return <Provider store={store}>{children}</Provider>; return <Provider store={storeRef.current}>{children}</Provider>;
} }

View File

@ -1,25 +1,25 @@
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from "@reduxjs/toolkit";
interface AdvancedSearchState { interface AdvancedSearchState {
keyword: string, keyword: string;
transactionID: string, transactionID: string;
transactionReferenceId: string, transactionReferenceId: string;
user: string, user: string;
currency: string, currency: string;
state: string, state: string;
statusDescription: string, statusDescription: string;
transactionType: string, transactionType: string;
paymentMethod: string, paymentMethod: string;
psps: string, psps: string;
initialPsps: string, initialPsps: string;
merchants: string, merchants: string;
startDate: null | string, startDate: null | string;
endDate: null | string, endDate: null | string;
lastUpdatedFrom: null | string, lastUpdatedFrom: null | string;
lastUpdatedTo: null | string, lastUpdatedTo: null | string;
minAmount: string, minAmount: string;
maxAmount: string, maxAmount: string;
channel: string, channel: string;
} }
const initialState: AdvancedSearchState = { const initialState: AdvancedSearchState = {
@ -45,12 +45,9 @@ const initialState: AdvancedSearchState = {
}; };
const advancedSearchSlice = createSlice({ const advancedSearchSlice = createSlice({
name: 'advancedSearch', name: "advancedSearch",
initialState, initialState,
reducers: { reducers: {},
}, });
},
);
export default advancedSearchSlice.reducer; export default advancedSearchSlice.reducer;

View File

@ -1,8 +1,6 @@
import { RootState } from "../types"; import { RootState } from "../types";
export const selectIsLoggedIn = (state: RootState) => export const selectIsLoggedIn = (state: RootState) => state.auth.isLoggedIn;
state.authSlice.isLoggedIn; export const selectStatus = (state: RootState) => state.auth?.status;
export const selectStatus = (state: RootState) => state.authSlice?.status; export const selectError = (state: RootState) => state.auth?.error;
export const selectError = (state: RootState) => state.authSlice?.error; export const selectAuthMessage = (state: RootState) => state.auth?.authMessage;
export const selectAuthMessage = (state: RootState) =>
state.authSlice?.authMessage;

View File

@ -2,9 +2,22 @@ import { configureStore } from "@reduxjs/toolkit";
import advancedSearchReducer from "./advanedSearch/advancedSearchSlice"; import advancedSearchReducer from "./advanedSearch/advancedSearchSlice";
import authReducer from "./auth/authSlice"; import authReducer from "./auth/authSlice";
export const store = configureStore({ export const makeStore = () => {
return configureStore({
reducer: { reducer: {
advancedSearch: advancedSearchReducer, advancedSearch: advancedSearchReducer,
authSlice: authReducer, auth: authReducer,
}, },
}); // Enable Redux DevTools
devTools: process.env.NODE_ENV !== "production",
});
};
// Create the store instance
export const store = makeStore();
// Infer the type of makeStore
export type AppStore = ReturnType<typeof makeStore>;
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<AppStore["getState"]>;
export type AppDispatch = AppStore["dispatch"];