Merge branch 'master'
[arvados-workbench2.git] / src / store / project / project-action.ts
index 35ff445e43d0d09c660c8ea22f68e2d3cc9d6030..4f03ae1c7dbe174713b29fc3dbed21b5582d27e8 100644 (file)
@@ -1,34 +1,38 @@
 // Copyright (C) The Arvados Authors. All rights reserved.
 //
 // SPDX-License-Identifier: AGPL-3.0
-import { default as unionize, ofType, UnionOf } from "unionize";
 
-import { Project } from "../../models/project";
-import { projectService } from "../../services/services";
+import { unionize, ofType, UnionOf } from '~/common/unionize';
+import { ProjectResource } from "~/models/project";
 import { Dispatch } from "redux";
+import { FilterBuilder } from "~/common/api/filter-builder";
+import { RootState } from "../store";
+import { updateFavorites } from "../favorites/favorites-actions";
+import { ServiceRepository } from "~/services/services";
+import { resourcesActions } from '~/store/resources/resources-actions';
 
-const actions = unionize({
-    CREATE_PROJECT: ofType<Project>(),
+export const projectActions = unionize({
     REMOVE_PROJECT: ofType<string>(),
     PROJECTS_REQUEST: ofType<string>(),
-    PROJECTS_SUCCESS: ofType<{ projects: Project[], parentItemId?: string }>(),
+    PROJECTS_SUCCESS: ofType<{ projects: ProjectResource[], parentItemId?: string }>(),
     TOGGLE_PROJECT_TREE_ITEM_OPEN: ofType<string>(),
     TOGGLE_PROJECT_TREE_ITEM_ACTIVE: ofType<string>(),
     RESET_PROJECT_TREE_ACTIVITY: ofType<string>()
-}, {
-        tag: 'type',
-        value: 'payload'
-    });
+});
 
-export const getProjectList = (parentUuid?: string) => (dispatch: Dispatch): Promise<Project[]> => {
-    if (parentUuid) {
-        dispatch(actions.PROJECTS_REQUEST(parentUuid));
-        return projectService.getProjectList(parentUuid).then(projects => {
-            dispatch(actions.PROJECTS_SUCCESS({ projects, parentItemId: parentUuid }));
+export const getProjectList = (parentUuid: string = '') => 
+    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        dispatch(projectActions.PROJECTS_REQUEST(parentUuid));
+        return services.projectService.list({
+            filters: new FilterBuilder()
+                .addEqual("ownerUuid", parentUuid)
+                .getFilters()
+        }).then(({ items: projects }) => {
+            dispatch(projectActions.PROJECTS_SUCCESS({ projects, parentItemId: parentUuid }));
+            dispatch<any>(updateFavorites(projects.map(project => project.uuid)));
+            dispatch<any>(resourcesActions.SET_RESOURCES(projects));
             return projects;
         });
-    } return Promise.resolve([]);
-};
+    };
 
-export type ProjectAction = UnionOf<typeof actions>;
-export default actions;
+export type ProjectAction = UnionOf<typeof projectActions>;