All files / src/store/features/document documentState.ts

64% Statements 16/25
66.67% Branches 4/6
60% Functions 6/10
64% Lines 16/25

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 8323x 23x                               23x                       23x         3x   1x     1x 1x                               1x 1x 1x 1x                   7x         3x         23x   23x  
import { createSlice, UnknownAction } from "@reduxjs/toolkit";
import projectActions, {
  SaveStep,
} from "store/features/project/projectActions";
 
export interface DocumentState {
  modified: boolean;
  loaded: boolean;
  saving: boolean;
  saveStep: SaveStep;
  saveWriteProgress: {
    completed: number;
    total: number;
  };
  saveError: boolean;
}
 
export const initialState: DocumentState = {
  modified: false,
  loaded: false,
  saving: false,
  saveStep: "complete",
  saveWriteProgress: {
    completed: 0,
    total: 0,
  },
  saveError: false,
};
 
const documentSlice = createSlice({
  name: "document",
  initialState,
  reducers: {},
  extraReducers: (builder) =>
    builder
      .addCase(projectActions.loadProject.pending, (state, _action) => {
        state.loaded = false;
      })
      .addCase(projectActions.loadProject.fulfilled, (state, action) => {
        state.modified = action.payload.isMigrated;
        state.loaded = true;
      })
      .addCase(projectActions.saveProject.pending, (state, _action) => {
        Iif (!state.saving) {
          state.saving = true;
          state.saveStep = "saving";
          state.saveError = false;
        }
      })
      .addCase(projectActions.saveProject.rejected, (state, _action) => {
        state.saving = false;
        Iif (state.saveStep !== "saving") {
          state.saveError = true;
        }
      })
      .addCase(projectActions.saveProject.fulfilled, (state, _action) => {
        state.saving = false;
        state.modified = false;
        state.saveStep = "complete";
        state.saveError = false;
      })
      .addCase(projectActions.setSaveStep, (state, action) => {
        state.saveStep = action.payload;
      })
      .addCase(projectActions.setSaveWriteProgress, (state, action) => {
        state.saveWriteProgress = action.payload;
      })
      .addMatcher(
        (action: UnknownAction): action is UnknownAction =>
          action.type.startsWith("entities/") ||
          action.type.startsWith("metadata/") ||
          action.type.startsWith("settings/") ||
          action.type.startsWith("sprite/detect/fulfilled"),
        (state, _action) => {
          state.modified = true;
        }
      ),
});
 
const { reducer } = documentSlice;
 
export default reducer;