1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 { 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";
15 export interface CollectionUpdateFormDialogData {
21 export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateFormName';
23 export const openCollectionUpdateDialog = (resource: ContextMenuResource) =>
24 (dispatch: Dispatch) => {
25 dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource));
26 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} }));
29 export const updateCollection = (collection: Partial<CollectionResource>) =>
30 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31 const uuid = collection.uuid || '';
32 dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
34 const updatedCollection = await services.collectionService.update(uuid, collection);
35 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
36 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
37 return updatedCollection;
39 const error = getCommonResourceServiceError(e);
40 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
41 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' }));