store - remove collection creator, modify modla for adding collection
[arvados-workbench2.git] / src / store / collections / collection-update-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 { initialize, startSubmit, stopSubmit } from 'redux-form';
7 import { RootState } from "~/store/store";
8 import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
9 import { updateDetails } from "~/store/details-panel/details-panel-action";
10 import { dialogActions } from "~/store/dialog/dialog-actions";
11 import { dataExplorerActions } from "~/store/data-explorer/data-explorer-action";
12 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
13 import { ContextMenuResource } from '~/store/context-menu/context-menu-reducer';
14 import { PROJECT_PANEL_ID } from "~/views/project-panel/project-panel";
15 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/common/api/common-resource-service";
16 import { ServiceRepository } from "~/services/services";
17 import { CollectionResource } from '~/models/collection';
18
19 export interface CollectionUpdateFormDialogData {
20     uuid: string;
21     name: string;
22     description: string;
23 }
24
25 export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateDialog';
26
27 export const openUpdateModal = (resource: ContextMenuResource) =>
28     (dispatch: Dispatch) => {
29         dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource));
30         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} }));
31     };
32
33 export const editCollection = (data: CollectionUpdateFormDialogData) =>
34     async (dispatch: Dispatch) => {
35         await dispatch<any>(updateCollection(data));
36         dispatch(snackbarActions.OPEN_SNACKBAR({
37             message: "Collection has been successfully updated.",
38             hideDuration: 2000
39         }));
40     };
41
42 export const updateCollection = (collection: Partial<CollectionResource>) =>
43     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44         const uuid = collection.uuid || '';
45         dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
46         try {
47             const updatedCollection = await services.collectionService.update(uuid, collection);
48             dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
49             dispatch<any>(updateDetails(updatedCollection));
50             dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
51             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
52         } catch(e) {
53             const error = getCommonResourceServiceError(e);
54             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
55                 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));
56             }
57         }
58     };