41ee62f8e1910e5fb1c274711c8138bda4489d8d
[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
45 interface LoadProjectParams {
46     id: string;
47     pickerId: string;
48     includeCollections?: boolean;
49     includeFiles?: boolean;
50     loadShared?: boolean;
51 }
52 export const loadProject = (params: LoadProjectParams) =>
53     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
54         const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
55
56         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
57
58         const filters = pipe(
59             (fb: FilterBuilder) => includeCollections
60                 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
61                 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
62             fb => fb.getFilters(),
63         )(new FilterBuilder());
64
65         const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
66
67         dispatch<any>(receiveTreePickerData<GroupContentsResource>({
68             id,
69             pickerId,
70             data: items,
71             extractNodeData: item => ({
72                 id: item.uuid,
73                 value: item,
74                 status: item.kind === ResourceKind.PROJECT
75                     ? TreeNodeStatus.INITIAL
76                     : includeFiles
77                         ? TreeNodeStatus.INITIAL
78                         : TreeNodeStatus.LOADED
79             }),
80         }));
81     };
82
83 export const loadCollection = (id: string, pickerId: string) =>
84     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
85         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
86
87         const files = await services.collectionService.files(id);
88         const data = getNodeDescendants('')(files).map(node => node.value);
89
90         dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
91             id,
92             pickerId,
93             data,
94             extractNodeData: value => ({
95                 id: value.id,
96                 status: TreeNodeStatus.LOADED,
97                 value,
98             }),
99         }));
100     };
101
102
103 export const initUserProject = (pickerId: string) =>
104     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
105         const uuid = services.authService.getUuid();
106         if (uuid) {
107             dispatch(receiveTreePickerData({
108                 id: '',
109                 pickerId,
110                 data: [{ uuid, name: 'Projects' }],
111                 extractNodeData: value => ({
112                     id: value.uuid,
113                     status: TreeNodeStatus.INITIAL,
114                     value,
115                 }),
116             }));
117         }
118     };
119 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
120     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
121         const uuid = services.authService.getUuid();
122         if (uuid) {
123             dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
124         }
125     };
126
127
128 export const initSharedProject = (pickerId: string) =>
129     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
130         dispatch(receiveTreePickerData({
131             id: '',
132             pickerId,
133             data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
134             extractNodeData: value => ({
135                 id: value.uuid,
136                 status: TreeNodeStatus.INITIAL,
137                 value,
138             }),
139         }));
140     };
141
142 export const initFavoritesProject = (pickerId: string) =>
143     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
144         dispatch(receiveTreePickerData({
145             id: '',
146             pickerId,
147             data: [{ uuid: 'Favorites', name: 'Favorites' }],
148             extractNodeData: value => ({
149                 id: value.uuid,
150                 status: TreeNodeStatus.INITIAL,
151                 value,
152             }),
153         }));
154     };
155
156 interface LoadFavoritesProjectParams {
157     pickerId: string;
158     includeCollections?: boolean;
159     includeFiles?: boolean;
160 }
161 export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
162     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
163         const { pickerId, includeCollections = false, includeFiles = false } = params;
164         const uuid = services.authService.getUuid();
165         if (uuid) {
166
167             const filters = pipe(
168                 (fb: FilterBuilder) => includeCollections
169                     ? fb.addIsA('headUuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
170                     : fb.addIsA('headUuid', [ResourceKind.PROJECT]),
171                 fb => fb.getFilters(),
172             )(new FilterBuilder());
173
174             const { items } = await services.favoriteService.list(uuid, { filters });
175
176             dispatch<any>(receiveTreePickerData<GroupContentsResource>({
177                 id: 'Favorites',
178                 pickerId,
179                 data: items,
180                 extractNodeData: item => ({
181                     id: item.uuid,
182                     value: item,
183                     status: item.kind === ResourceKind.PROJECT
184                         ? TreeNodeStatus.INITIAL
185                         : includeFiles
186                             ? TreeNodeStatus.INITIAL
187                             : TreeNodeStatus.LOADED
188                 }),
189             }));
190         }
191     };