Merge branch 'master'
[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     DEACTIVATE_TREE_PICKER_NODE: ofType<{ pickerId: string }>(),
22     TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(),
23     EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
24     RESET_TREE_PICKER: ofType<{ pickerId: string }>()
25 });
26
27 export type TreePickerAction = UnionOf<typeof treePickerActions>;
28
29 export const getProjectsTreePickerIds = (pickerId: string) => ({
30     home: `${pickerId}_home`,
31     shared: `${pickerId}_shared`,
32     favorites: `${pickerId}_favorites`,
33 });
34 export const initProjectsTreePicker = (pickerId: string) => 
35 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
36     const {home, shared, favorites} = getProjectsTreePickerIds(pickerId);
37     dispatch<any>(initUserProject(home));
38     dispatch<any>(initSharedProject(shared));
39     dispatch<any>(initFavoritesProject(favorites));
40 };
41
42 interface ReceiveTreePickerDataParams<T> {
43     data: T[];
44     extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
45     id: string;
46     pickerId: string;
47 }
48 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
49     (dispatch: Dispatch) => {
50         const { data, extractNodeData, id, pickerId, } = params;
51         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
52             id,
53             nodes: data.map(item => initTreeNode(extractNodeData(item))),
54             pickerId,
55         }));
56         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
57     };
58
59 interface LoadProjectParams {
60     id: string;
61     pickerId: string;
62     includeCollections?: boolean;
63     includeFiles?: boolean;
64     loadShared?: boolean;
65 }
66 export const loadProject = (params: LoadProjectParams) =>
67     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
68         const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
69
70         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
71
72         const filters = pipe(
73             (fb: FilterBuilder) => includeCollections
74                 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
75                 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
76             fb => fb.getFilters(),
77         )(new FilterBuilder());
78
79         const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
80
81         dispatch<any>(receiveTreePickerData<GroupContentsResource>({
82             id,
83             pickerId,
84             data: items,
85             extractNodeData: item => ({
86                 id: item.uuid,
87                 value: item,
88                 status: item.kind === ResourceKind.PROJECT
89                     ? TreeNodeStatus.INITIAL
90                     : includeFiles
91                         ? TreeNodeStatus.INITIAL
92                         : TreeNodeStatus.LOADED
93             }),
94         }));
95     };
96
97 export const loadCollection = (id: string, pickerId: string) =>
98     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
99         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
100
101         const files = await services.collectionService.files(id);
102         const data = getNodeDescendants('')(files).map(node => node.value);
103
104         dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
105             id,
106             pickerId,
107             data,
108             extractNodeData: value => ({
109                 id: value.id,
110                 status: TreeNodeStatus.LOADED,
111                 value,
112             }),
113         }));
114     };
115
116
117 export const initUserProject = (pickerId: string) =>
118     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
119         const uuid = services.authService.getUuid();
120         if (uuid) {
121             dispatch(receiveTreePickerData({
122                 id: '',
123                 pickerId,
124                 data: [{ uuid, name: 'Projects' }],
125                 extractNodeData: value => ({
126                     id: value.uuid,
127                     status: TreeNodeStatus.INITIAL,
128                     value,
129                 }),
130             }));
131         }
132     };
133 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
134     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
135         const uuid = services.authService.getUuid();
136         if (uuid) {
137             dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
138         }
139     };
140
141
142 export const initSharedProject = (pickerId: string) =>
143     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
144         dispatch(receiveTreePickerData({
145             id: '',
146             pickerId,
147             data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
148             extractNodeData: value => ({
149                 id: value.uuid,
150                 status: TreeNodeStatus.INITIAL,
151                 value,
152             }),
153         }));
154     };
155
156 export const initFavoritesProject = (pickerId: string) =>
157     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
158         dispatch(receiveTreePickerData({
159             id: '',
160             pickerId,
161             data: [{ uuid: 'Favorites', name: 'Favorites' }],
162             extractNodeData: value => ({
163                 id: value.uuid,
164                 status: TreeNodeStatus.INITIAL,
165                 value,
166             }),
167         }));
168     };
169
170 interface LoadFavoritesProjectParams {
171     pickerId: string;
172     includeCollections?: boolean;
173     includeFiles?: boolean;
174 }
175 export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
176     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
177         const { pickerId, includeCollections = false, includeFiles = false } = params;
178         const uuid = services.authService.getUuid();
179         if (uuid) {
180
181             const filters = pipe(
182                 (fb: FilterBuilder) => includeCollections
183                     ? fb.addIsA('headUuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
184                     : fb.addIsA('headUuid', [ResourceKind.PROJECT]),
185                 fb => fb.getFilters(),
186             )(new FilterBuilder());
187
188             const { items } = await services.favoriteService.list(uuid, { filters });
189
190             dispatch<any>(receiveTreePickerData<GroupContentsResource>({
191                 id: 'Favorites',
192                 pickerId,
193                 data: items,
194                 extractNodeData: item => ({
195                     id: item.uuid,
196                     value: item,
197                     status: item.kind === ResourceKind.PROJECT
198                         ? TreeNodeStatus.INITIAL
199                         : includeFiles
200                             ? TreeNodeStatus.INITIAL
201                             : TreeNodeStatus.LOADED
202                 }),
203             }));
204         }
205     };