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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
19 import { IActionHandler } from '../../../../framework/src/flux/action';
21 import { SetBusyAction, SetSearchTextAction, UpdateExpandedNodesAction, UpdateInventoryTreeAction, UpdateSelectedNodeAction } from '../actions/inventoryTreeActions';
22 import { InventoryTreeNode, InventoryType, TreeDemoItem } from '../models/inventory';
25 export interface IInvenroryTree {
27 rootNodes: TreeDemoItem[];
28 selectedNode?: InventoryType;
29 expandedItems: TreeDemoItem[];
33 const initialState: IInvenroryTree = {
37 selectedNode: undefined,
42 const getTreeDataFromInvetoryTreeNode = (node: InventoryTreeNode): TreeDemoItem[] => Object.keys(node).reduce<TreeDemoItem[]>((acc, key) => {
43 const cur = node[key];
46 content: cur.label || key,
48 children: cur.children && getTreeDataFromInvetoryTreeNode(cur.children),
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 || [] };