1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 { loadDetailsPanel } from "../details-panel/details-panel-action";
18 export interface CollectionUpdateFormDialogData {
24 export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateFormName';
26 export const openCollectionUpdateDialog = (resource: CollectionUpdateFormDialogData) =>
27 (dispatch: Dispatch) => {
28 dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource));
29 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} }));
32 export const updateCollection = (collection: CollectionUpdateFormDialogData) =>
33 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34 const uuid = collection.uuid || '';
35 dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
36 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME));
38 services.collectionService.update(uuid, {
39 name: collection.name,
40 description: collection.description }
41 ).then(updatedCollection => {
42 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
43 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
44 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
45 dispatch(snackbarActions.OPEN_SNACKBAR({
46 message: "Collection has been successfully updated.",
48 kind: SnackbarKind.SUCCESS
50 dispatch<any>(updateResources([updatedCollection]));
51 dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
53 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
54 const error = getCommonResourceServiceError(e);
55 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
56 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
58 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
59 dispatch(snackbarActions.OPEN_SNACKBAR({
60 message: e.errors.join(''),
62 kind: SnackbarKind.ERROR }));