aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/apps/inventoryApp/src/views/treeview.tsx
blob: b0e962daa1d02c44c583df88b5d3fba052d0029e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt odlux
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 */
import * as React from "react";
import { Theme } from '@mui/material/styles';

import { WithStyles } from '@mui/styles';
import withStyles from '@mui/styles/withStyles';
import createStyles from '@mui/styles/createStyles';

import { renderObject } from '../../../../framework/src/components/objectDump';
import { Connect, connect, IDispatcher } from '../../../../framework/src/flux/connect';
import { TreeView, TreeViewCtorType, SearchMode } from '../../../../framework/src/components/material-ui/treeView';

import { IApplicationStoreState } from "../../../../framework/src/store/applicationStore";

import Breadcrumbs from '@mui/material/Breadcrumbs';
import Link from '@mui/material/Link';

import { updateInventoryTreeAsyncAction, selectInventoryNodeAsyncAction, UpdateSelectedNodeAction, UpdateExpandedNodesAction, setSearchTermAction } from "../actions/inventoryTreeActions";
import { TreeDemoItem } from "../models/inventory";

import { RouteComponentProps } from 'react-router-dom';

const styles = (theme: Theme) => createStyles({
  root: {
    flex: "1 0 0%",
    display: "flex",
    flexDirection: "row",
  },
  tree: {
    flex: "1 0 0%",
    minWidth: "250px",
    padding: `0px ${theme.spacing(1)}`
  },
  details: {
    flex: "5 0 0%",
    padding: `0px ${theme.spacing(1)}`
  }
});

const mapProps = (state: IApplicationStoreState) => ({
  isBusy: state.inventory.inventoryTree.isBusy,
  rootNodes: state.inventory.inventoryTree.rootNodes,
  searchTerm: state.inventory.inventoryTree.searchTerm,
  selectedNode: state.inventory.inventoryTree.selectedNode,
  expendedItems: state.inventory.inventoryTree.expandedItems,
});

const mapDispatch = (dispatcher: IDispatcher) => ({
  updateExpendedNodes: (expendedNodes: TreeDemoItem[]) => dispatcher.dispatch(new UpdateExpandedNodesAction(expendedNodes)),
  updateInventoryTree: (mountId: string, searchTerm?: string) => dispatcher.dispatch(updateInventoryTreeAsyncAction(mountId, searchTerm)),
  selectTreeNode: (nodeId?: string) => nodeId ? dispatcher.dispatch(selectInventoryNodeAsyncAction(nodeId)) : dispatcher.dispatch(new UpdateSelectedNodeAction(undefined)),
  setSearchTerm: (searchTerm: string) => dispatcher.dispatch(setSearchTermAction(searchTerm)),
});

const propsChache = Symbol("PropsCache");
const InventoryTree = TreeView as any as TreeViewCtorType<string>;



type TreeviewComponentProps = RouteComponentProps<{ mountId: string }> & WithStyles<typeof styles> & Connect<typeof mapProps, typeof mapDispatch>

type TreeviewComponentState = {
  [propsChache]: {
    rootNodes?: TreeDemoItem[];
  };
  rootNodes: TreeDemoItem[];
}


class DashboardComponent extends React.Component<TreeviewComponentProps, TreeviewComponentState> {

  constructor(props: TreeviewComponentProps) {
    super(props);

    this.state = {
      [propsChache]: {},
      rootNodes: [],
    };
  }

  static getDerivedStateFromProps(props: TreeviewComponentProps, state: TreeviewComponentState) {
    if (state[propsChache].rootNodes != props.rootNodes) {
      state = { ...state, rootNodes: props.rootNodes }
    }
    return state;
  }

  render() {
    const { classes, updateInventoryTree, updateExpendedNodes, expendedItems, selectedNode, selectTreeNode, searchTerm, match: { params: { mountId } } } = this.props;
    const scrollbar = { overflow: "auto", paddingRight: "20px" }

    let filteredDashboardPath = `/inventory/dashboard/${this.props.match.params.mountId}`;
    let basePath = `/inventory/${this.props.match.params.mountId}`;

    return (
      <div style={scrollbar} >
        <div >
          <Breadcrumbs aria-label="breadcrumbs">
            <Link underline="hover" color="inherit" href="#" aria-label="back-breadcrumb"
              onClick={(event: React.MouseEvent<HTMLElement>) => {
                event.preventDefault();
                this.props.history.push(filteredDashboardPath);
              }}>Back</Link>
            <Link underline="hover" color="inherit" href="#"
              aria-label={this.props.match.params.mountId + '-breadcrumb'}
              onClick={(event: React.MouseEvent<HTMLElement>) => {
                event.preventDefault();
                this.props.history.push(basePath);
              }}><span>{this.props.match.params.mountId}</span></Link>
          </Breadcrumbs>
        </div>
        <br />
        <div style={scrollbar} className={classes.root}>
          <InventoryTree className={classes.tree} items={this.state.rootNodes} enableSearchBar initialSearchTerm={searchTerm} searchMode={SearchMode.OnEnter} searchTerm={searchTerm}
            onSearch={(searchTerm) => updateInventoryTree(mountId, searchTerm)} expandedItems={expendedItems} onFolderClick={(item) => {
              const indexOfItemToToggle = expendedItems.indexOf(item);
              if (indexOfItemToToggle === -1) {
                updateExpendedNodes([...expendedItems, item]);
              } else {
                updateExpendedNodes([
                  ...expendedItems.slice(0, indexOfItemToToggle),
                  ...expendedItems.slice(indexOfItemToToggle + 1),
                ]);
              }
            }}
            onItemClick={(elm) => selectTreeNode(elm.value)} />
          <div className={classes.details}>{
            selectedNode && renderObject(selectedNode, "tree-view") || null
          }</div>
        </div>
      </div>
    );
  }

  componentDidMount() {
    const { updateInventoryTree, searchTerm, match: { params: { mountId } } } = this.props;
    updateInventoryTree(mountId, searchTerm);
  }

  componentWillUnmount() {
    this.props.setSearchTerm("*");
  }
}

export const InventoryTreeView = connect(mapProps, mapDispatch)(withStyles(styles)(DashboardComponent));
export default InventoryTreeView;