17306: Added configurable options for favorite tree
[arvados-workbench2.git] / src / store / tree-picker / tree-picker-actions.ts
index a3816354828d2478d7125d001e699165d0005e36..d11f7527b4e0d7d18a1e6e9d1a0ecbddfae67e35 100644 (file)
@@ -4,8 +4,10 @@
 
 import { unionize, ofType, UnionOf } from "~/common/unionize";
 import { TreeNode, initTreeNode, getNodeDescendants, TreeNodeStatus, getNode, TreePickerId, Tree } from '~/models/tree';
+import { createCollectionFilesTree } from "~/models/collection-file";
 import { Dispatch } from 'redux';
 import { RootState } from '~/store/store';
+import { getUserUuid } from "~/common/getuser";
 import { ServiceRepository } from '~/services/services';
 import { FilterBuilder } from '~/services/api/filter-builder';
 import { pipe, values } from 'lodash/fp';
@@ -16,6 +18,10 @@ import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/
 import { OrderBuilder } from '~/services/api/order-builder';
 import { ProjectResource } from '~/models/project';
 import { mapTree } from '../../models/tree';
+import { LinkResource, LinkClass } from "~/models/link";
+import { mapTreeValues } from "~/models/tree";
+import { sortFilesTree } from "~/services/collection-service/collection-service-files-response";
+import { GroupResource } from "~/models/group";
 
 export const treePickerActions = unionize({
     LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
@@ -37,6 +43,7 @@ export const getProjectsTreePickerIds = (pickerId: string) => ({
     home: `${pickerId}_home`,
     shared: `${pickerId}_shared`,
     favorites: `${pickerId}_favorites`,
+    publicFavorites: `${pickerId}_publicFavorites`
 });
 
 export const getAllNodes = <Value>(pickerId: string, filter = (node: TreeNode<Value>) => true) => (state: TreePicker) =>
@@ -64,10 +71,11 @@ export const getSelectedNodes = <Value>(pickerId: string) => (state: TreePicker)
 
 export const initProjectsTreePicker = (pickerId: string) =>
     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
-        const { home, shared, favorites } = getProjectsTreePickerIds(pickerId);
+        const { home, shared, favorites, publicFavorites } = getProjectsTreePickerIds(pickerId);
         dispatch<any>(initUserProject(home));
         dispatch<any>(initSharedProject(shared));
         dispatch<any>(initFavoritesProject(favorites));
+        dispatch<any>(initPublicFavoritesProject(publicFavorites));
     };
 
 interface ReceiveTreePickerDataParams<T> {
@@ -136,8 +144,11 @@ export const loadCollection = (id: string, pickerId: string) =>
             const node = getNode(id)(picker);
             if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
 
-                const filesTree = await services.collectionService.files(node.value.portableDataHash);
-                
+                const files = await services.collectionService.files(node.value.portableDataHash);
+                const tree = createCollectionFilesTree(files);
+                const sorted = sortFilesTree(tree);
+                const filesTree = mapTreeValues(services.collectionService.extendFileURL)(sorted);
+
                 dispatch(
                     treePickerActions.APPEND_TREE_PICKER_NODE_SUBTREE({
                         id,
@@ -153,7 +164,7 @@ export const loadCollection = (id: string, pickerId: string) =>
 
 export const initUserProject = (pickerId: string) =>
     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
-        const uuid = services.authService.getUuid();
+        const uuid = getUserUuid(getState());
         if (uuid) {
             dispatch(receiveTreePickerData({
                 id: '',
@@ -169,19 +180,19 @@ export const initUserProject = (pickerId: string) =>
     };
 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
-        const uuid = services.authService.getUuid();
+        const uuid = getUserUuid(getState());
         if (uuid) {
             dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
         }
     };
 
-
+export const SHARED_PROJECT_ID = 'Shared with me';
 export const initSharedProject = (pickerId: string) =>
     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         dispatch(receiveTreePickerData({
             id: '',
             pickerId,
-            data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
+            data: [{ uuid: SHARED_PROJECT_ID, name: SHARED_PROJECT_ID }],
             extractNodeData: value => ({
                 id: value.uuid,
                 status: TreeNodeStatus.INITIAL,
@@ -190,12 +201,28 @@ export const initSharedProject = (pickerId: string) =>
         }));
     };
 
+export const FAVORITES_PROJECT_ID = 'Favorites';
 export const initFavoritesProject = (pickerId: string) =>
     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         dispatch(receiveTreePickerData({
             id: '',
             pickerId,
-            data: [{ uuid: 'Favorites', name: 'Favorites' }],
+            data: [{ uuid: FAVORITES_PROJECT_ID, name: FAVORITES_PROJECT_ID }],
+            extractNodeData: value => ({
+                id: value.uuid,
+                status: TreeNodeStatus.INITIAL,
+                value,
+            }),
+        }));
+    };
+
+export const PUBLIC_FAVORITES_PROJECT_ID = 'Public Favorites';
+export const initPublicFavoritesProject = (pickerId: string) =>
+    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+        dispatch(receiveTreePickerData({
+            id: '',
+            pickerId,
+            data: [{ uuid: PUBLIC_FAVORITES_PROJECT_ID, name: PUBLIC_FAVORITES_PROJECT_ID }],
             extractNodeData: value => ({
                 id: value.uuid,
                 status: TreeNodeStatus.INITIAL,
@@ -210,25 +237,31 @@ interface LoadFavoritesProjectParams {
     includeFiles?: boolean;
 }
 
-export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
+export const loadFavoritesProject = (params: LoadFavoritesProjectParams,
+    options: { showOnlyOwned: boolean, showOnlyWritable: boolean } = { showOnlyOwned: true, showOnlyWritable: false }) =>
     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         const { pickerId, includeCollections = false, includeFiles = false } = params;
-        const uuid = services.authService.getUuid();
+        const uuid = getUserUuid(getState());
         if (uuid) {
-
             const filters = pipe(
                 (fb: FilterBuilder) => includeCollections
-                    ? fb.addIsA('headUuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
-                    : fb.addIsA('headUuid', [ResourceKind.PROJECT]),
+                    ? fb.addIsA('head_uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
+                    : fb.addIsA('head_uuid', [ResourceKind.PROJECT]),
                 fb => fb.getFilters(),
             )(new FilterBuilder());
 
-            const { items } = await services.favoriteService.list(uuid, { filters });
+            const { items } = await services.favoriteService.list(uuid, { filters }, options.showOnlyOwned);
 
             dispatch<any>(receiveTreePickerData<GroupContentsResource>({
                 id: 'Favorites',
                 pickerId,
-                data: items,
+                data: items.filter((item) => {
+                    if (options.showOnlyWritable && (item as GroupResource).writableBy && (item as GroupResource).writableBy.indexOf(uuid) === -1) {
+                        return false;
+                    }
+
+                    return true;
+                }),
                 extractNodeData: item => ({
                     id: item.uuid,
                     value: item,
@@ -242,6 +275,40 @@ export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
         }
     };
 
+export const loadPublicFavoritesProject = (params: LoadFavoritesProjectParams) =>
+    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+        const { pickerId, includeCollections = false, includeFiles = false } = params;
+        const uuidPrefix = getState().auth.config.uuidPrefix;
+        const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
+
+        const filters = pipe(
+            (fb: FilterBuilder) => includeCollections
+                ? fb.addIsA('head_uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
+                : fb.addIsA('head_uuid', [ResourceKind.PROJECT]),
+            fb => fb
+                .addEqual('link_class', LinkClass.STAR)
+                .addEqual('owner_uuid', publicProjectUuid)
+                .getFilters(),
+        )(new FilterBuilder());
+
+        const { items } = await services.linkService.list({ filters });
+
+        dispatch<any>(receiveTreePickerData<LinkResource>({
+            id: 'Public Favorites',
+            pickerId,
+            data: items,
+            extractNodeData: item => ({
+                id: item.headUuid,
+                value: item,
+                status: item.headKind === ResourceKind.PROJECT
+                    ? TreeNodeStatus.INITIAL
+                    : includeFiles
+                        ? TreeNodeStatus.INITIAL
+                        : TreeNodeStatus.LOADED
+            }),
+        }));
+    };
+
 export const receiveTreePickerProjectsData = (id: string, projects: ProjectResource[], pickerId: string) =>
     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
@@ -257,7 +324,8 @@ export const loadProjectTreePickerProjects = (id: string) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PROJECTS }));
 
-        const ownerUuid = id.length === 0 ? services.authService.getUuid() || '' : id;
+
+        const ownerUuid = id.length === 0 ? getUserUuid(getState()) || '' : id;
         const { items } = await services.projectService.list(buildParams(ownerUuid));
 
         dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PROJECTS));
@@ -265,7 +333,7 @@ export const loadProjectTreePickerProjects = (id: string) =>
 
 export const loadFavoriteTreePickerProjects = (id: string) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        const parentId = services.authService.getUuid() || '';
+        const parentId = getUserUuid(getState()) || '';
 
         if (id === '') {
             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.FAVORITES }));
@@ -279,13 +347,29 @@ export const loadFavoriteTreePickerProjects = (id: string) =>
 
     };
 
+export const loadPublicFavoriteTreePickerProjects = (id: string) =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const parentId = getUserUuid(getState()) || '';
+
+        if (id === '') {
+            dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.PUBLIC_FAVORITES }));
+            const { items } = await services.favoriteService.list(parentId);
+            dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.PUBLIC_FAVORITES));
+        } else {
+            dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PUBLIC_FAVORITES }));
+            const { items } = await services.projectService.list(buildParams(id));
+            dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PUBLIC_FAVORITES));
+        }
+
+    };
+
 const buildParams = (ownerUuid: string) => {
     return {
         filters: new FilterBuilder()
-            .addEqual('ownerUuid', ownerUuid)
+            .addEqual('owner_uuid', ownerUuid)
             .getFilters(),
         order: new OrderBuilder<ProjectResource>()
             .addAsc('name')
             .getOrder()
     };
-};
\ No newline at end of file
+};