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 { uploadCollectionFiles } from '~/store/collections/uploader/collection-uploader-actions';
9 import { projectPanelActions } from "~/store/project-panel/project-panel-action";
10 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
11 import { dialogActions } from "~/store/dialog/dialog-actions";
12 import { CollectionResource } from '~/models/collection';
13 import { ServiceRepository } from '~/services/services';
14 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/common/api/common-resource-service";
16 export interface CollectionCreateFormDialogData {
23 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
25 export const openCollectionCreateDialog = (ownerUuid: string) =>
26 (dispatch: Dispatch) => {
27 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
28 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
31 export const addCollection = (data: CollectionCreateFormDialogData) =>
32 async (dispatch: Dispatch) => {
33 await dispatch<any>(createCollection(data));
34 dispatch(snackbarActions.OPEN_SNACKBAR({
35 message: "Collection has been successfully created.",
40 export const createCollection = (collection: Partial<CollectionResource>) =>
41 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42 dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
44 const newCollection = await services.collectionService.create(collection);
45 await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
46 dispatch(projectPanelActions.REQUEST_ITEMS());
47 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
48 dispatch(reset(COLLECTION_CREATE_FORM_NAME));
50 const error = getCommonResourceServiceError(e);
51 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
52 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));