Create project panel middleware
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Wed, 4 Jul 2018 10:27:20 +0000 (12:27 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Wed, 4 Jul 2018 10:27:20 +0000 (12:27 +0200)
Feature #13703

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/store/navigation/navigation-action.ts
src/store/store.ts
src/views/project-panel/project-panel-item.ts
src/views/project-panel/project-panel-middleware.ts [new file with mode: 0644]

index daeb26fd2a9ee0d9e0b4ab7f8dd5441e93b77412..ec6e9bb50cf36339f3e8b3e0ba2e14822dbf50af 100644 (file)
@@ -61,17 +61,9 @@ export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
                 : dispatch<any>(getProjectList(itemId));
 
             promise
-                .then(() => dispatch<any>(getCollectionList(itemId)))
                 .then(() => dispatch<any>(() => {
-                    const { projects, collections } = getState();
-                    dispatch(dataExplorerActions.SET_ITEMS({
-                        id: PROJECT_PANEL_ID,
-                        items: projectPanelItems(
-                            projects.items,
-                            treeItem.data.uuid,
-                            collections
-                        )
-                    }));
+                    dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
+                    dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));
                 }));
 
         }
index 68c5d8238c74894857e08f7d34f8e0df90bcfb9b..0c32e6538621dfdc0106173e025fd19bff700675 100644 (file)
@@ -12,6 +12,7 @@ import sidePanelReducer, { SidePanelState } from './side-panel/side-panel-reduce
 import authReducer, { AuthState } from "./auth/auth-reducer";
 import dataExplorerReducer, { DataExplorerState } from './data-explorer/data-explorer-reducer';
 import collectionsReducer, { CollectionState } from "./collection/collection-reducer";
+import { projectPanelMiddleware } from '../views/project-panel/project-panel-middleware';
 
 const composeEnhancers =
     (process.env.NODE_ENV === 'development' &&
@@ -40,7 +41,8 @@ const rootReducer = combineReducers({
 export default function configureStore(history: History) {
     const middlewares: Middleware[] = [
         routerMiddleware(history),
-        thunkMiddleware
+        thunkMiddleware,
+        projectPanelMiddleware
     ];
     const enhancer = composeEnhancers(applyMiddleware(...middlewares));
     return createStore(rootReducer, enhancer);
index e0eb84f05ad4c16c810dd6a8b9e477b89ae11df7..cf77aaf14e2c84ba6be3f749c004eb83532ae24f 100644 (file)
@@ -2,14 +2,13 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { TreeItem } from "../../components/tree/tree";
-import { Project } from "../../models/project";
-import { getResourceKind, Resource, ResourceKind } from "../../models/resource";
+import { Resource } from "../../common/api/common-resource-service";
+import { DataItem } from "../../components/data-table/data-table";
 
-export interface ProjectPanelItem {
+export interface ProjectPanelItem extends DataItem {
     uuid: string;
     name: string;
-    kind: ResourceKind;
+    kind: string;
     url: string;
     owner: string;
     lastModified: string;
@@ -17,11 +16,13 @@ export interface ProjectPanelItem {
     status?: string;
 }
 
-function resourceToDataItem(r: Resource, kind?: ResourceKind) {
+export function resourceToDataItem(r: Resource): ProjectPanelItem {
     return {
+        key: r.uuid,
         uuid: r.uuid,
-        name: r.name,
-        kind: kind ? kind : getResourceKind(r.kind),
+        name: r.uuid,
+        kind: r.kind,
+        url: "",
         owner: r.ownerUuid,
         lastModified: r.modifiedAt
     };
diff --git a/src/views/project-panel/project-panel-middleware.ts b/src/views/project-panel/project-panel-middleware.ts
new file mode 100644 (file)
index 0000000..9be5981
--- /dev/null
@@ -0,0 +1,55 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { Middleware } from "redux";
+import actions from "../../store/data-explorer/data-explorer-action";
+import { PROJECT_PANEL_ID, columns } from "./project-panel";
+import { groupsService } from "../../services/services";
+import { RootState } from "../../store/store";
+import { getDataExplorer } from "../../store/data-explorer/data-explorer-reducer";
+import { resourceToDataItem } from "./project-panel-item";
+
+export const projectPanelMiddleware: Middleware = store => next => {
+    next(actions.SET_COLUMNS({ id: PROJECT_PANEL_ID, columns }));
+
+    return action => {
+
+        const handleProjectPanelAction = <T extends { id: string }>(handler: (data: T) => void) =>
+            (data: T) => {
+                next(action);
+                if (data.id === PROJECT_PANEL_ID) {
+                    handler(data);
+                }
+            };
+
+        actions.match(action, {
+            SET_PAGE: handleProjectPanelAction(() => {
+                store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
+            }),
+            SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
+                store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
+            }),
+            REQUEST_ITEMS: handleProjectPanelAction(() => {
+                const state = store.getState() as RootState;
+                const dataExplorer = getDataExplorer(state.dataExplorer, PROJECT_PANEL_ID);
+                groupsService
+                    .contents(state.projects.currentItemId, {
+                        limit: dataExplorer.rowsPerPage,
+                        offset: dataExplorer.page * dataExplorer.rowsPerPage,
+                    })
+                    .then(response => {
+                        store.dispatch(actions.SET_ITEMS({
+                            id: PROJECT_PANEL_ID,
+                            items: response.items.map(resourceToDataItem),
+                            itemsAvailable: response.itemsAvailable,
+                            page: Math.floor(response.offset / response.limit),
+                            rowsPerPage: response.limit
+                        }));
+                    });
+
+            }),
+            default: () => next(action)
+        });
+    };
+};