editor.ts 12 KB
Newer Older
1
2
3
4
5
import { push } from 'react-router-redux';
import * as _ from 'lodash';

// Actions
import { navigateTo } from 'actions/navigation';
6
import {loadMoreAccessions} from 'datasets/actions/dashboard';
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

// Constants
import { ADD_CREATOR_TO_DATASET, ADD_LOCATION, RECEIVE_LOCATION, REMOVE_CREATOR_FROM_DATASET, REMOVE_LOCATION, UPDATE_DATASET_CREATOR, RECEIVE_DATASET, DASHBOARD_REMOVE_DATASET } from 'datasets/constants';

// Models
import Dataset from 'model/catalog/Dataset';
import { AccessionRef } from 'model/accession/AccessionRef';
import RepositoryFile from 'model/repository/RepositoryFile';
import DatasetCreator from 'model/catalog/DatasetCreator';
import DatasetLocation from 'model/catalog/DatasetLocation';

// Service
import DatasetService from 'service/catalog/DatasetService';

// Utility
import { log } from 'utilities/debug';

// UI
import steps from 'datasets/ui/dashboard/dataset-stepper/steps';

const removeDataset = (dataset: Dataset) => ({
  type: DASHBOARD_REMOVE_DATASET, payload: dataset,
});

const receiveDataset = (dataset: Dataset) => ({
  type: RECEIVE_DATASET, payload: dataset,
});

function showDataset(uuid: string) {
  return (dispatch) => {
    dispatch(push(`/datasets/${uuid}`));
  };
}

export const createDataset = () => (dispatch) => {
  return dispatch(receiveDataset(new Dataset()));
};

// Review step
function rejectDataset(dataset: Dataset, needToRedirect: boolean = true) {
  return (dispatch, getState) => {

    return DatasetService.rejectDataset(dataset.uuid, dataset.version)
      .then((saved) => {
        dispatch(receiveDataset(saved));
        if (needToRedirect) {
          dispatch(navigateTo(`/dashboard/datasets/${saved.uuid}/edit`));
        }
      }).catch((error) => {
        log('Error', error);
      });
  };
}

function approveDataset(dataset: Dataset) {
  return (dispatch, getState) => {

    return DatasetService.approveDataset(dataset.uuid, dataset.version)
      .then((saved) => {
        dispatch(receiveDataset(saved));
      }).catch((error) => {
        log('Error', error);
      });
  };
}

function publishDataset(dataset: Dataset, needToRedirect: boolean = false) {
  return (dispatch, getState) => {

    return DatasetService.reviewDataset(dataset.uuid, dataset.version)
      .then((saved) => {
        dispatch(receiveDataset(saved));
        if (needToRedirect) {
          dispatch(showDataset(saved.uuid));
        }
      }).catch((error) => {
        log('Error', error);
      });
  };
}

function deleteDataset(dataset: Dataset) {
  return (dispatch, getState) => {

    return DatasetService.deleteDataset(dataset.uuid, dataset.version)
      .then((deleted) => {
        dispatch(removeDataset(deleted));
        // dispatch(showDataset(saved.uuid));
      }).catch((error) => {
        log('Delete error', error);
      });
  };
}

// Review step exports
export { rejectDataset, approveDataset, publishDataset, deleteDataset };


// Accession identifiers step
106
107
function clearAccessionRefList(dataset: Dataset) {
  return (dispatch, getState) => {
108
    return DatasetService.setAccessions(dataset.uuid, dataset.version, [])
109
110
111
112
113
114
115
116
117
      .then((saved) => {
        dispatch(receiveDataset(saved));
        dispatch(loadMoreAccessions(saved.uuid));
      }).catch((error) => {
        log('Publish error', error);
      });
  };
}

118
function addDatasetAccessionRefs(dataset: Dataset, accessionRefs: AccessionRef[]) {
119
120
  return (dispatch, getState) => {

121
    return DatasetService.addAccessions(dataset.uuid, dataset.version, accessionRefs)
122
123
      .then((saved) => {
        dispatch(receiveDataset(saved));
124
        dispatch(loadMoreAccessions(saved.uuid));
125
        return saved;
126
127
      }).catch((error) => {
        log('Publish error', error);
128
129
        throw error;
    });
130
131
132
  };
}

133
134
135
136
137
138
139
140
141
142
143
144
function rematchDatasetAccessions(dataset: Dataset) {
  return (dispatch, getState) => {
    return DatasetService.rematchDatasetAccessions(dataset.uuid, dataset.version)
      .then((saved) => {
        dispatch(receiveDataset(saved));
        dispatch(loadMoreAccessions(saved.uuid));
      }).catch((error) => {
        log('Publish error', error);
      });
  };
}

145
// Accession identifiers step exports
146
export { addDatasetAccessionRefs as addDatasetAccessionRefs, rematchDatasetAccessions, clearAccessionRefList };
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435


// Descriptors step
function addDescriptorsToDataset(dataset: Dataset, descriptorUuids: string[]) {
  return (dispatch, getState) => {


    return DatasetService.addDescriptors(dataset.uuid, dataset.version, descriptorUuids)
      .then((saved) => {
        dispatch(receiveDataset(saved));
      }).catch((error) => {
        log('Add error', error);
      });
  };
}

const setDescriptorsToDatasetRequest = (dataset: Dataset, descriptorUuids: string[]) => (dispatch, getState) => {
  return DatasetService.updateDescriptors(dataset.uuid, dataset.version, descriptorUuids)
    .then((saved) => {
      dispatch(receiveDataset(saved));
    }).catch((error) => {
      log('Add error', error);
    });
};

function removeDescriptorsFromDataset(dataset: Dataset, descriptorUuids: string[]) {
  return (dispatch, getState) => {

    return DatasetService.removeDescriptors(dataset.uuid, dataset.version, descriptorUuids)
      .then((saved) => {
        dispatch(receiveDataset(saved));
      }).catch((error) => {
        log('Delete error', error);
      });
  };
}

// Descriptors step exports
export { addDescriptorsToDataset, setDescriptorsToDatasetRequest, removeDescriptorsFromDataset };


// Repository File step
const uploadRepositoryFileRequest = (file: File) => (dispatch, getState) => {
  const currentDataset = getState().datasets.dashboard.dataset;

  return DatasetService.addFileToDataset(currentDataset.uuid, file)
    .then((dataset) => {
      dispatch(receiveDataset(dataset));
    }).catch((error) => {
      log('Save error', error);
    });
};

const updateRepositoryFileRequest = (repositoryfile: RepositoryFile) => (dispatch, getState) => {
  const currentDataset = getState().datasets.dashboard.dataset;

  return DatasetService.updateDatasetFile(currentDataset.uuid, repositoryfile)
    .then((dataset) => {
      dispatch(receiveDataset(dataset));
    }).catch((error) => {
      log('Save error', error);
    });
};

const deleteRepositoryFileRequest = (uuid: string) => (dispatch, getState) => {
  const currentDataset = getState().datasets.dashboard.dataset;

  return DatasetService.removeFileOfDataset(currentDataset.uuid, uuid)
    .then((dataset) => {
      dispatch(receiveDataset(dataset));
    }).catch((error) => {
      log('Delete error', error);
    });
};

// Repository File step exports
export { uploadRepositoryFileRequest, updateRepositoryFileRequest, deleteRepositoryFileRequest };

// Creators step
function addCreatorToDataset(creator: DatasetCreator) {
  return {
    type: ADD_CREATOR_TO_DATASET,
    payload: { creator },
  };
}

function updateCreator(creator: DatasetCreator) {
  return {
    type: UPDATE_DATASET_CREATOR,
    payload: { creator },
  };
}

function removeCreator(creator: DatasetCreator) {
  return {
    type: REMOVE_CREATOR_FROM_DATASET,
    payload: { creator },
  };
}


function createDatasetCreator() {
  return (dispatch, getState) => {
    const creator: DatasetCreator = new DatasetCreator();
    creator.fullName = 'Dataset creator';
    const currentDataset = getState().datasets.dashboard.dataset;
    log('createCreator');
    return DatasetService.createDatasetCreator(currentDataset.uuid, creator)
      .then((obj) => {
        dispatch(addCreatorToDataset(obj));
      }).catch((error) => {
        log('Create creator error', error);
      });
  };
}

function updateCreatorRequest(creator: DatasetCreator) {
  return (dispatch, getState) => {
    const currentDataset = getState().datasets.dashboard.dataset;

    return DatasetService.updateDatasetCreator(currentDataset.uuid, creator)
      .then((obj) => {
        dispatch(updateCreator(obj));
      }).catch((error) => {
        log('Update creator error', error);
      });
  };
}

function deleteCreatorRequest(creator: DatasetCreator) {
  return (dispatch, getState) => {
    const currentDataset = getState().datasets.dashboard.dataset;

    return DatasetService.deleteDatasetCreator(currentDataset.uuid, creator)
      .then((obj) => {
        dispatch(removeCreator(obj));
      }).catch((error) => {
        log('Delete creator error', error);
      });
  };
}

// Creators step exports
export { createDatasetCreator, updateCreatorRequest, deleteCreatorRequest };


// Timing and Location step
function addLocation(location: DatasetLocation) {
  return {
    type: ADD_LOCATION,
    payload: { location },
  };
}

function receiveLocation(location: DatasetLocation) {
  return {
    type: RECEIVE_LOCATION,
    payload: {
      location,
    },
  };
}

function deleteLocation(location: DatasetLocation) {
  return {
    type: REMOVE_LOCATION,
    payload: { location },
  };
}

function createLocationRequest() {
  const location = new DatasetLocation();

  return (dispatch, getState) => {
    const currentDataset = getState().datasets.dashboard.dataset;

    return DatasetService.createLocation(currentDataset.uuid, location)
      .then((saved) => {
        dispatch(addLocation(saved));
      }).catch((error) => {
        log('Save error', error);
      });
  };
}

function updateLocationRequest(location: DatasetLocation) {

  return (dispatch, getState) => {
    const currentDataset = getState().datasets.dashboard.dataset;

    return DatasetService.updateLocation(currentDataset.uuid, location)
      .then((saved) => {
        dispatch(receiveLocation(saved));
        dispatch(receiveDataset(refreshStartEndDates(getState().datasets.dashboard.dataset)));
      }).catch((error) => {
        log('Save error', error);
      });
  };
}

function deleteLocationRequest(location: DatasetLocation) {
  return (dispatch, getState) => {
    const currentDataset = getState().datasets.dashboard.dataset;

    return DatasetService.deleteLocation(currentDataset.uuid, location)
      .then((saved) => {
        dispatch(deleteLocation(saved));
        dispatch(receiveDataset(refreshStartEndDates(getState().datasets.dashboard.dataset)));
      }).catch((error) => {
        log('Delete error', error);
      });
  };
}

// Timing and Location step exports
export { createLocationRequest, updateLocationRequest, deleteLocationRequest, receiveLocation };


// Common actions
function loadDataset(uuid: string) {
  return (dispatch, getState) => {


    return DatasetService.getDataset(uuid)
      .then((dataset) => {
        dispatch(receiveDataset(dataset));
      })
      .catch((error) => {
        log('Error', error);
      });
  };
}

const saveDataset = (dataset: Dataset) => (dispatch, getState) => {

  const needToRedirect: boolean = !(dataset.version && dataset.uuid);

  if (_.isEqual({ ...getState().datasets.dashboard.dataset }, { ...dataset })) {
    return;
  }

  const saveOrUpdate = dataset.id && dataset.version ? DatasetService.updateDataset : DatasetService.createDataset;

  // remove normalized data here
  const data: Dataset = {
    repositoryFiles: [],
    creators: [],
    locations: [],
    descriptors: [],
    ...dataset,
  };

  return saveOrUpdate(data)
    .then((saved) => {
      dispatch(receiveDataset(saved));
      if (needToRedirect) {
        dispatch(gotoNextStep(saved));
      }
    }).catch((error) => {
      log('Save error', error);
    });
};

function gotoNextStep(dataset: Dataset) {
  return (dispatch, getState) => {
    const link = window.location.pathname.split('/').pop();
    const stepId = steps.find((e) => e.path.endsWith(link)).id;
    const path = steps.find((e) => e.id === (stepId + 1)).path;
    dispatch(navigateTo(`/dashboard/datasets/${dataset.uuid}/${path}`));
  };
}

function refreshStartEndDates(dataset) {
  const res = { ...dataset };
  res.startDate = null;
  res.endDate = null;
  res.locations.forEach((location) => {
    if (!res.startDate || (location.startDate && res.startDate.localeCompare(location.startDate) > 0)) {
      res.startDate = location.startDate;
    }
    if (!res.endDate || (location.endDate && res.endDate.localeCompare(location.endDate) < 0)) {
      res.endDate = location.endDate;
    }
  });
  return res;
}

// Common actions exports
export { loadDataset, saveDataset };