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 } 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 {
22 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateDialog";
24 export const openCreateModal = () =>
25 (dispatch: Dispatch) => {
26 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: {} }));
29 export const addCollection = (data: CollectionCreateFormDialogData) =>
30 async (dispatch: Dispatch) => {
31 await dispatch<any>(createCollection(data));
32 dispatch(snackbarActions.OPEN_SNACKBAR({
33 message: "Collection has been successfully created.",
38 export const createCollection = (collection: Partial<CollectionResource>) =>
39 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40 dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
42 const newCollection = await services.collectionService.create(collection);
43 await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
44 dispatch(projectPanelActions.REQUEST_ITEMS());
45 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
46 dispatch(reset(COLLECTION_CREATE_FORM_NAME));
47 // return newCollection;
49 const error = getCommonResourceServiceError(e);
50 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
51 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));