feat(ui): add rtk action type guard

This commit is contained in:
psychedelicious 2023-04-02 19:48:29 +10:00
parent 4ac447cf74
commit 662f1321f6

View File

@ -1,14 +1,21 @@
import { Middleware, MiddlewareAPI } from '@reduxjs/toolkit';
import { isFulfilled, Middleware, MiddlewareAPI } from '@reduxjs/toolkit';
import { emitSubscribe } from 'app/nodesSocketio/actions';
import { AppDispatch, RootState } from 'app/store';
import { setSessionId } from './apiSlice';
import { invokeSession } from './thunks/session';
import { createSession, invokeSession } from './thunks/session';
/**
* `redux-toolkit` provides nice matching utilities, which can be used as type guards
* See: https://redux-toolkit.js.org/api/matching-utilities
*/
const isFulfilledCreateSession = isFulfilled(createSession);
export const invokeMiddleware: Middleware =
(store: MiddlewareAPI<AppDispatch, RootState>) => (next) => (action) => {
const { dispatch } = store;
if (action.type === 'api/createSession/fulfilled' && action?.payload?.id) {
if (isFulfilledCreateSession(action)) {
const sessionId = action.payload.id;
console.log('createSession.fulfilled');