Change move-to feature - files path, rename variables and files
[arvados-workbench2.git] / src / store / collections / collection-create-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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";
15
16 export interface CollectionCreateFormDialogData {
17     ownerUuid: string;
18     name: string;
19     description: string;
20     files: File[];
21 }
22
23 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
24
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 } }));
29     };
30
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.",
36             hideDuration: 2000
37         }));
38     };
39
40 export const createCollection = (collection: Partial<CollectionResource>) =>
41     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42         dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
43         try {
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));
49         } catch (e) {
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.' }));
53             }
54         }
55     };