Merge branch 'master' into 14231-multiple-collections-input
[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, getNode } 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, map, values, mapValues } 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 import { getTreePicker, TreePicker } from './tree-picker';
16 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
17
18 export const treePickerActions = unionize({
19     LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
20     LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array<TreeNode<any>>, pickerId: string }>(),
21     TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
22     ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
23     DEACTIVATE_TREE_PICKER_NODE: ofType<{ pickerId: string }>(),
24     TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(),
25     SELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
26     DESELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
27     EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
28     RESET_TREE_PICKER: ofType<{ pickerId: string }>()
29 });
30
31 export type TreePickerAction = UnionOf<typeof treePickerActions>;
32
33 export const getProjectsTreePickerIds = (pickerId: string) => ({
34     home: `${pickerId}_home`,
35     shared: `${pickerId}_shared`,
36     favorites: `${pickerId}_favorites`,
37 });
38
39 export const getAllNodes = <Value>(pickerId: string, filter = (node: TreeNode<Value>) => true) => (state: TreePicker) =>
40     pipe(
41         () => values(getProjectsTreePickerIds(pickerId)),
42
43         ids => ids
44             .map(id => getTreePicker<Value>(id)(state)),
45
46         trees => trees
47             .map(getNodeDescendants(''))
48             .reduce((allNodes, nodes) => allNodes.concat(nodes), []),
49
50         allNodes => allNodes
51             .reduce((map, node) =>
52                 filter(node)
53                     ? map.set(node.id, node)
54                     : map, new Map<string, TreeNode<Value>>())
55             .values(),
56
57         uniqueNodes => Array.from(uniqueNodes),
58     )();
59 export const getSelectedNodes = <Value>(pickerId: string) => (state: TreePicker) =>
60     getAllNodes<Value>(pickerId, node => node.selected)(state);
61     
62 export const initProjectsTreePicker = (pickerId: string) =>
63     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
64         const { home, shared, favorites } = getProjectsTreePickerIds(pickerId);
65         dispatch<any>(initUserProject(home));
66         dispatch<any>(initSharedProject(shared));
67         dispatch<any>(initFavoritesProject(favorites));
68     };
69
70 interface ReceiveTreePickerDataParams<T> {
71     data: T[];
72     extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
73     id: string;
74     pickerId: string;
75 }
76 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
77     (dispatch: Dispatch) => {
78         const { data, extractNodeData, id, pickerId, } = params;
79         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
80             id,
81             nodes: data.map(item => initTreeNode(extractNodeData(item))),
82             pickerId,
83         }));
84         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
85     };
86
87 interface LoadProjectParams {
88     id: string;
89     pickerId: string;
90     includeCollections?: boolean;
91     includeFiles?: boolean;
92     loadShared?: boolean;
93 }
94 export const loadProject = (params: LoadProjectParams) =>
95     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
96         const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
97
98         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
99
100         const filters = pipe(
101             (fb: FilterBuilder) => includeCollections
102                 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
103                 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
104             fb => fb.getFilters(),
105         )(new FilterBuilder());
106
107         const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
108
109         dispatch<any>(receiveTreePickerData<GroupContentsResource>({
110             id,
111             pickerId,
112             data: items,
113             extractNodeData: item => ({
114                 id: item.uuid,
115                 value: item,
116                 status: item.kind === ResourceKind.PROJECT
117                     ? TreeNodeStatus.INITIAL
118                     : includeFiles
119                         ? TreeNodeStatus.INITIAL
120                         : TreeNodeStatus.LOADED
121             }),
122         }));
123     };
124
125 export const loadCollection = (id: string, pickerId: string) =>
126     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
127         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
128
129         const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
130         if (picker) {
131
132             const node = getNode(id)(picker);
133             if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
134
135                 const files = await services.collectionService.files(node.value.portableDataHash);
136                 const data = getNodeDescendants('')(files).map(node => node.value);
137
138                 dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
139                     id,
140                     pickerId,
141                     data,
142                     extractNodeData: value => ({
143                         id: value.id,
144                         status: TreeNodeStatus.LOADED,
145                         value,
146                     }),
147                 }));
148             }
149         }
150     };
151
152
153 export const initUserProject = (pickerId: string) =>
154     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
155         const uuid = services.authService.getUuid();
156         if (uuid) {
157             dispatch(receiveTreePickerData({
158                 id: '',
159                 pickerId,
160                 data: [{ uuid, name: 'Projects' }],
161                 extractNodeData: value => ({
162                     id: value.uuid,
163                     status: TreeNodeStatus.INITIAL,
164                     value,
165                 }),
166             }));
167         }
168     };
169 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
170     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
171         const uuid = services.authService.getUuid();
172         if (uuid) {
173             dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
174         }
175     };
176
177
178 export const initSharedProject = (pickerId: string) =>
179     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
180         dispatch(receiveTreePickerData({
181             id: '',
182             pickerId,
183             data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
184             extractNodeData: value => ({
185                 id: value.uuid,
186                 status: TreeNodeStatus.INITIAL,
187                 value,
188             }),
189         }));
190     };
191
192 export const initFavoritesProject = (pickerId: string) =>
193     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
194         dispatch(receiveTreePickerData({
195             id: '',
196             pickerId,
197             data: [{ uuid: 'Favorites', name: 'Favorites' }],
198             extractNodeData: value => ({
199                 id: value.uuid,
200                 status: TreeNodeStatus.INITIAL,
201                 value,
202             }),
203         }));
204     };
205
206 interface LoadFavoritesProjectParams {
207     pickerId: string;
208     includeCollections?: boolean;
209     includeFiles?: boolean;
210 }
211 export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
212     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
213         const { pickerId, includeCollections = false, includeFiles = false } = params;
214         const uuid = services.authService.getUuid();
215         if (uuid) {
216
217             const filters = pipe(
218                 (fb: FilterBuilder) => includeCollections
219                     ? fb.addIsA('headUuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
220                     : fb.addIsA('headUuid', [ResourceKind.PROJECT]),
221                 fb => fb.getFilters(),
222             )(new FilterBuilder());
223
224             const { items } = await services.favoriteService.list(uuid, { filters });
225
226             dispatch<any>(receiveTreePickerData<GroupContentsResource>({
227                 id: 'Favorites',
228                 pickerId,
229                 data: items,
230                 extractNodeData: item => ({
231                     id: item.uuid,
232                     value: item,
233                     status: item.kind === ResourceKind.PROJECT
234                         ? TreeNodeStatus.INITIAL
235                         : includeFiles
236                             ? TreeNodeStatus.INITIAL
237                             : TreeNodeStatus.LOADED
238                 }),
239             }));
240         }
241     };