Merge branch '17016-delete-single-file-deletes-whole-collection'
[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 import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
15 import { updateResources } from "../resources/resources-actions";
16 import { reloadProjectMatchingUuid } from "../workbench/workbench-actions";
17 import { loadDetailsPanel } from "../details-panel/details-panel-action";
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: CollectionUpdateFormDialogData) =>
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: CollectionUpdateFormDialogData) =>
34     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35         const uuid = collection.uuid || '';
36         dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
37         dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME));
38
39         services.collectionService.update(uuid, {
40             name: collection.name,
41             description: collection.description }
42         ).then(updatedCollection => {
43             dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
44             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
45             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
46             dispatch(snackbarActions.OPEN_SNACKBAR({
47                 message: "Collection has been successfully updated.",
48                 hideDuration: 2000,
49                 kind: SnackbarKind.SUCCESS
50             }));
51             dispatch<any>(updateResources([updatedCollection]));
52             dispatch<any>(reloadProjectMatchingUuid([updatedCollection.ownerUuid]));
53             dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
54         }).catch (e => {
55             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
56             const error = getCommonResourceServiceError(e);
57             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
58                 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
59             } else {
60                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
61                 dispatch(snackbarActions.OPEN_SNACKBAR({
62                     message: e.errors.join(''),
63                     hideDuration: 2000,
64                     kind: SnackbarKind.ERROR }));
65                 }
66             }
67         );
68     };