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