Merge branch 'master'
[arvados-workbench2.git] / src / store / project / project-action.ts
index 2856de66a09a675d122fdd8ea44ed39216d717a0..4f03ae1c7dbe174713b29fc3dbed21b5582d27e8 100644 (file)
@@ -2,19 +2,37 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Project } from "../../models/project";
-import { default as unionize, ofType, UnionOf } from "unionize";
+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<any>(),
-    PROJECTS_SUCCESS: ofType<{ projects: Project[], parentItemId?: string }>(),
-    TOGGLE_PROJECT_TREE_ITEM: ofType<string>()
-}, {
-    tag: 'type',
-    value: 'payload'
+    PROJECTS_REQUEST: ofType<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>()
 });
 
-export type ProjectAction = UnionOf<typeof actions>;
-export default actions;
+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;
+        });
+    };
+
+export type ProjectAction = UnionOf<typeof projectActions>;