From: Michal Klobukowski Date: Wed, 4 Jul 2018 10:27:20 +0000 (+0200) Subject: Create project panel middleware X-Git-Tag: 1.2.0~59^2~25 X-Git-Url: https://git.arvados.org/arvados-workbench2.git/commitdiff_plain/3816facfe98d5ee9c0b825880188214e336b0506 Create project panel middleware Feature #13703 Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski --- diff --git a/src/store/navigation/navigation-action.ts b/src/store/navigation/navigation-action.ts index daeb26fd..ec6e9bb5 100644 --- a/src/store/navigation/navigation-action.ts +++ b/src/store/navigation/navigation-action.ts @@ -61,17 +61,9 @@ export const setProjectItem = (itemId: string, itemMode: ItemMode) => : dispatch(getProjectList(itemId)); promise - .then(() => dispatch(getCollectionList(itemId))) .then(() => dispatch(() => { - 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})); })); } diff --git a/src/store/store.ts b/src/store/store.ts index 68c5d823..0c32e653 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -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); diff --git a/src/views/project-panel/project-panel-item.ts b/src/views/project-panel/project-panel-item.ts index e0eb84f0..cf77aaf1 100644 --- a/src/views/project-panel/project-panel-item.ts +++ b/src/views/project-panel/project-panel-item.ts @@ -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 index 00000000..9be59811 --- /dev/null +++ b/src/views/project-panel/project-panel-middleware.ts @@ -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 = (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) + }); + }; +};