c42b4ef40cdaf4407ec4a1bd0ef541f133c9257c
[arvados-workbench2.git] / src / store / collections / collection-create-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { reset, startSubmit, stopSubmit, initialize } from 'redux-form';
7 import { RootState } from '~/store/store';
8 import { dialogActions } from "~/store/dialog/dialog-actions";
9 import { ServiceRepository } from '~/services/services';
10 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
11 import { uploadCollectionFiles } from './collection-upload-actions';
12 import { fileUploaderActions } from '~/store/file-uploader/file-uploader-actions';
13 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
14 import { isItemNotInProject, isProjectRoute } from '~/store/projects/project-create-actions';
15
16 export interface CollectionCreateFormDialogData {
17     ownerUuid: string;
18     name: string;
19     description: string;
20 }
21
22 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
23
24 export const openCollectionCreateDialog = (ownerUuid: string) =>
25     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
26         if (isItemNotInProject || !isProjectRoute) {
27             const userUuid = getState().auth.user!.uuid;
28             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { userUuid }));
29         } else {
30             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
31         }
32         dispatch(fileUploaderActions.CLEAR_UPLOAD());
33         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
34     };
35
36 export const createCollection = (data: CollectionCreateFormDialogData) =>
37     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38         dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
39         try {
40             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
41             const newCollection = await services.collectionService.create(data);
42             await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
43             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
44             dispatch(reset(COLLECTION_CREATE_FORM_NAME));
45             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
46             return newCollection;
47         } catch (e) {
48             const error = getCommonResourceServiceError(e);
49             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
50                 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));
51             }
52             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
53             return;
54         }
55     };