public.ts 4.15 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';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
4 5
import {RECEIVE_SUBSET, REMOVE_SUBSET, APPEND_SUBSETS, ADD_CREATOR_TO_SUBSET, REMOVE_CREATOR_FROM_SUBSET, UPDATE_SUBSET_CREATOR,
  APPEND_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';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
11
import ApiCall from 'model/ApiCall';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
12

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

Matija Obreza's avatar
Subsets  
Matija Obreza committed
15
const INITIAL_STATE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
16 17 18
  accessionRefs: ApiCall<Page<AccessionRef>>;
  subset: ApiCall<Subset>;
  paged: ApiCall<FilteredPage<Subset>>;
Matija Obreza's avatar
Subsets  
Matija Obreza committed
19
} = {
20
  accessionRefs: null,
Matija Obreza's avatar
Subsets  
Matija Obreza committed
21 22 23 24
  subset: null,
  paged: null,
};

25
function publicSubsets(state = INITIAL_STATE, action: IReducerAction) {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
26 27 28 29

  switch (action.type) {

    case RECEIVE_SUBSET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
30 31 32
      const {apiCall} = action.payload;

      const receivedIndex = state.paged && state.paged.data ? _.findIndex(state.paged.data.content, (item) => item.uuid === apiCall.uuid) : -1;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
33
      const mustRemoveAccessions = state.subset && state.subset.data  && apiCall.data && (apiCall.data.uuid !== state.subset.data.uuid);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
34 35 36

      if (receivedIndex !== -1) {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
37 38
          subset: { $set: apiCall },
          accessionRefs: { $set: mustRemoveAccessions ? null : state.accessionRefs },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
39 40
          paged: {
            content: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
41
              [receivedIndex]: { $set: apiCall },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
42
            },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
43
            loading: {$set: false},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
44 45 46 47
          },
        });
      } else {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
48
          subset: { $set: apiCall },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
49
          accessionRefs: { $set: mustRemoveAccessions ? null : state.accessionRefs },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
50 51
        });
      }
Matija Obreza's avatar
Subsets  
Matija Obreza committed
52 53
    }

54
    case APPEND_SUBSET_ACCESSIONS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
55
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
56

Matija Obreza's avatar
Subsets  
Matija Obreza committed
57
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
58 59 60 61 62
        accessionRefs: {
          $set: {
            loading,
            error,
            timestamp,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
63
            data: Page.merge(state.accessionRefs && state.accessionRefs.data, data),
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
64 65
          },
        },
Matija Obreza's avatar
Subsets  
Matija Obreza committed
66 67 68
      });
    }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
69
    case APPEND_SUBSETS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
70 71 72 73 74 75 76 77
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
      return update(state, {
        paged: {
          $set: {
            loading,
            error,
            timestamp,
            data: FilteredPage.merge(state.paged && state.paged.data, data),
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
78
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
79 80
        },
      });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
81
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    case REMOVE_SUBSET:  {
      const {apiCall} = action.payload;
      if (state.paged) {
        const removeIndex = state.paged.data ? _.findIndex(state.paged.data.content, (item) => item.uuid === apiCall.data.uuid) : -1;
        return removeIndex === -1 ? state
          : update(state, {
            paged: {
              data: {
                content: {$splice: [[removeIndex, 1]]},
                numberOfElements: {$set: state.paged.data.number - 1},
                totalElements: {$set: state.paged.data.totalElements - 1},
              },
            },
          });
      }
      return state;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
98
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
99

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
100
    case ADD_CREATOR_TO_SUBSET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
101 102
      const {apiCall} = action.payload;
      if (state.subset && apiCall.data) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
103
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
104
          subset: { data: { creators: { $push: [ apiCall.data ] } } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
105 106 107 108 109 110 111
        });
      } else {
        return state;
      }
    }

    case REMOVE_CREATOR_FROM_SUBSET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
112 113
      const {apiCall} = action.payload;
      if (state.subset && apiCall.data) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
114
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
115
          subset: { data: { creators: { $apply: (creators) => creators.filter((creator) => creator.uuid !== apiCall.data.uuid) } } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
116 117 118 119 120 121 122
        });
      } else {
        return state;
      }
    }

    case UPDATE_SUBSET_CREATOR: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
123 124 125
      const {apiCall} = action.payload;
      if (state.subset && apiCall.data) { // && action.payload.datasetUUID === state.dataset.uuid) {
        const index = state.subset.data && state.subset.data.creators.findIndex((creator) => creator.uuid === apiCall.data.uuid);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
126
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
127
          subset: { data: { creators: { [index]: { $set: apiCall.data } } } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
128 129 130 131 132
        });
      } else {
        return state;
      }
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
133

Matija Obreza's avatar
Subsets  
Matija Obreza committed
134 135 136 137 138
    default:
      return state;
  }
}

139
export default publicSubsets;