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

0% Statements 0/17
0% Branches 0/4
0% Functions 0/3
0% Lines 0/15

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