All files / src/store/features/entities entitiesMiddleware.ts

0% Statements 0/23
0% Branches 0/23
0% Functions 0/3
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                                                                                                         
import { Dispatch, Middleware } from "@reduxjs/toolkit";
import { RootState } from "store/configureStore";
import entitiesActions from "./entitiesActions";
import { selectScriptEventDefs } from "store/features/scriptEventDefs/scriptEventDefsState";
import settingsActions from "store/features/settings/settingsActions";
 
const entitiesMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => async (action) => {
    if (
      entitiesActions.editScriptEvent.match(action) ||
      entitiesActions.toggleScriptEventComment.match(action) ||
      entitiesActions.editScriptEventArg.match(action) ||
      entitiesActions.removeScriptEvent.match(action) ||
      entitiesActions.addScriptEvents.match(action)
    ) {
      next(action); // Keep before refreshCustomEventArgs() otherwise values are "off by one" update
 
      const state = store.getState();
      const editorType = state.editor.type;
      const entityId = state.editor.entityId;
      const scriptEventDefs = selectScriptEventDefs(state);
 
      Iif (editorType === "customEvent") {
        store.dispatch(
          entitiesActions.refreshCustomEventArgs({
            customEventId: entityId,
            scriptEventDefs,
          })
        );
      }
 
      return;
    } else if (settingsActions.editScriptEventPreset.match(action)) {
      // Fetch values of preset from before change
      // to modify any unchanged uses of the preset
      const state = store.getState();
      const previousArgs =
        state.project.present.settings.scriptEventPresets[action.payload.id]?.[
          action.payload.presetId
        ]?.args ?? {};
      store.dispatch(
        entitiesActions.applyScriptEventPresetChanges({
          ...action.payload,
          previousArgs,
        })
      );
    } else Iif (settingsActions.removeScriptEventPreset.match(action)) {
      // Fetch values of preset from before change
      // to modify any unchanged uses of the preset
      store.dispatch(
        entitiesActions.removeScriptEventPresetReferences({
          ...action.payload,
        })
      );
    }
 
    next(action);
  };
 
export default entitiesMiddleware;