dataset.ts 9.88 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
6
import { Creator } from 'model/creator.model';
import { Descriptor } from 'model/descriptors.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
13
import { RepositoryFileService } from 'service/RepositoryFileService';

Matija Obreza's avatar
Matija Obreza committed
14
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';
Matija Obreza's avatar
Matija Obreza committed
15
16
17
18
19

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

Matija Obreza's avatar
Matija Obreza committed
20
21
22
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
23
});
24
25

function showDataset(uuid: string) {
Matija Obreza's avatar
Matija Obreza committed
26
27
28
  return (dispatch) => {
    dispatch(push(`/datasets/${uuid}`));
  };
29
30
31
}

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

37
function listMyDatasets(page?, results?, sortBy?, filter?, order?) {
Matija Obreza's avatar
Matija Obreza committed
38
  return (dispatch, getState) => {
39
    const token = getState().login.access_token;
40

41
    return DatasetService.listMyDatasets(token, page, results, sortBy, filter, order)
Matija Obreza's avatar
Matija Obreza committed
42
      .then((paged) => {
Matija Obreza's avatar
Matija Obreza committed
43
        dispatch(receiveDatasetPage(paged, page, results, sortBy, filter, order));
Matija Obreza's avatar
Matija Obreza committed
44
45
46
47
48
      })
      .catch((error) => {
        console.log('Error', error);
      });
  };
49
50
}

Valeriy Panov's avatar
Valeriy Panov committed
51
function listDatasetsRequest(page?, results?, sortBy?, filter?, order?) {
52
53
  return (dispatch, getState) => {
    const token = getState().login.access_token;
54

55
    return DatasetService.listDatasets(token, page, results, sortBy, filter, order)
Matija Obreza's avatar
Matija Obreza committed
56
      .then((paged) => {
Matija Obreza's avatar
Matija Obreza committed
57
        return dispatch(receiveDatasetPage(paged, page, results, sortBy, filter, order));
Matija Obreza's avatar
Matija Obreza committed
58
59
60
61
62
      })
      .catch((error) => {
        console.log('Error', error);
      });
  };
63
64
}

Matija Obreza's avatar
Matija Obreza committed
65
function loadDataset(uuid: string) {
66
67
  return (dispatch, getState) => {
    const token = getState().login.access_token;
68

69
    return DatasetService.getDataset(token, uuid)
Matija Obreza's avatar
Matija Obreza committed
70
71
72
73
74
75
76
      .then((dataset) => {
        dispatch(receiveDataset(dataset));
      })
      .catch((error) => {
        console.log('Error', error);
      });
  };
77
78
}

Matija Obreza's avatar
Matija Obreza committed
79
80
81
82
83
84
// Create a new descriptor
const createDataset = () => (dispatch) => {
  dispatch({ type: CREATE_DATASET });
  return dispatch(push(`/datasets/edit`));
};

85
const saveDataset = (dataset: Dataset) => (dispatch, getState) => {
Matija Obreza's avatar
Matija Obreza committed
86
87
88
89
90
91
92
93
94

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

96
97
98
    const token = getState().login.access_token;

    return DatasetService.saveDataset(token, data)
Matija Obreza's avatar
Matija Obreza committed
99
100
101
102
103
104
      .then((saved) => {
        dispatch(receiveDataset(saved));
      }).catch((error) => {
        console.log('Save error', error);
      });
};
105

106
function publishDataset(dataset: Dataset, published: boolean = true) {
107
108
109
  return (dispatch, getState) => {

    const token = getState().login.access_token;
110

111
    return DatasetService.publishDataset(token, dataset, published)
Matija Obreza's avatar
Matija Obreza committed
112
113
114
115
116
117
118
      .then((saved) => {
        dispatch(receiveDataset(saved));
        dispatch(showDataset(saved.uuid));
      }).catch((error) => {
        console.log('Publish error', error);
      });
  };
119
120
}

121
function updateDatasetAccessionIdentifiers(dataset: Dataset, accessionIdentifiers: AccessionIdentifier[]) {
122
123
  return (dispatch, getState) => {
    const token = getState().login.access_token;
124

125
    return DatasetService.updateAccessionIdentifiers(token, dataset, accessionIdentifiers)
126
      .then((saved) => {
Matija Obreza's avatar
Matija Obreza committed
127
        dispatch(receiveDataset(saved));
128
129
130
131
      }).catch((error) => {
        console.log('Publish error', error);
      });
  };
132
133
}

134
function addDescriptorsToDatasetRequest(dataset: Dataset, descriptor: Descriptor) {
135

136
137
  return (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
138

139
    return DatasetService.addDescriptorsToDataset(token, dataset, [descriptor.uuid])
Matija Obreza's avatar
Matija Obreza committed
140
141
142
143
144
145
146
147
148
      .then((saved) => {
        dispatch(receiveDataset(saved));
      }).catch((error) => {
        console.log('Add error', error);
      });
  };
}

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

152
    return DatasetService.removeDescriptorsFromDataset(token, dataset, [descriptor.uuid])
Matija Obreza's avatar
Matija Obreza committed
153
154
155
156
157
158
159
160
161
162
163
      .then((saved) => {
        dispatch(receiveDataset(saved));
      }).catch((error) => {
        console.log('Delete error', error);
      });
  };
}

export {
  listMyDatasets, listDatasetsRequest, loadDataset, createDataset, saveDataset,
  publishDataset, receiveDataset, editDataset, showDataset, updateDatasetAccessionIdentifiers,
Matija Obreza's avatar
Matija Obreza committed
164
  addDescriptorsToDatasetRequest, removeDescriptorsFromDatasetRequest,
Matija Obreza's avatar
Matija Obreza committed
165
166
};

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

Valeriy Panov's avatar
Valeriy Panov committed
170
171
172
173
174
175
    return RepositoryFileService.uploadRepositoryFile(token, datasetUUID, file)
        .then((dataset) => {
            dispatch(receiveDataset(dataset));
        }).catch((error) => {
            console.log('Save error', error);
        });
Matija Obreza's avatar
Matija Obreza committed
176
177
};

Valeriy Panov's avatar
Valeriy Panov committed
178
179
180
181
182
183
184
185
186
187
188
189
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) => {
            console.log('Save error', error);
        });
};

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

Valeriy Panov's avatar
Valeriy Panov committed
192
  return RepositoryFileService.deleteRepositoryFile(token, datasetUUID, uuid)
Matija Obreza's avatar
Matija Obreza committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    .then((dataset) => {
      dispatch(receiveDataset(dataset));
    }).catch((error) => {
      console.log('Delete error', error);
    });
};

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) => {
    console.log('createCreator');
214
    return CreatorService.createCreator(getState().login.access_token, uuid)
Matija Obreza's avatar
Matija Obreza committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
      .then((obj) => {
        dispatch(addCreatorToDataset(obj, uuid));
      }).catch((error) => {
        console.log('Create creator error', error);
      });
  };
}

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

function updateCreatorRequest(uuid: string, creator: Creator) {
233
234
235
    return (dispatch, getState) => {
        const token = getState().login.access_token;
        return CreatorService.updateCreator(token, uuid, creator)
Matija Obreza's avatar
Matija Obreza committed
236
237
            .then((obj) => {
                dispatch(updateCreator(obj));
238
            }).catch((error) => {
Matija Obreza's avatar
Matija Obreza committed
239
                console.log('Update creator error', error);
240
241
242
243
            });
    };
}

Matija Obreza's avatar
Matija Obreza committed
244
245
246
247
248
249
250
251
252
253
254
function removeCreator(creator: Creator, uuid: string) {
    return {
        type: REMOVE_CREATOR_FROM_DATASET,
        payload : {
            creator,
            datasetUUID: uuid,
        },
    };
}

function deleteCreatorRequest(uuid: string, creator: Creator) {
255
256
257
    return (dispatch, getState) => {
        const token = getState().login.access_token;
        return CreatorService.deleteCreator(token, uuid, creator)
Matija Obreza's avatar
Matija Obreza committed
258
259
            .then((obj) => {
                dispatch(removeCreator(obj, uuid));
260
            }).catch((error) => {
Matija Obreza's avatar
Matija Obreza committed
261
                console.log('Delete creator error', error);
262
263
264
265
            });
    };
}

Matija Obreza's avatar
Matija Obreza committed
266
export { createDatasetCreator, deleteCreatorRequest, updateCreatorRequest };
Matija Obreza's avatar
Matija Obreza committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

//
// 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({});

284
285
  return (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
286

287
    return LocationService.saveLocation(token, datasetUUID, location)
Matija Obreza's avatar
Matija Obreza committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
      .then((saved) => {
        dispatch(addLocation(datasetUUID, saved));
      }).catch((error) => {
        console.log('Save error', error);
      });
  };
}

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

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

307
308
  return (dispatch, getState) => {
    const token = getState().login.access_token;
Matija Obreza's avatar
Matija Obreza committed
309

310
    return LocationService.saveLocation(token, datasetUUID, location)
Matija Obreza's avatar
Matija Obreza committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
      .then((saved) => {
        dispatch(receiveLocation(saved));
      }).catch((error) => {
        console.log('Save error', error);
      });
  };
}

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

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

333
    return LocationService.deleteLocation(token, datasetUUID, location)
Matija Obreza's avatar
Matija Obreza committed
334
335
336
337
338
339
340
341
342
      .then((saved) => {
        dispatch(deleteLocation(datasetUUID, saved));
      }).catch((error) => {
        console.log('Delete error', error);
      });
  };
}

export { createLocationRequest, updateLocationRequest, deleteLocationRequest, receiveLocation };