2023-05-10 20:59:29 +10:00
|
|
|
import type { TypedUseSelectorHook } from 'react-redux';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
|
2023-05-10 00:56:26 +10:00
|
|
|
import { configureStore } from '@reduxjs/toolkit';
|
2023-05-10 20:59:29 +10:00
|
|
|
|
2023-05-10 00:56:26 +10:00
|
|
|
import { rootReducer } from '../reducers';
|
2023-05-10 20:59:29 +10:00
|
|
|
|
2023-05-10 00:56:26 +10:00
|
|
|
import { errorsMiddleware } from './middlewares/errors';
|
2023-05-10 20:59:29 +10:00
|
|
|
import { loadingBarMiddleware } from './middlewares/loading_bar';
|
2023-05-10 00:56:26 +10:00
|
|
|
import { soundsMiddleware } from './middlewares/sounds';
|
|
|
|
|
|
|
|
export const store = configureStore({
|
|
|
|
reducer: rootReducer,
|
2023-05-10 03:02:12 +10:00
|
|
|
middleware: (getDefaultMiddleware) =>
|
2023-05-10 16:38:02 +10:00
|
|
|
getDefaultMiddleware({
|
|
|
|
// In development, Redux Toolkit enables 2 default middlewares to detect
|
|
|
|
// common issues with states. Unfortunately, our use of ImmutableJS for state
|
|
|
|
// triggers both, so lets disable them until our state is fully refactored
|
|
|
|
|
|
|
|
// https://redux-toolkit.js.org/api/serializabilityMiddleware
|
|
|
|
// This checks recursively that every values in the state are serializable in JSON
|
|
|
|
// Which is not the case, as we use ImmutableJS structures, but also File objects
|
|
|
|
serializableCheck: false,
|
|
|
|
|
|
|
|
// https://redux-toolkit.js.org/api/immutabilityMiddleware
|
|
|
|
// This checks recursively if every value in the state is immutable (ie, a JS primitive type)
|
|
|
|
// But this is not the case, as our Root State is an ImmutableJS map, which is an object
|
|
|
|
immutableCheck: false,
|
|
|
|
})
|
2023-05-10 03:02:12 +10:00
|
|
|
.concat(
|
|
|
|
loadingBarMiddleware({
|
|
|
|
promiseTypeSuffixes: ['REQUEST', 'SUCCESS', 'FAIL'],
|
|
|
|
})
|
|
|
|
)
|
2023-05-10 00:56:26 +10:00
|
|
|
.concat(errorsMiddleware)
|
|
|
|
.concat(soundsMiddleware()),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Infer the `RootState` and `AppDispatch` types from the store itself
|
2023-05-10 03:02:12 +10:00
|
|
|
export type RootState = ReturnType<typeof rootReducer>;
|
2023-05-10 00:56:26 +10:00
|
|
|
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
|
2023-05-10 03:02:12 +10:00
|
|
|
export type AppDispatch = typeof store.dispatch;
|
2023-05-10 00:56:26 +10:00
|
|
|
|
|
|
|
export const useAppDispatch: () => AppDispatch = useDispatch;
|
|
|
|
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
|