Change groupClass condition in filters used for fetching groups
[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 { reset, startSubmit, stopSubmit, initialize, FormErrors } 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 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
16 import { CollectionResource } from "~/models/collection";
17
18 export interface CollectionCreateFormDialogData {
19     ownerUuid: string;
20     name: string;
21     description: string;
22 }
23
24 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
25
26 export const openCollectionCreateDialog = (ownerUuid: string) =>
27     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
28         const router = getState();
29         const properties = getState().properties;
30         if (isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router)) {
31             const userUuid = getState().auth.user!.uuid;
32             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { userUuid }));
33         } else {
34             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
35         }
36         dispatch(fileUploaderActions.CLEAR_UPLOAD());
37         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
38     };
39
40 export const createCollection = (data: CollectionCreateFormDialogData) =>
41     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42         dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
43         let newCollection: CollectionResource;
44         try {
45             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
46             newCollection = await services.collectionService.create(data);
47             await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
48             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
49             dispatch(reset(COLLECTION_CREATE_FORM_NAME));
50             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
51             return newCollection;
52         } catch (e) {
53             const error = getCommonResourceServiceError(e);
54             if (error === CommonResourceServiceError.UNIQUE_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                 await services.collectionService.delete(newCollection!.uuid);
65             }
66             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
67             return;
68         }
69     };