13902-ui-move-to-popup
[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 { default as unionize, ofType, UnionOf } from "unionize";
6
7 import { TreePickerNode } from "./tree-picker";
8 import { receiveTreePickerData, TreePickerKind } from "../../views-components/project-tree-picker/project-tree-picker";
9 import { mockProjectResource } from "../../models/test-utils";
10 import { Dispatch } from "redux";
11 import { RootState } from "../store";
12 import { ServiceRepository } from "../../services/services";
13
14 export const treePickerActions = unionize({
15     LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
16     LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array<TreePickerNode>, pickerId: string }>(),
17     TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
18     TOGGLE_TREE_PICKER_NODE_SELECT: ofType<{ id: string, pickerId: string }>()
19 }, {
20         tag: 'type',
21         value: 'payload'
22     });
23
24 export const initPickerProjectTree = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
25     const uuid = services.authService.getUuid();
26
27     dispatch<any>(getPickerTreeProjects(uuid));
28     dispatch<any>(getSharedWithMeProjectsPickerTree(uuid));
29     dispatch<any>(getFavoritesProjectsPickerTree(uuid));
30 };
31
32 const getPickerTreeProjects = (uuid: string = '') => {
33     return getProjectsPickerTree(uuid, TreePickerKind.PROJECTS);
34 };
35
36 const getSharedWithMeProjectsPickerTree = (uuid: string = '') => {
37     return getProjectsPickerTree(uuid, TreePickerKind.SHARED_WITH_ME);
38 };
39
40 const getFavoritesProjectsPickerTree = (uuid: string = '') => {
41     return getProjectsPickerTree(uuid, TreePickerKind.FAVORITES);
42 };
43
44 const getProjectsPickerTree = (uuid: string, kind: string) => {
45     return receiveTreePickerData(
46         '',
47         [mockProjectResource({ uuid, name: kind })],
48         kind
49     );
50 };
51
52 export type TreePickerAction = UnionOf<typeof treePickerActions>;