dashboard.ts 6.64 KB
Newer Older
1
2
3
4
import update from 'immutability-helper';
import { log } from 'utilities/debug';
import * as _ from 'lodash';

5
import { CREATE_DATASET, ADD_CREATOR_TO_DATASET, REMOVE_CREATOR_FROM_DATASET, UPDATE_DATASET_CREATOR, ADD_LOCATION,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
6
7
  RECEIVE_LOCATION, REMOVE_LOCATION, DASHBOARD_REMOVE_DATASET, RECEIVE_DATASET,
  DASHBOARD_APPEND_DATASET_PAGE , DASHBOARD_APPEND_ACCESSIONS_PAGE,
8
} from 'datasets/constants';
9
import {LOGOUT} from 'constants/login';
10

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
11
12
import {dereferenceReferences} from 'utilities';

13
import Dataset from 'model/catalog/Dataset';
14
import { AccessionRef } from 'model/accession/AccessionRef';
15
import Page from 'model/Page';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
16
17
import ApiCall from 'model/ApiCall';
import FilteredPage from 'model/FilteredPage';
18
19

const INITIAL_STATE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
20
21
22
  dataset: ApiCall<Dataset>,
  paged: ApiCall<Page<Dataset>>,
  accessionRefs: ApiCall<Page<AccessionRef>>,
23
24
25
} = {
  dataset: null,
  paged: null,
26
  accessionRefs: null,
27
28
29
30
31
};

function datasetsDashboard(state = INITIAL_STATE, action: { type?: string, payload?: any } = { type: '', payload: {} }) {

  switch (action.type) {
32
33
34
    case LOGOUT: {
      return update(state, { $set: INITIAL_STATE });
    }
35
36
37
38
    case CREATE_DATASET: {
      const dataset: Dataset = new Dataset();
      dataset.versionTag = '1.0';
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
39
        dataset: { $set: ApiCall.success(dataset) },
40
41
42
      });
    }
    case RECEIVE_DATASET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
43
44
45
46
47
      const {apiCall} = action.payload;

      const receivedIndex = state.paged && state.paged.data && apiCall.data ? _.findIndex(state.paged.data.content, (item) => item.uuid === apiCall.data.uuid) : -1;
      const mustRemoveAccessions = state.dataset && state.dataset.data && apiCall.data  && (apiCall.data.uuid !== state.dataset.data.uuid);
      apiCall.data = apiCall && apiCall.loading && state.dataset ? state.dataset.data : apiCall.data;
48
49
50

      if (receivedIndex !== -1) {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
51
          dataset: { $set: apiCall },
52
          accessionRefs: { $set: mustRemoveAccessions ? null : state.accessionRefs },
53
          paged: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
54
55
56
57
            data: {
              content: {
                [receivedIndex]: {$set: apiCall.data},
              },
58
59
60
61
62
            },
          },
        });
      } else {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
63
64
          dataset: { $set: apiCall },
          paged: apiCall.loading ? { $set: state.paged } : { $set: null },
65
          accessionRefs: { $set: mustRemoveAccessions ? null : state.accessionRefs },
66
67
68
69
        });
      }
    }

70
    case DASHBOARD_APPEND_ACCESSIONS_PAGE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
71
72
73
74
75
76
77
78
      const {apiCall: {loading, error, timestamp, data}} = action.payload;

      return update(state, {
        accessionRefs: {
          $set: {
            loading,
            error,
            timestamp,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
79
            data: Page.merge(state.accessionRefs && state.accessionRefs.data, data),
80
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
81
82
        },
      });
83
84
    }

85
    case DASHBOARD_REMOVE_DATASET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
86
      const {apiCall} = action.payload;
87
      if (state.paged) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
88
        const removeIndex = state.paged && state.paged.data && apiCall.data ? _.findIndex(state.paged.data.content, (item) => item.uuid === apiCall.data.uuid) : -1;
89
90
91
        return removeIndex === -1 ? state
          : update(state, {
            paged: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
92
93
94
95
96
              data: {
                content: {$splice: [[removeIndex, 1]]},
                numberOfElements: {$set: state.paged.data.number - 1},
                totalElements: {$set: state.paged.data.totalElements - 1},
              },
97
98
99
100
101
102
            },
          });
      }
      return state;
    }
    case DASHBOARD_APPEND_DATASET_PAGE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
103
104
105
106
107
108
109
110
111
112
113
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
      if (data) {
        dereferenceReferences(data.content, 'owner');
      }
      return update(state, {
        paged: {
          $set: {
            loading,
            error,
            timestamp,
            data: FilteredPage.merge(state.paged && state.paged.data, data),
114
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
115
116
        },
      });
117
118
119
120
    }

    // Creators step
    case ADD_CREATOR_TO_DATASET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
121
122
123
      const {apiCall} = action.payload;
      log('Payload ADD_CREATOR_TO_DATASET', apiCall);
      if (state.dataset && apiCall.data) { // && action.payload.datasetUUID === state.dataset.uuid) {
124
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
125
          dataset: { data: { creators: { $push: [apiCall.data] } }},
126
127
128
129
130
131
132
        });
      } else {
        return state;
      }
    }

    case REMOVE_CREATOR_FROM_DATASET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
133
134
135
      const {apiCall} = action.payload;
      log('Payload REMOVE_CREATOR_FROM_DATASET', apiCall);
      if (state.dataset && apiCall.data) { // && action.payload.datasetUUID === state.dataset.uuid) {
136
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
137
          dataset: { data: { creators: { $apply: (creators) => creators.filter((creator) => creator.uuid !== apiCall.data.uuid) } } },
138
139
140
141
142
143
144
        });
      } else {
        return state;
      }
    }

    case UPDATE_DATASET_CREATOR: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
145
146
147
148
      const {apiCall} = action.payload;
      log('Payload UPDATE_DATASET_CREATOR', apiCall);
      if (state.dataset && apiCall.data) { // && action.payload.datasetUUID === state.dataset.uuid) {
        const index = state.dataset.data && state.dataset.data.creators.findIndex((creator) => creator.uuid === apiCall.data.uuid);
149
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
150
          dataset: { data: { creators: { [index]: { $set: apiCall.data } } } },
151
152
153
154
155
156
157
158
        });
      } else {
        return state;
      }
    }

    // Location step
    case ADD_LOCATION: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
159
160
161
      const {apiCall} = action.payload;
      log('Payload ADD_LOCATION', apiCall);
      if (state.dataset && apiCall.data) { // && action.payload.datasetUUID === state.dataset.uuid) {
162
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
163
          dataset: { data: { locations: { $push: [apiCall] } } },
164
165
166
167
168
169
170
        });
      } else {
        return state;
      }
    }

    case REMOVE_LOCATION: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
171
      const {apiCall} = action.payload;
172
      log('Payload REMOVE_LOCATION', action);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
173
      if (state.dataset && apiCall.data) { // && action.payload.datasetUUID === state.dataset.uuid) {
174
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
175
          dataset: { data: { locations: { $apply: (locations) => locations.filter((location) => location.uuid !== apiCall.data.uuid) } } },
176
177
178
179
180
181
182
        });
      } else {
        return state;
      }
    }

    case RECEIVE_LOCATION: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
183
184
185
186
      const {apiCall} = action.payload;
      log('Payload RECEIVE_LOCATION', apiCall);
      if (state.dataset && apiCall.data) { // && action.payload.datasetUUID === state.dataset.uuid) {
        const index = state.dataset.data.locations.findIndex((location) => location.uuid === apiCall.data.uuid);
187
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
188
          dataset: { data: { locations: { [index]: { $set: apiCall.data } } } },
189
190
191
192
193
194
195
196
197
198
199
200
        });
      } else {
        return state;
      }
    }

    default:
      return state;
  }
}

export default datasetsDashboard;