I'm not sure how to write a React observable epic with Redux Toolkit and Typescript.
Suppose I have this authSlice:
import { CaseReducer, createSlice, PayloadAction } from "@reduxjs/toolkit";
type AuthState = {
token: string,
loading: boolean,
};
const initialState: AuthState = {
token: "",
loading: false,
};
const loginStart: CaseReducer<AuthState, PayloadAction<{username: string, password: string}>> = (state, action) => ({
...state,
loading: true,
token: "",
});
const loginCompleted: CaseReducer<AuthState, PayloadAction<{token: string}>> = (state, action) => ({
...state,
loading: false,
token: action.payload.token,
});
const authSlice = createSlice({
name: 'auth',
initialState,
reducers: {
loginStart,
loginCompleted,
},
});
export default authSlice;
and this store:
import { configureStore } from '@reduxjs/toolkit';
import { combineEpics, createEpicMiddleware } from 'redux-observable';
import authEpic from './epics/authEpic';
import authSlice from './slices/authSlice';
const epicMiddleware = createEpicMiddleware();
export const rootEpic = combineEpics(
authEpic
);
const store = configureStore({
reducer: {
auth: authSlice.reducer,
},
middleware: [epicMiddleware]
});
epicMiddleware.run(rootEpic);
export type RootState = ReturnType<typeof store.getState>;
export default store;
how should I write this authEpic (I hope the purpose is self-explanatory):
import { Action, Observable } from 'redux';
import { ActionsObservable, ofType } from 'redux-observable';
import { ajax } from 'rxjs/ajax';
import { switchMap } from 'rxjs/operators';
import authSlice from '../slices/authSlice';
export default (action$: ActionsObservable<???>) => action$.pipe(
ofType(???), /* should be of type loginStart */
switchMap<???,???>(action => ajax.post( // should be from a loginStart action to {token: string}
"url", {
username: action.payload.username,
password: action.payload.password
}
)),
...
);
I'm totally confused about the ??? that is what should be the types and how redux observable should be linked with redux toolkit.
Any hint?