1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { unionize, ofType, UnionOf } from "~/common/unionize";
6 import { TreeNode, initTreeNode, getNodeDescendants, getNodeDescendantsIds, getNodeValue, TreeNodeStatus } from '~/models/tree';
7 import { Dispatch } from 'redux';
8 import { RootState } from '~/store/store';
9 import { ServiceRepository } from '~/services/services';
10 import { FilterBuilder } from '~/services/api/filter-builder';
11 import { pipe } from 'lodash/fp';
12 import { ResourceKind } from '~/models/resource';
13 import { GroupContentsResource } from '../../services/groups-service/groups-service';
14 import { CollectionDirectory, CollectionFile } from '../../models/collection-file';
16 export const treePickerActions = unionize({
17 LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
18 LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array<TreeNode<any>>, pickerId: string }>(),
19 TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
20 ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
21 TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(),
22 EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
23 RESET_TREE_PICKER: ofType<{ pickerId: string }>()
26 export type TreePickerAction = UnionOf<typeof treePickerActions>;
28 interface ReceiveTreePickerDataParams<T> {
30 extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
34 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
35 (dispatch: Dispatch) => {
36 const { data, extractNodeData, id, pickerId, } = params;
37 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
39 nodes: data.map(item => initTreeNode(extractNodeData(item))),
42 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
44 export const loadProject = (id: string, pickerId: string, includeCollections = false, includeFiles = false) =>
45 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
47 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
50 (fb: FilterBuilder) => fb.addEqual('ownerUuid', id),
51 fb => includeCollections
52 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
53 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
54 fb => fb.getFilters(),
55 )(new FilterBuilder());
57 const { items } = await services.groupsService.contents(id, { filters });
59 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
63 extractNodeData: item => ({
66 status: item.kind === ResourceKind.PROJECT
67 ? TreeNodeStatus.INITIAL
69 ? TreeNodeStatus.INITIAL
70 : TreeNodeStatus.LOADED
75 export const loadCollection = (id: string, pickerId: string) =>
76 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
77 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
79 const files = await services.collectionService.files(id);
80 const data = getNodeDescendants('')(files).map(node => node.value);
82 dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
86 extractNodeData: value => ({
88 status: TreeNodeStatus.LOADED,
95 export const initUserProject = (pickerId: string) =>
96 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
97 const uuid = services.authService.getUuid();
99 dispatch(receiveTreePickerData({
102 data: [{ uuid, name: 'Projects' }],
103 extractNodeData: value => ({
105 status: TreeNodeStatus.INITIAL,
111 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
112 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
113 const uuid = services.authService.getUuid();
115 dispatch(loadProject(uuid, pickerId, includeCollections, includeFiles));
120 export const initSharedProject = (pickerId: string) =>
121 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
122 dispatch(receiveTreePickerData({
125 data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
126 extractNodeData: value => ({
128 status: TreeNodeStatus.INITIAL,