Merge branch '14402-creating-a-new-project-or-collection-anywhere-in-the-app-creates...
[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, isProjectOrRunProcessRoute } 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         const router = getState();
27         const properties = getState().properties;
28         if (isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router)) {
29             const userUuid = getState().auth.user!.uuid;
30             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { userUuid }));
31         } else {
32             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
33         }
34         dispatch(fileUploaderActions.CLEAR_UPLOAD());
35         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
36     };
37
38 export const createCollection = (data: CollectionCreateFormDialogData) =>
39     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40         dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
41         try {
42             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
43             const newCollection = await services.collectionService.create(data);
44             await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
45             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
46             dispatch(reset(COLLECTION_CREATE_FORM_NAME));
47             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
48             return newCollection;
49         } catch (e) {
50             const error = getCommonResourceServiceError(e);
51             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
52                 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));
53             }
54             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
55             return;
56         }
57     };