public.ts 4.34 KB
Newer Older
Matija Obreza's avatar
Subsets  
Matija Obreza committed
1 2 3
import update from 'immutability-helper';

import { IReducerAction } from 'model/common.model';
4 5
import {RECEIVE_SUBSETS, RECEIVE_SUBSET, REMOVE_SUBSET, APPEND_SUBSETS, ADD_CREATOR_TO_SUBSET, REMOVE_CREATOR_FROM_SUBSET, UPDATE_SUBSET_CREATOR,
  APPEND_SUBSET_ACCESSIONS, RECEIVE_SUBSET_ACCESSIONS} from 'subsets/constants';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
6 7

import FilteredPage from 'model/FilteredPage';
8
import Subset from 'model/subset/Subset';
9 10
import {AccessionRef} from 'model/accession/AccessionRef';
import Page from 'model/Page';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
11

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
12 13
import * as _ from 'lodash';

Matija Obreza's avatar
Subsets  
Matija Obreza committed
14
const INITIAL_STATE: {
15 16
  accessionRefs: Page<AccessionRef>;
  accessionsError: any;
Matija Obreza's avatar
Subsets  
Matija Obreza committed
17 18 19 20 21
  subset: Subset;
  subsetError: any;
  paged: FilteredPage<Subset>;
  pagedError: any;
} = {
22 23
  accessionRefs: null,
  accessionsError: null,
Matija Obreza's avatar
Subsets  
Matija Obreza committed
24 25 26 27 28 29
  subset: null,
  subsetError: null,
  paged: null,
  pagedError: null,
};

30
function publicSubsets(state = INITIAL_STATE, action: IReducerAction) {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
31 32 33 34 35

  switch (action.type) {

    case RECEIVE_SUBSET: {
      const { subset, error } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
36
      const receivedIndex = state.paged ? state.paged.content.findIndex((item) => item.uuid === subset.uuid) : -1;
37
      const mustRemoveAccessions = state.subset && (subset.uuid !== state.subset.uuid);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
38 39 40 41 42 43 44 45 46 47

      if (receivedIndex !== -1) {
        return update(state, {
          subset: { $set: subset },
          paged: {
            content: {
              [receivedIndex]: {$set: subset},
            },
          },
          subsetError: {$set: error},
48
          accessionRefs: { $set: mustRemoveAccessions ? null : state.accessionRefs },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
49 50 51 52 53 54 55 56
        });
      } else {
        return update(state, {
          subset: { $set: subset},
          paged: {$set: null},
          subsetError: {$set: error},
        });
      }
Matija Obreza's avatar
Subsets  
Matija Obreza committed
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
    case RECEIVE_SUBSET_ACCESSIONS: {
      const { accessionRefs, error } = action.payload;
      return update(state, {
        accessionRefs: { $set: accessionRefs },
        accessionsError: { $set: error },
      });
    }

    case APPEND_SUBSET_ACCESSIONS: {
      const {accessionRefs, error} = action.payload;

      return !state.accessionRefs ? update(state, {
          accessionRefs: {$set: accessionRefs},
          accessionsError: {$set: error},
        }) :
        update(state, {
          accessionRefs: {
            content: {$push: accessionRefs.content},
            number: {$set: accessionRefs.number},
            last: {$set: accessionRefs.last},
          },
          accessionsError: {$set: error},
        });
    }

Matija Obreza's avatar
Subsets  
Matija Obreza committed
84 85 86 87 88 89 90 91
    case RECEIVE_SUBSETS: {
      const { paged, error } = action.payload;
      return update(state, {
        paged: { $set: paged },
        pagedError: { $set: error },
      });
    }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    case APPEND_SUBSETS: {
      const {paged, error} = action.payload;

      return !state.paged ? update(state, {
          paged: {$set: paged},
          pagedError: {$set: error},
        }) :
        update(state, {
          paged: {
            content: {$push: paged.content},
            number: {$set: paged.number},
            last: {$set: paged.last},
          },
          pagedError: {$set: error},
        });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
108 109 110 111 112 113 114 115 116 117
    case REMOVE_SUBSET: {
      const removeIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.subset.uuid) : -1;
      return removeIndex === -1 ? state
        : update(state, {
          paged: {
            content: {$splice: [[removeIndex, 1]]},
            totalElements: {$set: state.paged.totalElements - 1},
          },
        });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    case ADD_CREATOR_TO_SUBSET: {
      if (state.subset) {
        return update(state, {
          subset: { creators: { $push: [ action.payload.creator ] } },
        });
      } else {
        return state;
      }
    }

    case REMOVE_CREATOR_FROM_SUBSET: {
      if (state.subset) {
        return update(state, {
          subset: { creators: { $apply: (creators) => creators.filter((creator) => creator.uuid !== action.payload.creator.uuid) } },
        });
      } else {
        return state;
      }
    }

    case UPDATE_SUBSET_CREATOR: {
      if (state.subset) {
        const index = state.subset.creators.findIndex((creator) => creator.uuid === action.payload.creator.uuid);
        return update(state, {
          subset: { creators: { [index]: { $set: action.payload.creator } } },
        });
      } else {
        return state;
      }
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
148

Matija Obreza's avatar
Subsets  
Matija Obreza committed
149 150 151 152 153
    default:
      return state;
  }
}

154
export default publicSubsets;