dataset.ts 12.3 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
import { push } from 'react-router-redux';
2

Matija Obreza's avatar
Matija Obreza committed
3
import { Page } from 'model/common.model';
Matija Obreza's avatar
Matija Obreza committed
4
import { Dataset, IDatasetFilter, AccessionIdentifier } from 'model/dataset.model';
Matija Obreza's avatar
Matija Obreza committed
5
import { Creator } from 'model/creator.model';
Matija Obreza's avatar
Matija Obreza committed
6
import { Descriptor } from 'model/descriptor.model';
Matija Obreza's avatar
Matija Obreza committed
7
import { Location } from 'model/location.model';
Valeriy Panov's avatar
Valeriy Panov committed
8
import { RepositoryFile } from 'model/repositoryFile.model';
Matija Obreza's avatar
Matija Obreza committed
9
10
import { DatasetService } from 'service/DatasetService';
import { CreatorService } from 'service/CreatorService';
Matija Obreza's avatar
Matija Obreza committed
11
import { LocationService } from 'service/LocationService';
Matija Obreza's avatar
Matija Obreza committed
12
import { RepositoryFileService } from 'service/RepositoryFileService';
Valeriy Panov's avatar
Valeriy Panov committed
13
import {log} from 'utilities/debug';
Matija Obreza's avatar
Matija Obreza committed
14

Matija Obreza's avatar
Matija Obreza committed
15
import { CREATE_DATASET, RECEIVE_DATASET, RECEIVE_DATASET_PAGE, ADD_CREATOR_TO_DATASET, REMOVE_CREATOR_FROM_DATASET, UPDATE_DATASET_CREATOR, ADD_LOCATION, RECEIVE_LOCATION, REMOVE_LOCATION } from 'constants/datasets';
Maxym Borodenko's avatar
Maxym Borodenko committed
16
import {addFilterCode} from 'actions/filterCode';
Maxym Borodenko's avatar
Maxym Borodenko committed
17
18
19
import steps from 'ui/pages/dataset/dataset-stepper/steps';
import { navigateTo } from 'actions/navigation';
import * as _ from 'lodash';
Matija Obreza's avatar
Matija Obreza committed
20
21
22
23
24

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

Matija Obreza's avatar
Matija Obreza committed
25
26
27
const receiveDatasetPage = (paged: Page<Dataset>, page, results, sortBy, filter: IDatasetFilter, order) => ({
    type: RECEIVE_DATASET_PAGE,
    payload: { paged, query: { page, results, sortBy, filter, order } },
Matija Obreza's avatar
Matija Obreza committed
28
});
29
30

function showDataset(uuid: string) {
Matija Obreza's avatar
Matija Obreza committed
31
32
33
  return (dispatch) => {
    dispatch(push(`/datasets/${uuid}`));
  };
34
35
36
}

function editDataset(uuid: string) {
Matija Obreza's avatar
Matija Obreza committed
37
38
39
  return (dispatch) => {
    dispatch(push(`/datasets/${uuid}/edit`));
  };
40
41
}

42
function listMyDatasets(page?, results?, sortBy?, filter?: string | IDatasetFilter, order?) {
Matija Obreza's avatar
Matija Obreza committed
43
  return (dispatch, getState) => {
44
    const token = getState().login.access_token;
45

46
    return DatasetService.listMyDatasets(token, page, results, sortBy, filter, order)
Matija Obreza's avatar
Matija Obreza committed
47
      .then((paged) => {
48
49
        dispatch(receiveDatasetPage(paged, page, results, sortBy, paged.filter, order));
        return dispatch(addFilterCode(paged.filterCode, paged.filter));
Matija Obreza's avatar
Matija Obreza committed
50
51
      })
      .catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
52
        log('Error', error);
Matija Obreza's avatar
Matija Obreza committed
53
54
      });
  };
55
56
}

Valeriy Panov's avatar
Valeriy Panov committed
57
58
59
60
61
62
63
64
65
66
export const promiselistDatasets = (page?, results?, sortBy?, filter?, order?) => (dispatch, getState) => {
    const token = getState().login.access_token;

    return DatasetService.listDatasets(token, page, results, sortBy, filter, order)
        .catch((error) => {
            log('Error', error);
            return error;
        });
};

Valeriy Panov's avatar
Valeriy Panov committed
67
function listDatasetsRequest(page?, results?, sortBy?, filter?, order?) {
68
69
  return (dispatch, getState) => {
    const token = getState().login.access_token;
70

71
    return DatasetService.listDatasets(token, page, results, sortBy, filter, order)
Matija Obreza's avatar
Matija Obreza committed
72
      .then((paged) => {
Maxym Borodenko's avatar
Maxym Borodenko committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        dispatch(receiveDatasetPage(paged, page, results, sortBy, filter, order));
        return dispatch(addFilterCode(paged.filterCode, paged.filter));
      })
      .catch((error) => {
        log('Error', error);
      });
  };
}

function listDatasetsByCodeRequest(page?, results?, sortBy?, filterCode?, order?) {
  return (dispatch, getState) => {
    const token = getState().login.access_token;

    return DatasetService.listDatasetsByCode(token, page, results, sortBy, filterCode, order)
      .then((paged) => {
88
        dispatch(receiveDatasetPage(paged, page, results, sortBy, paged.filter, order));
Maxym Borodenko's avatar
Maxym Borodenko committed
89
        return dispatch(addFilterCode(paged.filterCode, paged.filter));
Matija Obreza's avatar
Matija Obreza committed
90
91
      })
      .catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
92
        log('Error', error);
Matija Obreza's avatar
Matija Obreza committed
93
94
      });
  };
95
96
}

Matija Obreza's avatar
Matija Obreza committed
97
function loadDataset(uuid: string) {
98
99
  return (dispatch, getState) => {
    const token = getState().login.access_token;
100

101
    return DatasetService.getDataset(token, uuid)
Matija Obreza's avatar
Matija Obreza committed
102
103
104
105
      .then((dataset) => {
        dispatch(receiveDataset(dataset));
      })
      .catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
106
        log('Error', error);
Matija Obreza's avatar
Matija Obreza committed
107
108
      });
  };
109
110
}

Matija Obreza's avatar
Matija Obreza committed
111
112
113
114
115
116
// Create a new descriptor
const createDataset = () => (dispatch) => {
  dispatch({ type: CREATE_DATASET });
  return dispatch(push(`/datasets/edit`));
};

117
const saveDataset = (dataset: Dataset) => (dispatch, getState) => {
Matija Obreza's avatar
Matija Obreza committed
118

Maxym Borodenko's avatar
Maxym Borodenko committed
119
120
121
122
123
124
    const needToRedirect: boolean = !(dataset.version && dataset.uuid);

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

Matija Obreza's avatar
Matija Obreza committed
125
126
127
128
129
130
131
132
    // remove normalized data here
    const data = new Dataset({
      ...dataset,
      repositoryFiles: [],
      creators: [],
      locations: [],
      descriptors: [],
    });
133

134
135
136
    const token = getState().login.access_token;

    return DatasetService.saveDataset(token, data)
Matija Obreza's avatar
Matija Obreza committed
137
138
      .then((saved) => {
        dispatch(receiveDataset(saved));
Maxym Borodenko's avatar
Maxym Borodenko committed
139
140
141
        if (needToRedirect) {
            dispatch(gotoNextStep(saved));
        }
Matija Obreza's avatar
Matija Obreza committed
142
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
143
        log('Save error', error);
Matija Obreza's avatar
Matija Obreza committed
144
145
      });
};
146

Maxym Borodenko's avatar
Maxym Borodenko committed
147
148
149
150
151
152
153
154
155
function gotoNextStep(dataset: Dataset) {
    return (dispatch, getState) => {
        const link = window.location.pathname.split('/').pop();
        const stepId = steps.find((e) => e.link.endsWith(link)).id;
        const path = steps.find((e) => e.id === (stepId + 1)).link;
        dispatch(navigateTo(`/datasets/${dataset.uuid}/${path}`));
    };
}

156
function publishDataset(dataset: Dataset, published: boolean = true) {
157
158
  return (dispatch, getState) => {
    const token = getState().login.access_token;
159

160
    return DatasetService.publishDataset(token, dataset, published)
Matija Obreza's avatar
Matija Obreza committed
161
162
163
164
      .then((saved) => {
        dispatch(receiveDataset(saved));
        dispatch(showDataset(saved.uuid));
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
165
        log('Publish error', error);
Matija Obreza's avatar
Matija Obreza committed
166
167
      });
  };
168
169
}

170
171
172
173
174
175
176
177
178
function deleteDataset(dataset: Dataset) {
  return (dispatch, getState) => {
    const token = getState().login.access_token;

    return DatasetService.deleteDataset(token, dataset)
      .then((saved) => {
        dispatch(receiveDataset(saved));
        // dispatch(showDataset(saved.uuid));
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
179
        log('Delete error', error);
180
181
182
183
      });
  };
}

184
function updateDatasetAccessionIdentifiers(dataset: Dataset, accessionIdentifiers: AccessionIdentifier[]) {
185
186
  return (dispatch, getState) => {
    const token = getState().login.access_token;
187

188
    return DatasetService.updateAccessionIdentifiers(token, dataset, accessionIdentifiers)
189
      .then((saved) => {
Matija Obreza's avatar
Matija Obreza committed
190
        dispatch(receiveDataset(saved));
191
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
192
        log('Publish error', error);
193
194
      });
  };
195
196
}

197
function addDescriptorsToDatasetRequest(dataset: Dataset, descriptor: Descriptor) {
198

199
200
  return (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
201

202
    return DatasetService.addDescriptorsToDataset(token, dataset, [descriptor.uuid])
Matija Obreza's avatar
Matija Obreza committed
203
204
205
      .then((saved) => {
        dispatch(receiveDataset(saved));
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
206
        log('Add error', error);
Matija Obreza's avatar
Matija Obreza committed
207
208
209
210
      });
  };
}

211
212
213
214
215
216
217
218
219
export const setDescriptorsToDatasetRequest = (dataset: Dataset, descriptorUuids: string[]) => (dispatch, getState) => {
  return DatasetService.setDescriptorsToDataset(getState().login.access_token, dataset, descriptorUuids)
    .then((saved) => {
      dispatch(receiveDataset(saved));
    }).catch((error) => {
      log('Add error', error);
    });
};

Matija Obreza's avatar
Matija Obreza committed
220
function removeDescriptorsFromDatasetRequest(dataset: Dataset, descriptor: Descriptor) {
221
222
  return (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
223

224
    return DatasetService.removeDescriptorsFromDataset(token, dataset, [descriptor.uuid])
Matija Obreza's avatar
Matija Obreza committed
225
226
227
      .then((saved) => {
        dispatch(receiveDataset(saved));
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
228
        log('Delete error', error);
Matija Obreza's avatar
Matija Obreza committed
229
230
231
232
233
      });
  };
}

export {
234
  listMyDatasets, listDatasetsRequest, loadDataset, createDataset, saveDataset, deleteDataset,
Matija Obreza's avatar
Matija Obreza committed
235
  publishDataset, receiveDataset, editDataset, showDataset, updateDatasetAccessionIdentifiers,
Matija Obreza's avatar
Matija Obreza committed
236
  addDescriptorsToDatasetRequest, removeDescriptorsFromDatasetRequest,
Matija Obreza's avatar
Matija Obreza committed
237
238
};

Valeriy Panov's avatar
Valeriy Panov committed
239
240
export const uploadRepositoryFileRequest = (datasetUUID: string, file: File) => (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
241

Valeriy Panov's avatar
Valeriy Panov committed
242
243
244
245
    return RepositoryFileService.uploadRepositoryFile(token, datasetUUID, file)
        .then((dataset) => {
            dispatch(receiveDataset(dataset));
        }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
246
            log('Save error', error);
Valeriy Panov's avatar
Valeriy Panov committed
247
        });
Matija Obreza's avatar
Matija Obreza committed
248
249
};

Valeriy Panov's avatar
Valeriy Panov committed
250
251
252
253
254
255
256
export const updateRepositoryFileRequest = (datasetUUID: string, repositoryfile: RepositoryFile) => (dispatch, getState) => {
    const token = getState().login.access_token;

    return RepositoryFileService.updateRepositoryFile(token, datasetUUID, repositoryfile)
        .then((dataset) => {
            dispatch(receiveDataset(dataset));
        }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
257
            log('Save error', error);
Valeriy Panov's avatar
Valeriy Panov committed
258
259
260
261
        });
};

export const deleteRepositoryFileRequest = (datasetUUID: string, uuid: string) => (dispatch, getState) => {
262
  const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
263

Valeriy Panov's avatar
Valeriy Panov committed
264
  return RepositoryFileService.deleteRepositoryFile(token, datasetUUID, uuid)
Matija Obreza's avatar
Matija Obreza committed
265
266
267
    .then((dataset) => {
      dispatch(receiveDataset(dataset));
    }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
268
      log('Delete error', error);
Matija Obreza's avatar
Matija Obreza committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    });
};

function addCreatorToDataset(creator: Creator, uuid: string) {
  return {
    type: ADD_CREATOR_TO_DATASET,
    payload: {
      creator,
      datasetUUID: uuid,
    },
  };
}

// FIXME No uuid param required, currentDataset is available in state
function createDatasetCreator(uuid: string) {
  return (dispatch, getState) => {
Valeriy Panov's avatar
Valeriy Panov committed
285
    log('createCreator');
286
    return CreatorService.createCreator(getState().login.access_token, uuid)
Matija Obreza's avatar
Matija Obreza committed
287
288
289
      .then((obj) => {
        dispatch(addCreatorToDataset(obj, uuid));
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
290
        log('Create creator error', error);
Matija Obreza's avatar
Matija Obreza committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
      });
  };
}

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

function updateCreatorRequest(uuid: string, creator: Creator) {
305
306
307
    return (dispatch, getState) => {
        const token = getState().login.access_token;
        return CreatorService.updateCreator(token, uuid, creator)
Matija Obreza's avatar
Matija Obreza committed
308
309
            .then((obj) => {
                dispatch(updateCreator(obj));
310
            }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
311
                log('Update creator error', error);
312
313
314
315
            });
    };
}

Matija Obreza's avatar
Matija Obreza committed
316
317
318
319
320
321
322
323
324
325
326
function removeCreator(creator: Creator, uuid: string) {
    return {
        type: REMOVE_CREATOR_FROM_DATASET,
        payload : {
            creator,
            datasetUUID: uuid,
        },
    };
}

function deleteCreatorRequest(uuid: string, creator: Creator) {
327
328
329
    return (dispatch, getState) => {
        const token = getState().login.access_token;
        return CreatorService.deleteCreator(token, uuid, creator)
Matija Obreza's avatar
Matija Obreza committed
330
331
            .then((obj) => {
                dispatch(removeCreator(obj, uuid));
332
            }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
333
                log('Delete creator error', error);
334
335
336
337
            });
    };
}

Maxym Borodenko's avatar
Maxym Borodenko committed
338
export { createDatasetCreator, deleteCreatorRequest, updateCreatorRequest, listDatasetsByCodeRequest };
Matija Obreza's avatar
Matija Obreza committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

//
// Dataset: Timing and Location

function addLocation(datasetUUID: string, location: Location) {
  return {
    type: ADD_LOCATION,
    payload: {
      datasetUUID,
      location,
    },
  };
}

function createLocationRequest(datasetUUID: string) {
  const location = new Location({});

356
357
  return (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
358

359
    return LocationService.saveLocation(token, datasetUUID, location)
Matija Obreza's avatar
Matija Obreza committed
360
361
362
      .then((saved) => {
        dispatch(addLocation(datasetUUID, saved));
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
363
        log('Save error', error);
Matija Obreza's avatar
Matija Obreza committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
      });
  };
}

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

function updateLocationRequest(datasetUUID: string, location: Location) {

379
380
  return (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
381

382
    return LocationService.saveLocation(token, datasetUUID, location)
Matija Obreza's avatar
Matija Obreza committed
383
384
385
      .then((saved) => {
        dispatch(receiveLocation(saved));
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
386
        log('Save error', error);
Matija Obreza's avatar
Matija Obreza committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
      });
  };
}

function deleteLocation(datasetUUID: string, location: Location) {
  return {
    type: REMOVE_LOCATION,
    payload: {
      datasetUUID,
      location,
    },
  };
}

function deleteLocationRequest(datasetUUID: string, location: Location) {
402
403
  return (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
404

405
    return LocationService.deleteLocation(token, datasetUUID, location)
Matija Obreza's avatar
Matija Obreza committed
406
407
408
      .then((saved) => {
        dispatch(deleteLocation(datasetUUID, saved));
      }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
409
        log('Delete error', error);
Matija Obreza's avatar
Matija Obreza committed
410
411
412
413
414
      });
  };
}

export { createLocationRequest, updateLocationRequest, deleteLocationRequest, receiveLocation };