Merge branch '13747-incorrect-items-in-project-tree'
[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 { Project, ProjectResource } from "../../models/project";
7 import { projectService } from "../../services/services";
8 import { Dispatch } from "redux";
9 import { getResourceKind } from "../../models/resource";
10 import FilterBuilder from "../../common/api/filter-builder";
11
12 const actions = unionize({
13     CREATE_PROJECT: ofType<Project>(),
14     REMOVE_PROJECT: ofType<string>(),
15     PROJECTS_REQUEST: ofType<string>(),
16     PROJECTS_SUCCESS: ofType<{ projects: Project[], parentItemId?: string }>(),
17     TOGGLE_PROJECT_TREE_ITEM_OPEN: ofType<string>(),
18     TOGGLE_PROJECT_TREE_ITEM_ACTIVE: ofType<string>(),
19     RESET_PROJECT_TREE_ACTIVITY: ofType<string>()
20 }, {
21         tag: 'type',
22         value: 'payload'
23     });
24
25 export const getProjectList = (parentUuid: string = '') => (dispatch: Dispatch) => {
26     dispatch(actions.PROJECTS_REQUEST(parentUuid));
27     return projectService.list({
28         filters: FilterBuilder
29             .create<ProjectResource>()
30             .addEqual("ownerUuid", parentUuid)
31     }).then(listResults => {
32         const projects = listResults.items.map(item => ({
33             ...item,
34             kind: getResourceKind(item.kind)
35         }));
36         dispatch(actions.PROJECTS_SUCCESS({ projects, parentItemId: parentUuid }));
37         return projects;
38     });
39 };
40
41 export type ProjectAction = UnionOf<typeof actions>;
42 export default actions;