Merge branch '20845-queued-process-filter' refs #20845
[arvados.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 {
7     reset,
8     startSubmit,
9     stopSubmit,
10     initialize,
11     FormErrors,
12     formValueSelector
13 } from 'redux-form';
14 import { RootState } from 'store/store';
15 import { getUserUuid } from "common/getuser";
16 import { dialogActions } from "store/dialog/dialog-actions";
17 import { ServiceRepository } from 'services/services';
18 import { getCommonResourceServiceError, CommonResourceServiceError } from "services/common-service/common-resource-service";
19 import { uploadCollectionFiles } from './collection-upload-actions';
20 import { fileUploaderActions } from 'store/file-uploader/file-uploader-actions';
21 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
22 import { isProjectOrRunProcessRoute } from 'store/projects/project-create-actions';
23 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
24 import { CollectionResource } from "models/collection";
25
26 export interface CollectionCreateFormDialogData {
27     ownerUuid: string;
28     name: string;
29     description: string;
30     storageClassesDesired: string[];
31     properties: CollectionProperties;
32 }
33
34 export interface CollectionProperties {
35     [key: string]: string | string[];
36 }
37
38 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
39 export const COLLECTION_CREATE_PROPERTIES_FORM_NAME = "collectionCreatePropertiesFormName";
40 export const COLLECTION_CREATE_FORM_SELECTOR = formValueSelector(COLLECTION_CREATE_FORM_NAME);
41
42 export const openCollectionCreateDialog = (ownerUuid: string) =>
43     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44         const { router } = getState();
45         if (!isProjectOrRunProcessRoute(router)) {
46             const userUuid = getUserUuid(getState());
47             if (!userUuid) { return; }
48             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid: userUuid }));
49         } else {
50             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
51         }
52         dispatch(fileUploaderActions.CLEAR_UPLOAD());
53         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
54     };
55
56 export const createCollection = (data: CollectionCreateFormDialogData) =>
57     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
58         dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
59         let newCollection: CollectionResource | undefined;
60         try {
61             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
62             newCollection = await services.collectionService.create(data, false);
63             await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
64             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
65             dispatch(reset(COLLECTION_CREATE_FORM_NAME));
66             return newCollection;
67         } catch (e) {
68             const error = getCommonResourceServiceError(e);
69             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
70                 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
71             } else {
72                 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME));
73                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
74                 const errMsg = e.errors
75                     ? e.errors.join('')
76                     : 'There was an error while creating the collection';
77                 dispatch(snackbarActions.OPEN_SNACKBAR({
78                     message: errMsg,
79                     hideDuration: 2000,
80                     kind: SnackbarKind.ERROR
81                 }));
82                 if (newCollection) { await services.collectionService.delete(newCollection.uuid); }
83             }
84             return;
85         } finally {
86             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
87         }
88     };