Merge branch '14102-actions-repository'
[arvados-workbench2.git] / src / store / projects / project-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 } from 'redux-form';
7 import { RootState } from '~/store/store';
8 import { dialogActions } from "~/store/dialog/dialog-actions";
9 import { getCommonResourceServiceError, CommonResourceServiceError } from '~/common/api/common-resource-service';
10 import { ProjectResource } from '~/models/project';
11 import { ServiceRepository } from '~/services/services';
12
13 export interface ProjectCreateFormDialogData {
14     ownerUuid: string;
15     name: string;
16     description: string;
17 }
18
19 export const PROJECT_CREATE_FORM_NAME = 'projectCreateFormName';
20
21 export const openProjectCreateDialog = (ownerUuid: string) =>
22     (dispatch: Dispatch) => {
23         dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
24         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_CREATE_FORM_NAME, data: {} }));
25     };
26
27 export const createProject = (project: Partial<ProjectResource>) =>
28     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29         dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
30         try {
31             const newProject = await services.projectService.create(project);
32             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
33             dispatch(reset(PROJECT_CREATE_FORM_NAME));
34             return newProject;
35         } catch (e) {
36             const error = getCommonResourceServiceError(e);
37             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
38                 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Project with the same name already exists.' }));
39             }
40             return undefined;
41         }
42     };