Create tree picker with support for collections and files, Create User content tree...
[arvados.git] / src / store / tree-picker / tree-picker-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { createTree, TreeNode, setNode, Tree, TreeNodeStatus, setNodeStatus, expandNode } from '~/models/tree';
6 import { TreePicker } from "./tree-picker";
7 import { treePickerActions, TreePickerAction } from "./tree-picker-actions";
8 import { compose } from "redux";
9 import { activateNode, getNode, toggleNodeCollapse, toggleNodeSelection } from '~/models/tree';
10
11 export const treePickerReducer = (state: TreePicker = {}, action: TreePickerAction) =>
12     treePickerActions.match(action, {
13         LOAD_TREE_PICKER_NODE: ({ id, pickerId }) =>
14             updateOrCreatePicker(state, pickerId, setNodeStatus(id)(TreeNodeStatus.PENDING)),
15         LOAD_TREE_PICKER_NODE_SUCCESS: ({ id, nodes, pickerId }) =>
16             updateOrCreatePicker(state, pickerId, compose(receiveNodes(nodes)(id), setNodeStatus(id)(TreeNodeStatus.LOADED))),
17         TOGGLE_TREE_PICKER_NODE_COLLAPSE: ({ id, pickerId }) =>
18             updateOrCreatePicker(state, pickerId, toggleNodeCollapse(id)),
19         ACTIVATE_TREE_PICKER_NODE: ({ id, pickerId }) =>
20             updateOrCreatePicker(state, pickerId, activateNode(id)),
21         TOGGLE_TREE_PICKER_NODE_SELECTION: ({ id, pickerId }) =>
22             updateOrCreatePicker(state, pickerId, toggleNodeSelection(id)),
23         RESET_TREE_PICKER: ({ pickerId }) =>
24             updateOrCreatePicker(state, pickerId, createTree),
25         EXPAND_TREE_PICKER_NODES: ({ pickerId, ids }) =>
26             updateOrCreatePicker(state, pickerId, expandNode(...ids)),
27         default: () => state
28     });
29
30 const updateOrCreatePicker = <V>(state: TreePicker, pickerId: string, func: (value: Tree<V>) => Tree<V>) => {
31     const picker = state[pickerId] || createTree();
32     const updatedPicker = func(picker);
33     return { ...state, [pickerId]: updatedPicker };
34 };
35
36 const receiveNodes = <V>(nodes: Array<TreeNode<V>>) => (parent: string) => (state: Tree<V>) => {
37     const parentNode = getNode(parent)(state);
38     let newState = state;
39     if (parentNode) {
40         newState = setNode({ ...parentNode, children: [] })(state);
41     }
42     return nodes.reduce((tree, node) => {
43         return setNode({ ...node, parent })(tree);
44     }, newState);
45 };