Add support for multiple views per extensibility
[aai/sparky-fe.git] / src / app / MainScreenWrapper.jsx
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 import React, {Component} from 'react';
22 import {connect} from 'react-redux';
23 import * as Extensibility from './extensibility/index.js';
24 import TierSupport from './tierSupport/TierSupport.jsx';
25 import VnfSearch from './vnfSearch/VnfSearch.jsx';
26 import MainScreenHeader from './MainScreenHeader.jsx';
27 import {decryptParamsForView, changeUrlAddress} from 'utils/Routes.js';
28 import {isEmpty} from 'lodash';
29
30 import {
31   Route,
32   HashRouter as Router,
33   Switch,
34   Redirect
35 } from 'react-router-dom';
36
37 import {
38   windowResize,
39   extensibleViewNetworkCallback,
40   overlayNetworkCallback,
41   extensibleViewMessageCallback
42 } from './MainScreenWrapperActionHelper.js';
43
44 import extensibleViews from 'resources/views/extensibleViews.json';
45
46 const mapStateToProps = ({mainWrapper}) => {
47   let {
48     showMenu = false,
49     toggleButtonActive = false,
50     extensibleViewNetworkCallbackData = {}
51   } = mainWrapper;
52
53   return {
54     showMenu,
55     toggleButtonActive,
56     extensibleViewNetworkCallbackData
57   };
58 };
59
60 const mapActionsToProps = (dispatch) => {
61   return {
62     onWindowSizeChange: () => dispatch(windowResize()),
63     onExtensibleViewNetworkCallback: (apiUrl,body,viewName,curViewData) =>  {
64       dispatch(extensibleViewNetworkCallback(apiUrl,body,viewName,curViewData));
65     },
66     onExtensibleViewMessageCallback: (message, messageSevirity) => {
67       dispatch(extensibleViewMessageCallback(message, messageSevirity));
68     },
69     onOverlayNetworkCallback: (apiUrl, body, viewName, curViewData, responseEventKey) =>  {
70       dispatch(overlayNetworkCallback(apiUrl, body, viewName, curViewData, responseEventKey));
71     }
72   };
73 };
74
75 class MainScreenWrapper extends Component {
76
77   constructor() {
78     super();
79     window.addEventListener('resize', () => {
80       this.props.onWindowSizeChange();
81     });
82
83   }
84
85
86   render() {
87
88     const {
89       onExtensibleViewNetworkCallback,
90       extensibleViewNetworkCallbackData,
91       onExtensibleViewMessageCallback,
92       onOverlayNetworkCallback
93     } = this.props;
94
95     let customViewList = [];
96     extensibleViews.forEach(function(view,key) {
97
98       let path;
99       if(isEmpty(extensibleViews[key]['routePath'])){
100         path = '/' + view.viewName + '/:extensibleViewParams?';
101       } else {
102         path = '/' + view.viewName  + view.routePath + '/:extensibleViewParams?';
103       }
104       var renderComponent = (props) => {
105         let viewParams = {};
106         if(props.match.params.extensibleViewParams !== undefined) {
107           viewParams = decryptParamsForView(props.match.params.extensibleViewParams);
108         }
109
110         if (Extensibility.default.hasOwnProperty(view.componentName)) {
111           let Component = Extensibility.default[view.componentName];
112           return (
113             <Component
114               {...props}
115               networkingCallback={(apiUrl, body, paramName, curViewData) => {
116                 onExtensibleViewNetworkCallback(apiUrl, body, paramName, curViewData);
117               }}
118               overlayCallback={(apiUrl, body, paramName, curOverlayData,responseEventKey) => {
119                 onOverlayNetworkCallback(apiUrl, body, paramName, curOverlayData, responseEventKey);
120               }}
121               messagingCallback ={(message, messageSeverity) => {
122                 onExtensibleViewMessageCallback(message, messageSeverity);
123               }}
124               changeRouteCallback = {(routeParam, historyObj) => {
125                 changeUrlAddress(routeParam, historyObj);
126               }}
127               viewName={view.displayName}
128               viewData={extensibleViewNetworkCallbackData}
129               viewParams={viewParams}/>
130           );
131         }
132       };
133
134       customViewList.push(
135           <Route key={extensibleViews[key]['viewName'] + 'Route'} path={path}
136              render={renderComponent}/>
137       );
138     });
139
140     return (
141       <Router>
142         <div className='main-app-container'>
143           <Switch>
144             <Redirect from='/' exact to='/schema'/>
145           </Switch>
146           <Route key='MainScreenHeaderRoute' path='/:externalUrl?' component={MainScreenHeader}/>
147           <Route key='TierSupportRoue' path='/schema/:viParam?' component={TierSupport}/>
148           <Route key='VnfSearchRoute' path='/vnfSearch/:filters?' component={VnfSearch}/>
149           {customViewList}
150         </div>
151       </Router>
152     );
153   }
154 }
155
156 export default connect(mapStateToProps, mapActionsToProps)(MainScreenWrapper);