5b176beac5bd6d2c67bcbca2204e70b307e1c369
[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 { FormErrors, initialize, startSubmit, stopSubmit } from 'redux-form';
7 import { RootState } from "~/store/store";
8 import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
9 import { dialogActions } from "~/store/dialog/dialog-actions";
10 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
11 import { ServiceRepository } from "~/services/services";
12 import { CollectionResource } from '~/models/collection';
13 import { ContextMenuResource } from "~/store/context-menu/context-menu-actions";
14 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
15
16 export interface CollectionUpdateFormDialogData {
17     uuid: string;
18     name: string;
19     description: string;
20 }
21
22 export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateFormName';
23
24 export const openCollectionUpdateDialog = (resource: ContextMenuResource) =>
25     (dispatch: Dispatch) => {
26         dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource));
27         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} }));
28     };
29
30 export const updateCollection = (collection: Partial<CollectionResource>) =>
31     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
32         const uuid = collection.uuid || '';
33         dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
34         dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME));
35         try {
36             const updatedCollection = await services.collectionService.update(uuid, collection);
37             dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
38             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
39             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
40             return updatedCollection;
41         } catch (e) {
42             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
43             const error = getCommonResourceServiceError(e);
44             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
45                 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
46             } else {
47                 // Unknown error, handling left to caller.
48                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
49                 throw(e);
50             }
51         }
52         return;
53     };