18219: Adds property edition capabilities to create & update dialogs.
[arvados-workbench2.git] / src / store / collections / collection-create-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 { reset, startSubmit, stopSubmit, initialize, FormErrors, change, formValueSelector } 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";
18 import { ResourcePropertiesFormData } from "views-components/resource-properties-form/resource-properties-form";
19 import { addProperty, deleteProperty } from "lib/resource-properties";
20
21 export interface CollectionCreateFormDialogData {
22     ownerUuid: string;
23     name: string;
24     description: string;
25     storageClassesDesired: string[];
26     properties: CollectionProperties;
27 }
28
29 export interface CollectionProperties {
30     [key: string]: string | string[];
31 }
32
33 export const COLLECTION_CREATE_FORM_NAME = "collectionCreateFormName";
34 export const COLLECTION_CREATE_PROPERTIES_FORM_NAME = "collectionCreatePropertiesFormName";
35 export const COLLECTION_CREATE_FORM_SELECTOR = formValueSelector(COLLECTION_CREATE_FORM_NAME);
36
37 export const openCollectionCreateDialog = (ownerUuid: string) =>
38     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39         const { router } = getState();
40         if (!isProjectOrRunProcessRoute(router)) {
41             const userUuid = getUserUuid(getState());
42             if (!userUuid) { return; }
43             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid: userUuid }));
44         } else {
45             dispatch(initialize(COLLECTION_CREATE_FORM_NAME, { ownerUuid }));
46         }
47         dispatch(fileUploaderActions.CLEAR_UPLOAD());
48         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_CREATE_FORM_NAME, data: { ownerUuid } }));
49     };
50
51 export const createCollection = (data: CollectionCreateFormDialogData) =>
52     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
53         dispatch(startSubmit(COLLECTION_CREATE_FORM_NAME));
54         let newCollection: CollectionResource | undefined;
55         try {
56             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_CREATE_FORM_NAME));
57             newCollection = await services.collectionService.create(data);
58             await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
59             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
60             dispatch(reset(COLLECTION_CREATE_FORM_NAME));
61             return newCollection;
62         } catch (e) {
63             const error = getCommonResourceServiceError(e);
64             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
65                 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
66             } else if (error === CommonResourceServiceError.NONE) {
67                 dispatch(stopSubmit(COLLECTION_CREATE_FORM_NAME));
68                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_CREATE_FORM_NAME }));
69                 dispatch(snackbarActions.OPEN_SNACKBAR({
70                     message: 'Collection has not been created.',
71                     hideDuration: 2000,
72                     kind: SnackbarKind.ERROR
73                 }));
74                 if (newCollection) { await services.collectionService.delete(newCollection.uuid); }
75             }
76             return;
77         } finally {
78             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_CREATE_FORM_NAME));
79         }
80     };
81
82 export const addPropertyToCreateCollectionForm = (data: ResourcePropertiesFormData) =>
83     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
84         const properties = { ...COLLECTION_CREATE_FORM_SELECTOR(getState(), 'properties') };
85         const key = data.keyID || data.key;
86         const value =  data.valueID || data.value;
87         dispatch(change(
88             COLLECTION_CREATE_FORM_NAME,
89             'properties',
90             addProperty(properties, key, value)));
91     };
92
93 export const removePropertyFromCreateCollectionForm = (key: string, value: string) =>
94     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
95         const properties = { ...COLLECTION_CREATE_FORM_SELECTOR(getState(), 'properties') };
96         dispatch(change(
97             COLLECTION_CREATE_FORM_NAME,
98             'properties',
99             deleteProperty(properties, key, value)));
100     };