Clean process-logs-panel code
[arvados.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 { loadDetailsPanel } 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 = 'collectionUpdateFormName';
26
27 export const openCollectionUpdateDialog = (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 updateCollection = (collection: Partial<CollectionResource>) =>
34     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35         const uuid = collection.uuid || '';
36         dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
37         try {
38             const updatedCollection = await services.collectionService.update(uuid, collection);
39             dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
40             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
41             return updatedCollection;
42         } catch (e) {
43             const error = getCommonResourceServiceError(e);
44             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
45                 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));
46             }
47             return;
48         }
49     };