1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
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 }>()
27 export type TreePickerAction = UnionOf<typeof treePickerActions>;
29 export const getProjectsTreePickerIds = (pickerId: string) => ({
30 home: `${pickerId}_home`,
31 shared: `${pickerId}_shared`,
32 favorites: `${pickerId}_favorites`,
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));
42 interface ReceiveTreePickerDataParams<T> {
44 extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
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({
53 nodes: data.map(item => initTreeNode(extractNodeData(item))),
56 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
59 interface LoadProjectParams {
62 includeCollections?: boolean;
63 includeFiles?: boolean;
66 export const loadProject = (params: LoadProjectParams) =>
67 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
68 const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
70 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
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());
79 const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
81 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
85 extractNodeData: item => ({
88 status: item.kind === ResourceKind.PROJECT
89 ? TreeNodeStatus.INITIAL
91 ? TreeNodeStatus.INITIAL
92 : TreeNodeStatus.LOADED
97 export const loadCollection = (id: string, pickerId: string) =>
98 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
99 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
101 const files = await services.collectionService.files(id);
102 const data = getNodeDescendants('')(files).map(node => node.value);
104 dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
108 extractNodeData: value => ({
110 status: TreeNodeStatus.LOADED,
117 export const initUserProject = (pickerId: string) =>
118 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
119 const uuid = services.authService.getUuid();
121 dispatch(receiveTreePickerData({
124 data: [{ uuid, name: 'Projects' }],
125 extractNodeData: value => ({
127 status: TreeNodeStatus.INITIAL,
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();
137 dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
142 export const initSharedProject = (pickerId: string) =>
143 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
144 dispatch(receiveTreePickerData({
147 data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
148 extractNodeData: value => ({
150 status: TreeNodeStatus.INITIAL,
156 export const initFavoritesProject = (pickerId: string) =>
157 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
158 dispatch(receiveTreePickerData({
161 data: [{ uuid: 'Favorites', name: 'Favorites' }],
162 extractNodeData: value => ({
164 status: TreeNodeStatus.INITIAL,
170 interface LoadFavoritesProjectParams {
172 includeCollections?: boolean;
173 includeFiles?: boolean;
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();
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());
188 const { items } = await services.favoriteService.list(uuid, { filters });
190 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
194 extractNodeData: item => ({
197 status: item.kind === ResourceKind.PROJECT
198 ? TreeNodeStatus.INITIAL
200 ? TreeNodeStatus.INITIAL
201 : TreeNodeStatus.LOADED