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 "~/common/api/common-resource-service";
11 import { uploadCollectionFiles } from './collection-upload-actions';
12 import { fileUploaderActions } from '~/store/file-uploader/file-uploader-actions';
14 export interface CollectionCreateFormDialogData {
20 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
22 export const openCollectionCreateDialog = (ownerUuid: string) =>
23 (dispatch: Dispatch) => {
24 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
25 dispatch(fileUploaderActions.CLEAR_UPLOAD());
26 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
29 export const createCollection = (data: CollectionCreateFormDialogData) =>
30 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31 dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
33 const newCollection = await services.collectionService.create(data);
34 await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
35 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
36 dispatch(reset(COLLECTION_CREATE_FORM_NAME));
39 const error = getCommonResourceServiceError(e);
40 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
41 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));