1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 { isNotProjectItem } from '~/store/projects/project-create-actions';
16 export interface CollectionCreateFormDialogData {
22 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
24 export const openCollectionCreateDialog = (ownerUuid: string) =>
25 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
26 if (isNotProjectItem) {
27 const userUuid = getState().auth.user!.uuid;
28 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { userUuid }));
30 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
32 dispatch(fileUploaderActions.CLEAR_UPLOAD());
33 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
36 export const createCollection = (data: CollectionCreateFormDialogData) =>
37 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38 dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
40 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
41 const newCollection = await services.collectionService.create(data);
42 await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
43 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
44 dispatch(reset(COLLECTION_CREATE_FORM_NAME));
45 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
48 const error = getCommonResourceServiceError(e);
49 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
50 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));
52 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));