public.ts 4 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
33
      const {apiCall} = action.payload;

      const receivedIndex = state.paged && state.paged.data ? _.findIndex(state.paged.data.content, (item) => item.uuid === apiCall.uuid) : -1;
      const mustRemoveAccessions = state.subset && state.subset.data && (apiCall.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
43
44
45
46
            },
          },
        });
      } else {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
47
          subset: { $set: apiCall },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
48
49
        });
      }
Matija Obreza's avatar
Subsets  
Matija Obreza committed
50
51
    }

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

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

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
67
    case APPEND_SUBSETS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
68
69
70
71
72
73
74
75
      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
76
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
77
78
        },
      });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
79
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    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
96
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
97

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

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

    case UPDATE_SUBSET_CREATOR: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
121
122
123
      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
124
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
125
          subset: { data: { creators: { [index]: { $set: apiCall.data } } } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
126
127
128
129
130
        });
      } else {
        return state;
      }
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
131

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

137
export default publicSubsets;