X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/cc493b89840b48f40c2beaf626994724331aa196..2f39812f57c2acbc20a6292964b0a6b3512a9870:/src/store/tree-picker/tree-picker-actions.ts?ds=sidebyside diff --git a/src/store/tree-picker/tree-picker-actions.ts b/src/store/tree-picker/tree-picker-actions.ts index 5b04389a..f0f6bfcd 100644 --- a/src/store/tree-picker/tree-picker-actions.ts +++ b/src/store/tree-picker/tree-picker-actions.ts @@ -3,16 +3,82 @@ // SPDX-License-Identifier: AGPL-3.0 import { unionize, ofType, UnionOf } from "~/common/unionize"; - -import { TreePickerNode } from "./tree-picker"; +import { TreeNode, initTreeNode, getNodeDescendants, getNodeDescendantsIds, getNodeValue, TreeNodeStatus } from '~/models/tree'; +import { Dispatch } from 'redux'; +import { RootState } from '~/store/store'; +import { ServiceRepository } from '~/services/services'; +import { FilterBuilder } from '~/services/api/filter-builder'; +import { pipe } from 'lodash/fp'; +import { ResourceKind } from '~/models/resource'; +import { GroupContentsResource } from '../../services/groups-service/groups-service'; +import { CollectionDirectory, CollectionFile } from '../../models/collection-file'; export const treePickerActions = unionize({ - LOAD_TREE_PICKER_NODE: ofType<{ nodeId: string, pickerId: string }>(), - LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ nodeId: string, nodes: Array, pickerId: string }>(), - TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ nodeId: string, pickerId: string }>(), - TOGGLE_TREE_PICKER_NODE_SELECT: ofType<{ nodeId: string, pickerId: string }>(), - EXPAND_TREE_PICKER_NODES: ofType<{ nodeIds: string[], pickerId: string }>(), + LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(), + LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array>, pickerId: string }>(), + TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(), + ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(), + TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(), + EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(), RESET_TREE_PICKER: ofType<{ pickerId: string }>() }); export type TreePickerAction = UnionOf; + +interface ReceiveTreePickerDataParams { + data: T[]; + extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus }; + id: string; + pickerId: string; +} +export const receiveTreePickerData = (params: ReceiveTreePickerDataParams) => + (dispatch: Dispatch) => { + const { data, extractNodeData, id, pickerId, } = params; + dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ + id, + nodes: data.map(item => initTreeNode(extractNodeData(item))), + pickerId, + })); + dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId })); + }; + +export const loadProject = (id: string, pickerId: string, include?: ResourceKind[]) => + async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => { + dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId })); + + const filters = pipe( + (fb: FilterBuilder) => fb.addEqual('ownerUuid', id), + fb => include ? fb.addIsA('uuid', include) : fb, + fb => fb.getFilters(), + )(new FilterBuilder()); + + const { items } = await services.groupsService.contents(id, { filters }); + + dispatch(receiveTreePickerData({ + id, + pickerId, + data: items, + extractNodeData: item => ({ id: item.uuid, value: item }), + })); + }; + +export const loadCollection = (id: string, pickerId: string) => + async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => { + dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId })); + + const files = await services.collectionService.files(id); + const data = getNodeDescendants('')(files).map(node => node.value); + + dispatch(receiveTreePickerData({ + id, + pickerId, + data, + extractNodeData: value => { + return { + id: value.id, + value, + status: TreeNodeStatus.LOADED, + }; + }, + })); + };