All files / src/store/features/settings settingsMiddleware.ts

55.55% Statements 10/18
12.5% Branches 1/8
100% Functions 3/3
50% Lines 8/16

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    1x 1x 1x     1x 1x   1x                                               1x     1x  
import { Dispatch, Middleware } from "@reduxjs/toolkit";
import { RootState } from "store/storeTypes";
import settingsActions from "./settingsActions";
import { getSettings } from "store/features/settings/settingsState";
import { defaultProjectSettings } from "consts";
 
const settingsMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => (action) => {
    const result = next(action);
 
    Iif (settingsActions.editSettings.match(action)) {
      // When color mode changes reset previewAsMono to false
      if (action.payload.colorMode) {
        store.dispatch(
          settingsActions.editSettings({
            previewAsMono: false,
          }),
        );
      }
      // When collisions enabled set opacity to a min of 50%
      if (action.payload.showCollisions) {
        const state = store.getState();
        const settings = getSettings(state);
        const defaultOpacity = defaultProjectSettings.collisionLayerOpacity;
        if (settings.collisionLayerOpacity < defaultOpacity) {
          store.dispatch(
            settingsActions.editSettings({
              collisionLayerOpacity: defaultOpacity,
            }),
          );
        }
      }
    }
 
    return result;
  };
 
export default settingsMiddleware;