Create file array 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 getSelectedNodes = <Value>(pickerId: string) => (state: TreePicker) => {
40     return 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) => node.selected
52                 ? map.set(node.id, node)
53                 : map, new Map<string, TreeNode<Value>>())
54             .values(),
55
56         uniqueNodes => Array.from(uniqueNodes),
57     )();
58 };
59 export const initProjectsTreePicker = (pickerId: string) =>
60     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
61         const { home, shared, favorites } = getProjectsTreePickerIds(pickerId);
62         dispatch<any>(initUserProject(home));
63         dispatch<any>(initSharedProject(shared));
64         dispatch<any>(initFavoritesProject(favorites));
65     };
66
67 interface ReceiveTreePickerDataParams<T> {
68     data: T[];
69     extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
70     id: string;
71     pickerId: string;
72 }
73 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
74     (dispatch: Dispatch) => {
75         const { data, extractNodeData, id, pickerId, } = params;
76         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
77             id,
78             nodes: data.map(item => initTreeNode(extractNodeData(item))),
79             pickerId,
80         }));
81         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
82     };
83
84 interface LoadProjectParams {
85     id: string;
86     pickerId: string;
87     includeCollections?: boolean;
88     includeFiles?: boolean;
89     loadShared?: boolean;
90 }
91 export const loadProject = (params: LoadProjectParams) =>
92     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
93         const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
94
95         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
96
97         const filters = pipe(
98             (fb: FilterBuilder) => includeCollections
99                 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
100                 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
101             fb => fb.getFilters(),
102         )(new FilterBuilder());
103
104         const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
105
106         dispatch<any>(receiveTreePickerData<GroupContentsResource>({
107             id,
108             pickerId,
109             data: items,
110             extractNodeData: item => ({
111                 id: item.uuid,
112                 value: item,
113                 status: item.kind === ResourceKind.PROJECT
114                     ? TreeNodeStatus.INITIAL
115                     : includeFiles
116                         ? TreeNodeStatus.INITIAL
117                         : TreeNodeStatus.LOADED
118             }),
119         }));
120     };
121
122 export const loadCollection = (id: string, pickerId: string) =>
123     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
124         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
125
126         const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
127         if (picker) {
128
129             const node = getNode(id)(picker);
130             if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
131
132                 const files = await services.collectionService.files(node.value.portableDataHash);
133                 const data = getNodeDescendants('')(files).map(node => node.value);
134
135                 dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
136                     id,
137                     pickerId,
138                     data,
139                     extractNodeData: value => ({
140                         id: value.id,
141                         status: TreeNodeStatus.LOADED,
142                         value,
143                     }),
144                 }));
145             }
146         }
147     };
148
149
150 export const initUserProject = (pickerId: string) =>
151     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
152         const uuid = services.authService.getUuid();
153         if (uuid) {
154             dispatch(receiveTreePickerData({
155                 id: '',
156                 pickerId,
157                 data: [{ uuid, name: 'Projects' }],
158                 extractNodeData: value => ({
159                     id: value.uuid,
160                     status: TreeNodeStatus.INITIAL,
161                     value,
162                 }),
163             }));
164         }
165     };
166 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
167     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
168         const uuid = services.authService.getUuid();
169         if (uuid) {
170             dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
171         }
172     };
173
174
175 export const initSharedProject = (pickerId: string) =>
176     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
177         dispatch(receiveTreePickerData({
178             id: '',
179             pickerId,
180             data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
181             extractNodeData: value => ({
182                 id: value.uuid,
183                 status: TreeNodeStatus.INITIAL,
184                 value,
185             }),
186         }));
187     };
188
189 export const initFavoritesProject = (pickerId: string) =>
190     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
191         dispatch(receiveTreePickerData({
192             id: '',
193             pickerId,
194             data: [{ uuid: 'Favorites', name: 'Favorites' }],
195             extractNodeData: value => ({
196                 id: value.uuid,
197                 status: TreeNodeStatus.INITIAL,
198                 value,
199             }),
200         }));
201     };
202
203 interface LoadFavoritesProjectParams {
204     pickerId: string;
205     includeCollections?: boolean;
206     includeFiles?: boolean;
207 }
208 export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
209     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
210         const { pickerId, includeCollections = false, includeFiles = false } = params;
211         const uuid = services.authService.getUuid();
212         if (uuid) {
213
214             const filters = pipe(
215                 (fb: FilterBuilder) => includeCollections
216                     ? fb.addIsA('headUuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
217                     : fb.addIsA('headUuid', [ResourceKind.PROJECT]),
218                 fb => fb.getFilters(),
219             )(new FilterBuilder());
220
221             const { items } = await services.favoriteService.list(uuid, { filters });
222
223             dispatch<any>(receiveTreePickerData<GroupContentsResource>({
224                 id: 'Favorites',
225                 pickerId,
226                 data: items,
227                 extractNodeData: item => ({
228                     id: item.uuid,
229                     value: item,
230                     status: item.kind === ResourceKind.PROJECT
231                         ? TreeNodeStatus.INITIAL
232                         : includeFiles
233                             ? TreeNodeStatus.INITIAL
234                             : TreeNodeStatus.LOADED
235                 }),
236             }));
237         }
238     };