3c3b052af3aefc7e1ef4f76676d55a40815fd69c
[arvados-workbench2.git] / src / store / tree-picker / tree-picker-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
15
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 }>()
24 });
25
26 export type TreePickerAction = UnionOf<typeof treePickerActions>;
27
28 interface ReceiveTreePickerDataParams<T> {
29     data: T[];
30     extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
31     id: string;
32     pickerId: string;
33 }
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({
38             id,
39             nodes: data.map(item => initTreeNode(extractNodeData(item))),
40             pickerId,
41         }));
42         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
43     };
44 export const loadProject = (id: string, pickerId: string, includeCollections = false, includeFiles = false) =>
45     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
46
47         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
48
49         const filters = pipe(
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());
56
57         const { items } = await services.groupsService.contents(id, { filters });
58
59         dispatch<any>(receiveTreePickerData<GroupContentsResource>({
60             id,
61             pickerId,
62             data: items,
63             extractNodeData: item => ({
64                 id: item.uuid,
65                 value: item,
66                 status: item.kind === ResourceKind.PROJECT
67                     ? TreeNodeStatus.INITIAL
68                     : includeFiles
69                         ? TreeNodeStatus.INITIAL
70                         : TreeNodeStatus.LOADED
71             }),
72         }));
73     };
74
75 export const loadCollection = (id: string, pickerId: string) =>
76     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
77         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
78
79         const files = await services.collectionService.files(id);
80         const data = getNodeDescendants('')(files).map(node => node.value);
81
82         dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
83             id,
84             pickerId,
85             data,
86             extractNodeData: value => ({
87                 id: value.id,
88                 status: TreeNodeStatus.LOADED,
89                 value,
90             }),
91         }));
92     };
93
94
95 export const initUserProject = (pickerId: string) =>
96     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
97         const uuid = services.authService.getUuid();
98         if (uuid) {
99             dispatch(receiveTreePickerData({
100                 id: '',
101                 pickerId,
102                 data: [{ uuid, name: 'Projects' }],
103                 extractNodeData: value => ({
104                     id: value.uuid,
105                     status: TreeNodeStatus.INITIAL,
106                     value,
107                 }),
108             }));
109         }
110     };
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();
114         if (uuid) {
115             dispatch(loadProject(uuid, pickerId, includeCollections, includeFiles));
116         }
117     };
118
119
120 export const initSharedProject = (pickerId: string) =>
121     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
122         dispatch(receiveTreePickerData({
123             id: '',
124             pickerId,
125             data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
126             extractNodeData: value => ({
127                 id: value.uuid,
128                 status: TreeNodeStatus.INITIAL,
129                 value,
130             }),
131         }));
132     };