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 { getUserUuid } from "~/common/getuser";
9 import { dialogActions } from "~/store/dialog/dialog-actions";
10 import { ServiceRepository } from '~/services/services';
11 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
12 import { uploadCollectionFiles } from './collection-upload-actions';
13 import { fileUploaderActions } from '~/store/file-uploader/file-uploader-actions';
14 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
15 import { isProjectOrRunProcessRoute } from '~/store/projects/project-create-actions';
16 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
17 import { CollectionResource } from "~/models/collection";
19 export interface CollectionCreateFormDialogData {
25 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
27 export const openCollectionCreateDialog = (ownerUuid: string) =>
28 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29 const { router } = getState();
30 if (!isProjectOrRunProcessRoute(router)) {
31 const userUuid = getUserUuid(getState());
32 if (!userUuid) { return; }
33 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid: userUuid }));
35 dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
37 dispatch(fileUploaderActions.CLEAR_UPLOAD());
38 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
41 export const createCollection = (data: CollectionCreateFormDialogData) =>
42 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43 dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
44 let newCollection: CollectionResource | undefined;
46 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
47 newCollection = await services.collectionService.create(data);
48 await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
49 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
50 dispatch(reset(COLLECTION_CREATE_FORM_NAME));
53 const error = getCommonResourceServiceError(e);
54 if (error === CommonResourceServiceError.UNIQUE_NAME_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 if (newCollection) { await services.collectionService.delete(newCollection.uuid); }
68 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));