1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
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";
26 export interface CollectionCreateFormDialogData {
30 storageClassesDesired: string[];
31 properties: CollectionProperties;
34 export interface CollectionProperties {
35 [key: string]: string | string[];
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);
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 }));
50 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
52 dispatch(fileUploaderActions.CLEAR_UPLOAD());
53 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
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;
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));
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));
72 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME));
73 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
74 const errMsg = e.errors
76 : 'There was an error while creating the collection';
77 dispatch(snackbarActions.OPEN_SNACKBAR({
80 kind: SnackbarKind.ERROR
82 if (newCollection) { await services.collectionService.delete(newCollection.uuid); }
86 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));