All files / src/apps/sources/redux reducer.ts

13.33% Statements 2/15
0% Branches 0/2
0% Functions 0/10
14.29% Lines 2/14

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                              1x           1x                                                                                    
import { createReducer } from "@reduxjs/toolkit";
import { Action } from "../../../redux";
import { SourceState } from "../types";
import {
  CREATE_SOURCE_ACTION,
  EDIT_SOURCE_ACTION,
  RETRIEVE_SOURCE_ACTION,
  RETRIEVE_SOURCES_ACTION,
  RETRIEVE_SOURCE_VERSIONS_ACTION,
  EDIT_SOURCE_VERSION_ACTION,
  CREATE_SOURCE_VERSION_ACTION,
  TOGGLE_SHOW_VERIFIED_ACTION
} from "./actionTypes";
import { LOGOUT_ACTION } from "../../authentication/redux/actionTypes";
 
const initialState: SourceState = {
  sources: [],
  versions: [],
  showOnlyVerified: false
};
 
export const reducer = createReducer(initialState, {
  [TOGGLE_SHOW_VERIFIED_ACTION]: (state, action) => ({
    ...state,
    showOnlyVerified: !state.showOnlyVerified
  }),
  [CREATE_SOURCE_ACTION]: (state, action) => ({
    ...state,
    newSource: action.payload
  }),
  [RETRIEVE_SOURCES_ACTION]: (
    state,
    { actionIndex, payload, responseMeta }: Action
  ) => {
    state.sources[actionIndex] = { items: payload, responseMeta };
  },
  [RETRIEVE_SOURCE_ACTION]: (state, action) => ({
    ...state,
    source: action.payload
  }),
  [EDIT_SOURCE_ACTION]: (state, action) => ({
    ...state,
    editedSource: action.payload
  }),
  [RETRIEVE_SOURCE_VERSIONS_ACTION]: (state, action) => ({
    ...state,
    versions: action.payload
  }),
  [EDIT_SOURCE_VERSION_ACTION]: (state, { actionIndex, payload }) => {
    const versionIndex = state.versions.findIndex(
      version => version.id === payload.id
    );
    if (versionIndex !== -1) state.versions[versionIndex] = payload;
    else state.versions.push(payload);
  },
  [CREATE_SOURCE_VERSION_ACTION]: (state, { actionIndex, payload, meta }) => {
    state.versions = [payload, ...state.versions];
  },
  [LOGOUT_ACTION]: () => {
    return initialState;
  }
});
export { reducer as default };