Odlux Update
[ccsdk/features.git] / sdnr / wt / odlux / apps / inventoryApp / src / handlers / inventoryTreeHandler.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 import { IActionHandler } from '../../../../framework/src/flux/action';
20
21 import { SetBusyAction, SetSearchTextAction, UpdateExpandedNodesAction, UpdateInventoryTreeAction, UpdateSelectedNodeAction } from '../actions/inventoryTreeActions';
22 import { InventoryTreeNode, InventoryType, TreeDemoItem } from '../models/inventory';
23
24
25 export interface IInvenroryTree {
26   isBusy: boolean;
27   rootNodes: TreeDemoItem[];
28   selectedNode?: InventoryType;
29   expandedItems: TreeDemoItem[];
30   searchTerm: string;
31 }
32
33 const initialState: IInvenroryTree = {
34   isBusy: false,
35   rootNodes: [],
36   searchTerm: '',
37   selectedNode: undefined,
38   expandedItems: [],
39 };
40
41
42 const getTreeDataFromInvetoryTreeNode = (node: InventoryTreeNode): TreeDemoItem[] => Object.keys(node).reduce<TreeDemoItem[]>((acc, key) => {
43   const cur = node[key];
44   acc.push({
45     isMatch: cur.isMatch,
46     content: cur.label || key,
47     value: key,
48     children: cur.children && getTreeDataFromInvetoryTreeNode(cur.children),
49   });
50   return acc;
51 }, []);
52
53 export const inventoryTreeHandler: IActionHandler<IInvenroryTree> = (state = initialState, action) => {
54   if (action instanceof SetBusyAction) {
55     state = { ...state, isBusy: action.busy };
56   } else if (action instanceof SetSearchTextAction) {
57     state = { ...state, searchTerm: action.searchTerm };
58   } else if (action instanceof UpdateInventoryTreeAction) {
59     const rootNodes = getTreeDataFromInvetoryTreeNode(action.rootNode);
60     state = { ...state, rootNodes: rootNodes, expandedItems: [], selectedNode: undefined };
61   } else if (action instanceof UpdateSelectedNodeAction) {
62     state = { ...state, selectedNode: action.selectedNode };
63   } else if (action instanceof UpdateExpandedNodesAction) {
64     state = { ...state, expandedItems: action.expandedNodes || [] };
65   }
66
67   return state;
68 };