Merge branch '14103-improve-dialogs'
[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 { Dispatch } from "redux";
8 import { FilterBuilder } from "~/common/api/filter-builder";
9 import { RootState } from "../store";
10 import { checkPresenceInFavorites } from "../favorites/favorites-actions";
11 import { ServiceRepository } from "~/services/services";
12
13 export const projectActions = unionize({
14     REMOVE_PROJECT: ofType<string>(),
15     PROJECTS_REQUEST: ofType<string>(),
16     PROJECTS_SUCCESS: ofType<{ projects: ProjectResource[], 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 = '') => 
26     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
27         dispatch(projectActions.PROJECTS_REQUEST(parentUuid));
28         return services.projectService.list({
29             filters: new FilterBuilder()
30                 .addEqual("ownerUuid", parentUuid)
31                 .getFilters()
32         }).then(({ items: projects }) => {
33             dispatch(projectActions.PROJECTS_SUCCESS({ projects, parentItemId: parentUuid }));
34             dispatch<any>(checkPresenceInFavorites(projects.map(project => project.uuid)));
35             return projects;
36         });
37     };
38
39 export type ProjectAction = UnionOf<typeof projectActions>;