Creation dialog with redux-form validation
[arvados-workbench2.git] / src / store / project / project-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4 import { default as unionize, ofType, UnionOf } from "unionize";
5
6 import { ProjectResource } from "../../models/project";
7 import { projectService } from "../../services/services";
8 import { Dispatch } from "redux";
9 import FilterBuilder from "../../common/api/filter-builder";
10 import { RootState } from "../store";
11
12 const actions = unionize({
13     OPEN_PROJECT_CREATOR: ofType<{ ownerUuid: string }>(),
14     CLOSE_PROJECT_CREATOR: ofType<{}>(),
15     CREATE_PROJECT: ofType<Partial<ProjectResource>>(),
16     CREATE_PROJECT_SUCCESS: ofType<ProjectResource>(),
17     REMOVE_PROJECT: ofType<string>(),
18     PROJECTS_REQUEST: ofType<string>(),
19     PROJECTS_SUCCESS: ofType<{ projects: ProjectResource[], parentItemId?: string }>(),
20     TOGGLE_PROJECT_TREE_ITEM_OPEN: ofType<string>(),
21     TOGGLE_PROJECT_TREE_ITEM_ACTIVE: ofType<string>(),
22     RESET_PROJECT_TREE_ACTIVITY: ofType<string>()
23 }, {
24         tag: 'type',
25         value: 'payload'
26     });
27
28 export const getProjectList = (parentUuid: string = '') => (dispatch: Dispatch) => {
29     dispatch(actions.PROJECTS_REQUEST(parentUuid));
30     return projectService.list({
31         filters: FilterBuilder
32             .create<ProjectResource>()
33             .addEqual("ownerUuid", parentUuid)
34     }).then(({ items: projects }) => {
35         dispatch(actions.PROJECTS_SUCCESS({ projects, parentItemId: parentUuid }));
36         return projects;
37     });
38 };
39
40 export const createProject = (project: Partial<ProjectResource>) =>
41     (dispatch: Dispatch, getState: () => RootState) => {
42         const { ownerUuid } = getState().projects.creator;
43         const projectData = { ownerUuid, ...project };
44         dispatch(actions.CREATE_PROJECT(projectData));
45         return projectService
46             .create(projectData)
47             .then(project => dispatch(actions.CREATE_PROJECT_SUCCESS(project)));
48     };
49
50 export type ProjectAction = UnionOf<typeof actions>;
51 export default actions;