Files
queueful/frontend/app/src/config/store.ts
2024-04-08 15:12:55 +03:00

38 lines
1.2 KiB
TypeScript

import { configureStore, createAction, createReducer } from "@reduxjs/toolkit";
import { setupListeners } from "@reduxjs/toolkit/query";
import { AuthApi } from "../slice/AuthApi";
export type authState = {
token: string | null;
};
const initialAuthState: authState = {
token: null,
};
export const store = configureStore({
reducer: {
// Add the generated reducer as a specific top-level slice
[AuthApi.reducerPath]: AuthApi.reducer,
auth: createReducer(initialAuthState, (builder) => {
builder.addCase(createAction("auth/token"), (state, _) => {
const token: string | null = localStorage.getItem("token");
if (token) {
state.token = token;
}
});
}),
},
// Adding the api middleware enables caching, invalidation, polling,
// and other useful features of `rtk-query`.
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(AuthApi.middleware),
});
// optional, but required for refetchOnFocus/refetchOnReconnect behaviors
// see `setupListeners` docs - takes an optional callback as the 2nd arg for customization
setupListeners(store.dispatch);
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;