1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { reset, startSubmit, stopSubmit, initialize, FormErrors } from 'redux-form';
7 import { RootState } from '~/store/store';
8 import { dialogActions } from "~/store/dialog/dialog-actions";
9 import { ServiceRepository } from '~/services/services';
10 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
11 import { uploadCollectionFiles } from './collection-upload-actions';
12 import { fileUploaderActions } from '~/store/file-uploader/file-uploader-actions';
13 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
14 import { isItemNotInProject, isProjectOrRunProcessRoute } from '~/store/projects/project-create-actions';
15 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
16 import { CollectionResource } from "~/models/collection";
18 export interface CollectionCreateFormDialogData {
24 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
26 export const openCollectionCreateDialog = (ownerUuid: string) =>
27 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
28 const router = getState();
29 const properties = getState().properties;
30 if (isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router)) {
31 const userUuid = getState().auth.user!.uuid;
32 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { userUuid }));
34 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
36 dispatch(fileUploaderActions.CLEAR_UPLOAD());
37 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
40 export const createCollection = (data: CollectionCreateFormDialogData) =>
41 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42 dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
43 let newCollection: CollectionResource;
45 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
46 newCollection = await services.collectionService.create(data);
47 await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
48 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
49 dispatch(reset(COLLECTION_CREATE_FORM_NAME));
50 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
53 const error = getCommonResourceServiceError(e);
54 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
55 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
56 } else if (error === CommonResourceServiceError.NONE) {
57 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME));
58 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
59 dispatch(snackbarActions.OPEN_SNACKBAR({
60 message: 'Collection has not been created.',
62 kind: SnackbarKind.ERROR
64 await services.collectionService.delete(newCollection!.uuid);
66 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));