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

12.5% Statements 2/16
0% Branches 0/6
0% Functions 0/11
13.33% Lines 2/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 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                          1x                   1x                                                                                                                  
import { createReducer } from "@reduxjs/toolkit";
import { APIConcept, ConceptsState } from "../types";
import { startAction } from "../../../redux";
import {
  RETRIEVE_ACTIVE_CONCEPTS_ACTION,
  RETRIEVE_CONCEPT_ACTION,
  RETRIEVE_CONCEPTS_ACTION,
  UPSERT_CONCEPT_ACTION,
  UPSERT_MAPPING_ACTION
} from "./actionTypes";
import { REMOVE_REFERENCES_FROM_DICTIONARY } from "../../dictionaries/redux/actionTypes";
import { LOGOUT_ACTION } from "../../authentication/redux/actionTypes";
 
const initialState: ConceptsState = {
  mappings: [],
  concepts: {
    items: []
  },
  activeConcepts: {
    items: []
  }
};
 
export const reducer = createReducer<ConceptsState>(initialState, {
  [startAction(UPSERT_CONCEPT_ACTION).type]: state => ({
    ...state,
    concept: undefined
  }),
  [startAction(RETRIEVE_CONCEPT_ACTION).type]: state => ({
    ...state,
    concept: undefined
  }),
  [UPSERT_CONCEPT_ACTION]: (state, action) => ({
    ...state,
    concept: action.payload
  }),
  [RETRIEVE_CONCEPT_ACTION]: (state, { payload }) => ({
    ...state,
    concept: payload,
    mappings: payload.mappings || []
  }),
  [RETRIEVE_CONCEPTS_ACTION]: (state, action) => ({
    ...state,
    concepts: {
      items: action.payload as APIConcept[],
      responseMeta: action.responseMeta
    }
  }),
  [RETRIEVE_ACTIVE_CONCEPTS_ACTION]: (state, action) => ({
    ...state,
    activeConcepts: {
      items: (action.payload as APIConcept[]) || [],
      responseMeta: action.responseMeta
    }
  }),
  [UPSERT_MAPPING_ACTION]: (state, { actionIndex, payload, meta }) => {
    const mappingIndex = state.mappings.findIndex(
      mapping => mapping.external_id === payload.external_id
    );
    if (mappingIndex !== -1) state.mappings[mappingIndex] = payload;
    else state.mappings.push(payload);
  },
  [REMOVE_REFERENCES_FROM_DICTIONARY]: (
    state,
    {
      actionIndex,
      payload,
      meta
    }: { actionIndex: number; payload: {}; meta: [string, string[]] }
  ) => {
    state.concepts.items = state.concepts.items.filter(
      (concept: APIConcept) => !meta[1].includes(concept.version_url)
    );
  },
  [LOGOUT_ACTION]: () => {
    return initialState;
  }
});
 
export default reducer;