All files / src/store/features/trackerDocument trackerDocumentMiddleware.ts

97.36% Statements 37/38
87.09% Branches 27/31
100% Functions 5/5
97.22% Lines 35/36

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91      2x 2x 2x         2x 2x 2x 2x 2x 2x     2x 6x 6x 6x 6x 6x       6x   2x 2x 1x   4x   2x     2x     3x           8x 20x   20x             10x 6x               14x 1x     14x             14x 1x               14x     2x  
import type { Middleware, UnknownAction } from "@reduxjs/toolkit";
import type { ThunkDispatch } from "redux-thunk";
import type { AppThunk, RootState } from "store/storeTypes";
import { musicSelectors } from "store/features/entities/entitiesSelectors";
import navigationActions from "store/features/navigation/navigationActions";
import {
  addNewSongFile,
  requestAddNewSongFile,
  saveSongFile,
} from "./trackerDocumentState";
import trackerDocumentActions from "./trackerDocumentActions";
import electronActions from "store/features/electron/electronActions";
import l10n from "shared/lib/lang/l10n";
import API from "renderer/lib/api";
import projectActions from "store/features/project/projectActions";
import trackerActions from "store/features/tracker/trackerActions";
 
const confirmUnsavedChangesAndContinue =
  (action: UnknownAction): AppThunk<Promise<UnknownAction | undefined>> =>
  async (dispatch, getState) => {
    const state = getState();
    const songsLookup = musicSelectors.selectEntities(state);
    const selectedSong = songsLookup[state.tracker.selectedSongId];
    const option = await API.dialog.confirmUnsavedChangesTrackerDialog(
      selectedSong?.name ?? "",
    );
 
    if (option === 0) {
      // Save and continue
      const result = await dispatch(saveSongFile());
      if (saveSongFile.rejected.match(result)) {
        return;
      }
    } else if (option === 1) {
      // Continue without saving
      dispatch(trackerDocumentActions.unloadSong());
    } else {
      // Cancel
      return;
    }
 
    return dispatch(action);
  };
 
type TrackerDispatch = ThunkDispatch<RootState, undefined, UnknownAction>;
 
const trackerMiddleware: Middleware<object, RootState, TrackerDispatch> =
  (store) => (next) => (action) => {
    const state = store.getState();
 
    if (
      (navigationActions.setSection.match(action) &&
        action.payload !== "music") ||
      (trackerActions.setSelectedSongId.match(action) &&
        action.payload !== state.tracker.selectedSongId) ||
      requestAddNewSongFile.match(action)
    ) {
      if (state.tracker.modified) {
        return store.dispatch(
          confirmUnsavedChangesAndContinue(action as UnknownAction),
        );
      }
    }
 
    // Delay creation until confirmUnsavedChangesTrackerDialog has
    // had a chance to ask about unsaved changes
    if (requestAddNewSongFile.match(action)) {
      store.dispatch(addNewSongFile(action.payload));
    }
 
    Iif (
      projectActions.saveProject.pending.match(action) &&
      state.tracker.modified
    ) {
      store.dispatch(saveSongFile());
    }
 
    if (saveSongFile.rejected.match(action)) {
      store.dispatch(
        electronActions.showErrorBox({
          title: l10n("ERROR_UNABLE_TO_SAVE_MUSIC_FILE"),
          content: l10n("ERROR_UNABLE_TO_SAVE_MUSIC_FILE_DESC"),
        }),
      );
    }
 
    return next(action);
  };
 
export default trackerMiddleware;