Merge "Web Client context menu item display"
[ccsdk/features.git] / sdnr / wt / odlux / apps / mediatorApp / src / plugin.tsx
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 // app configuration and main entry point for the app
19
20 import React from "react";
21 import { withRouter, RouteComponentProps, Route, Switch, Redirect } from 'react-router-dom';
22
23 import applicationManager from '../../../framework/src/services/applicationManager';
24
25 import { connect, Connect, IDispatcher } from '../../../framework/src/flux/connect';
26
27 import { mediatorAppRootHandler } from './handlers/mediatorAppRootHandler';
28 import { avaliableMediatorServersReloadAction } from "./handlers/avaliableMediatorServersHandler";
29
30 import { MediatorApplication } from "./views/mediatorApplication";
31 import { MediatorServerSelection } from "./views/mediatorServerSelection";
32 import { initializeMediatorServerAsyncActionCreator } from "./actions/mediatorServerActions";
33
34 const appIcon = require('./assets/icons/mediatorAppIcon.svg');  // select app icon
35
36 let currentMediatorServerId: string | undefined = undefined;
37
38 const mapDisp = (dispatcher: IDispatcher) => ({
39   loadMediatorServer : (mediatorServerId: string) => dispatcher.dispatch(initializeMediatorServerAsyncActionCreator(mediatorServerId)),
40 });
41
42 const MediatorServerRouteAdapter = connect(undefined, mapDisp)((props: RouteComponentProps<{ mediatorServerId: string }> & Connect<undefined, typeof mapDisp>) => {
43   if (currentMediatorServerId !== props.match.params.mediatorServerId) {
44     // route parameter has changed
45     currentMediatorServerId = props.match.params.mediatorServerId || undefined;
46     // Hint: This timeout is need, since it is not recommended to change the state while rendering is in progress !
47     window.setTimeout(() => {
48       if (currentMediatorServerId) {
49         props.loadMediatorServer(currentMediatorServerId);
50       }
51     });
52   }
53   return (
54     <MediatorApplication />
55   )
56 });
57
58 type AppProps = RouteComponentProps & Connect;
59
60 const App = (props: AppProps) => (
61   <Switch>
62     <Route exact path={ `${ props.match.path }` } component={ MediatorServerSelection } />
63     <Route path={ `${ props.match.path }/:mediatorServerId` } component={ MediatorServerRouteAdapter } />
64     <Redirect to={ `${ props.match.path }` } />
65    </Switch>
66 );
67
68 const FinalApp = withRouter(connect()(App));
69
70 export function register() {
71   const applicationApi = applicationManager.registerApplication({
72     name: "mediator",
73     icon: appIcon,
74     rootComponent: FinalApp,
75     rootActionHandler: mediatorAppRootHandler,
76     menuEntry: "Mediator"
77   });
78
79   // prefetch all available mediator servers
80   applicationApi.applicationStoreInitialized.then(applicationStore => {
81     applicationStore.dispatch(avaliableMediatorServersReloadAction)
82   });
83 };