-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add reducer manager for async reducers
- Loading branch information
Showing
18 changed files
with
236 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
import { CounterSchema } from 'entities/Counter'; | ||
import { UserSchema } from 'entities/User'; | ||
import { LoginSchema } from 'features/AuthByUsername'; | ||
import { | ||
AnyAction, EnhancedStore, Reducer, ReducersMapObject, | ||
} from '@reduxjs/toolkit'; | ||
import { CombinedState } from 'redux'; | ||
|
||
export interface StateSchema { | ||
counter: CounterSchema; | ||
user: UserSchema; | ||
loginForm: LoginSchema | ||
// Асинхронные редюсеры | ||
loginForm?: LoginSchema; | ||
} | ||
|
||
export type StateSchemaKey = keyof StateSchema; | ||
|
||
export interface ReducerManager { | ||
getReducerMap: () => ReducersMapObject<StateSchema>; | ||
reduce: (state: StateSchema, action: AnyAction) => CombinedState<StateSchema>; | ||
Check warning on line 20 in src/app/providers/StoreProvider/config/StateSchema.ts GitHub Actions / pipeline (17.x)
|
||
add: (key: StateSchemaKey, reducer: Reducer) => void; | ||
Check warning on line 21 in src/app/providers/StoreProvider/config/StateSchema.ts GitHub Actions / pipeline (17.x)
|
||
remove: (key: StateSchemaKey) => void; | ||
} | ||
|
||
export interface ReduxStoreWithManager extends EnhancedStore<StateSchema> { | ||
reducerManager: ReducerManager; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
AnyAction, combineReducers, Reducer, ReducersMapObject, | ||
} from '@reduxjs/toolkit'; | ||
import { ReducerManager, StateSchema, StateSchemaKey } from './StateSchema'; | ||
|
||
export function createReducerManager(initialReducers: ReducersMapObject<StateSchema>): ReducerManager { | ||
const reducers = { ...initialReducers }; | ||
|
||
let combinedReducer = combineReducers(reducers); | ||
|
||
let keysToRemove: Array<StateSchemaKey> = []; | ||
|
||
return { | ||
getReducerMap: () => reducers, | ||
reduce: (state: StateSchema, action: AnyAction) => { | ||
if (keysToRemove.length > 0) { | ||
state = { ...state }; | ||
keysToRemove.forEach((key) => { | ||
delete state[key]; | ||
}); | ||
keysToRemove = []; | ||
} | ||
return combinedReducer(state, action); | ||
}, | ||
add: (key: StateSchemaKey, reducer: Reducer) => { | ||
if (!key || reducers[key]) { | ||
return; | ||
} | ||
reducers[key] = reducer; | ||
|
||
combinedReducer = combineReducers(reducers); | ||
}, | ||
remove: (key: StateSchemaKey) => { | ||
if (!key || !reducers[key]) { | ||
return; | ||
} | ||
delete reducers[key]; | ||
keysToRemove.push(key); | ||
combinedReducer = combineReducers(reducers); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
import { configureStore, ReducersMapObject } from '@reduxjs/toolkit'; | ||
import { configureStore, DeepPartial, ReducersMapObject } from '@reduxjs/toolkit'; | ||
import { counterReducer } from 'entities/Counter'; | ||
import { userReducer } from 'entities/User'; | ||
import { loginReducer } from 'features/AuthByUsername'; | ||
import { StateSchema } from './StateSchema'; | ||
import { createReducerManager } from './reducerManager'; | ||
|
||
export function createReduxStore(initialState?:StateSchema) { | ||
export function createReduxStore( | ||
initialState?: StateSchema, | ||
asyncReducers?: ReducersMapObject<StateSchema>, | ||
) { | ||
const rootReducers: ReducersMapObject<StateSchema> = { | ||
...asyncReducers, | ||
counter: counterReducer, | ||
user: userReducer, | ||
loginForm: loginReducer, | ||
}; | ||
|
||
return configureStore<StateSchema>({ | ||
reducer: rootReducers, | ||
const reducerManager = createReducerManager(rootReducers); | ||
|
||
const store = configureStore<StateSchema>({ | ||
reducer: reducerManager.reduce, | ||
devTools: __IS_DEV__, | ||
preloadedState: initialState, | ||
}); | ||
|
||
// @ts-ignore | ||
store.reducerManager = reducerManager; | ||
|
||
return store; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { StoreProvider } from 'app/providers/StoreProvider/ui/StoreProvider'; | ||
import { createReduxStore } from 'app/providers/StoreProvider/config/store'; | ||
import type { StateSchema } from 'app/providers/StoreProvider/config/StateSchema'; | ||
import type { StateSchema, ReduxStoreWithManager } from 'app/providers/StoreProvider/config/StateSchema'; | ||
|
||
export { StoreProvider, createReduxStore, StateSchema }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { LoginModal } from './ui/LoginModal/LoginModal'; | ||
export { LoginSchema } from './model/types/loginSchema'; | ||
export { loginReducer } from './model/slice/loginSlice'; |
3 changes: 3 additions & 0 deletions
3
src/features/AuthByUsername/model/selectors/getLoginError/getLoginError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
|
||
export const getLoginError = (state: StateSchema) => state?.loginForm?.error; |
3 changes: 3 additions & 0 deletions
3
src/features/AuthByUsername/model/selectors/getLoginIsLoading/getLoginIsLoading.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
|
||
export const getLoginIsLoading = (state: StateSchema) => state?.loginForm?.isLoading || false; |
3 changes: 3 additions & 0 deletions
3
src/features/AuthByUsername/model/selectors/getLoginPassword/getLoginPassword.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
|
||
export const getLoginPassword = (state: StateSchema) => state?.loginForm?.password || ''; |
3 changes: 0 additions & 3 deletions
3
src/features/AuthByUsername/model/selectors/getLoginState/getLoginState.ts
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
src/features/AuthByUsername/model/selectors/getLoginUsername/getLoginUsername.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
|
||
export const getLoginUsername = (state: StateSchema) => state?.loginForm?.username || ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { FC, lazy } from 'react'; | ||
import { LoginFormProps } from 'features/AuthByUsername/ui/LoginForm/LoginForm'; | ||
|
||
export const LoginFormAsync = lazy<FC<LoginFormProps>>(() => new Promise((resolve) => { | ||
// @ts-ignore | ||
// ТАК В РЕАЛЬНЫХ ПРОЕКТАХ НЕ ДЕЛАТЬ!!!!! ДЕЛАЕМ ДЛЯ КУРСА! | ||
setTimeout(() => resolve(import('./LoginForm')), 1500); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 15 additions & 3 deletions
18
src/shared/config/storybook/StoreDecorator/StoreDecorator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
import { Story } from '@storybook/react'; | ||
import { StateSchema, StoreProvider } from 'app/providers/StoreProvider'; | ||
import { DeepPartial } from '@reduxjs/toolkit'; | ||
import { DeepPartial, ReducersMapObject } from '@reduxjs/toolkit'; | ||
import loginForm from 'features/AuthByUsername/ui/LoginForm/LoginForm'; | ||
import { loginReducer } from 'features/AuthByUsername/model/slice/loginSlice'; | ||
|
||
export const StoreDecorator = (state: DeepPartial<StateSchema>) => (StoryComponent: Story) => ( | ||
<StoreProvider initialState={state}> | ||
const defaultAsyncReducers:DeepPartial<ReducersMapObject<StateSchema>> = { | ||
loginForm: loginReducer, | ||
}; | ||
|
||
export const StoreDecorator = ( | ||
state: DeepPartial<StateSchema>, | ||
asyncReducer?: DeepPartial<ReducersMapObject<StateSchema>>, | ||
) => (StoryComponent: Story) => ( | ||
<StoreProvider | ||
initialState={state} | ||
asyncReducers={{ ...defaultAsyncReducers, ...asyncReducer }} | ||
> | ||
<StoryComponent /> | ||
</StoreProvider> | ||
); |
53 changes: 53 additions & 0 deletions
53
src/shared/lib/components/DynamicModuleLoared/DynamicModuleLoared.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { FC, useEffect } from 'react'; | ||
import { useDispatch, useStore } from 'react-redux'; | ||
import { | ||
ReduxStoreWithManager, | ||
StateSchemaKey, | ||
} from 'app/providers/StoreProvider/config/StateSchema'; | ||
import { Reducer } from '@reduxjs/toolkit'; | ||
|
||
export type ReducersList = { | ||
[name in StateSchemaKey]?: Reducer; | ||
}; | ||
|
||
type ReducersListEntry = [StateSchemaKey, Reducer] | ||
|
||
interface DynamicModuleLoaderProps { | ||
reducers: ReducersList; | ||
removeAfterUnmount?: boolean; | ||
} | ||
|
||
export const DynamicModuleLoader: FC<DynamicModuleLoaderProps> = (props) => { | ||
const { | ||
children, | ||
reducers, | ||
removeAfterUnmount, | ||
} = props; | ||
|
||
const store = useStore() as ReduxStoreWithManager; | ||
const dispatch = useDispatch(); | ||
|
||
useEffect(() => { | ||
Object.entries(reducers).forEach(([name, reducer]: ReducersListEntry) => { | ||
store.reducerManager.add(name, reducer); | ||
dispatch({ type: `@INIT ${name} reducer` }); | ||
}); | ||
|
||
return () => { | ||
if (removeAfterUnmount) { | ||
Object.entries(reducers).forEach(([name, reducer]: ReducersListEntry) => { | ||
store.reducerManager.remove(name); | ||
dispatch({ type: `@DESTROY ${name} reducer` }); | ||
}); | ||
} | ||
}; | ||
// eslint-disable-next-line | ||
}, []); | ||
|
||
return ( | ||
// eslint-disable-next-line react/jsx-no-useless-fragment | ||
<> | ||
{children} | ||
</> | ||
); | ||
}; |
Oops, something went wrong.