16848: Improved event handler installation/removal.
[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, FormErrors } from 'redux-form';
7 import { RootState } from '~/store/store';
8 import { getUserUuid } from "~/common/getuser";
9 import { dialogActions } from "~/store/dialog/dialog-actions";
10 import { ServiceRepository } from '~/services/services';
11 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
12 import { uploadCollectionFiles } from './collection-upload-actions';
13 import { fileUploaderActions } from '~/store/file-uploader/file-uploader-actions';
14 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
15 import { isProjectOrRunProcessRoute } from '~/store/projects/project-create-actions';
16 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
17 import { CollectionResource } from "~/models/collection";
18
19 export interface CollectionCreateFormDialogData {
20     ownerUuid: string;
21     name: string;
22     description: string;
23 }
24
25 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
26
27 export const openCollectionCreateDialog = (ownerUuid: string) =>
28     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29         const { router } = getState();
30         if (!isProjectOrRunProcessRoute(router)) {
31             const userUuid = getUserUuid(getState());
32             if (!userUuid) { return; }
33             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid: userUuid }));
34         } else {
35             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
36         }
37         dispatch(fileUploaderActions.CLEAR_UPLOAD());
38         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
39     };
40
41 export const createCollection = (data: CollectionCreateFormDialogData) =>
42     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43         dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
44         let newCollection: CollectionResource | undefined;
45         try {
46             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
47             newCollection = await services.collectionService.create(data);
48             await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
49             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
50             dispatch(reset(COLLECTION_CREATE_FORM_NAME));
51             return newCollection;
52         } catch (e) {
53             const error = getCommonResourceServiceError(e);
54             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
55                 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
56             } else if (error === CommonResourceServiceError.NONE) {
57                 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME));
58                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
59                 dispatch(snackbarActions.OPEN_SNACKBAR({
60                     message: 'Collection has not been created.',
61                     hideDuration: 2000,
62                     kind: SnackbarKind.ERROR
63                 }));
64                 if (newCollection) { await services.collectionService.delete(newCollection.uuid); }
65             }
66             return;
67         } finally {
68             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
69         }
70     };