All files / src/background/redux/reducers filesystem.ts

44.44% Statements 4/9
50% Branches 3/6
50% Functions 1/2
44.44% Lines 4/9

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                    2x               7x                             1x                   6x      
import {
  ADD_TO_SELECTED,
  CLEAR_SELECTED,
  FilesystemActionTypes,
  FilesystemState,
  REMOVE_FROM_SELECTED,
  REPLACE_SELECTED
} from "../actions/filesystemTypes";
import { FsEntity } from "../../api/filesystemTypes";
 
const initialState: FilesystemState = {
  selectedFsEnties: []
};
 
export default function filesystem(
  state = initialState,
  action: FilesystemActionTypes
) {
  switch (action.type) {
    case ADD_TO_SELECTED: {
      console.log("[REDUX] ADD_TO_SELECTED", action.payload);
      return {
        selectedFsEnties: [...state.selectedFsEnties, action.payload] //concat because it does not modify the original array
      };
    }
    case REMOVE_FROM_SELECTED: {
      return {
        selectedFsEnties: state.selectedFsEnties.filter(
          (e: FsEntity) => e.fileSystemId !== action.payload.fileSystemId
        ) //filter return a new array
      };
    }
    case CLEAR_SELECTED: {
      return {
        selectedFsEnties: []
      };
    }
    case REPLACE_SELECTED: {
      return {
        selectedFsEnties: action.payload
      };
    }
    default:
      return state;
  }
}