Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / framework / src / services / broadcastService.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2021 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 { setGeneralSettingsAction } from '../actions/settingsAction';
20 import { loginUserAction, logoutUser } from '../actions/authentication';
21 import { ReplaceAction } from '../actions/navigationActions';
22 import { User } from '../models/authentication';
23 import { ApplicationStore } from '../store/applicationStore';
24
25 type Broadcaster = {
26   channel: BroadcastChannel;
27   key: String;
28 };
29
30 type AuthTypes = 'login' | 'logout';
31 export type AuthMessage = {
32   key: AuthTypes;
33   data: any;
34 };
35
36 type SettingsType = 'general';
37 export type SettingsMessage = {
38   key: SettingsType;
39   enableNotifications: boolean;
40   user: string;
41 };
42
43 const channels: Broadcaster[] = [];
44 let store: ApplicationStore | null = null;
45
46 export const saveChannel = (channel: BroadcastChannel, channelName: string) => {
47   channels.push({ channel: channel, key: channelName });
48 };
49
50 const createSettingsBroadcastChannel = () => {
51
52   const name = 'odlux_settings';
53   const bc: BroadcastChannel = new BroadcastChannel(name);
54   channels.push({ channel: bc, key: name });
55
56   bc.onmessage = (eventMessage: MessageEvent<SettingsMessage>) => {
57     console.log(eventMessage);
58
59     if (eventMessage.data.key === 'general') {
60
61       if (store?.state.framework.authenticationState.user) {
62         const data = eventMessage.data;
63         if (store.state.framework.authenticationState.user.user === data.user) {
64           store?.dispatch(setGeneralSettingsAction(data.enableNotifications));
65         }
66       }
67     }
68   };
69 };
70
71 const createAuthBroadcastChannel = () => {
72   const name = 'odlux_auth';
73   const bc: BroadcastChannel = new BroadcastChannel(name);
74   channels.push({ channel: bc, key: name });
75
76   bc.onmessage = (eventMessage: MessageEvent<AuthMessage>) => {
77     console.log(eventMessage);
78
79     if (eventMessage.data.key === 'login') {
80       if (!store?.state.framework.authenticationState.user) {
81         const initialToken = localStorage.getItem('userToken');
82         if (initialToken) {
83           store?.dispatch(loginUserAction(User.fromString(initialToken)));
84           store?.dispatch(new ReplaceAction('/'));
85         }
86       }
87     } else if (eventMessage.data.key === 'logout') {
88
89       if (store?.state.framework.authenticationState.user) {
90         store?.dispatch(logoutUser());
91         store?.dispatch(new ReplaceAction('/login'));
92       }
93     }
94   };
95 };
96
97 export const startBroadcastChannel = (applicationStore: ApplicationStore) => {
98   store = applicationStore;
99
100   // might decide to use one general broadcast channel with more keys in the future
101   createAuthBroadcastChannel();
102   createSettingsBroadcastChannel();
103 };
104
105 export const getBroadcastChannel = (channelName: string) => {
106   const foundChannel = channels.find(s => s.key === channelName);
107   return foundChannel?.channel;
108 };
109
110 export const sendMessage = (data: any, channel: string) => {
111   const foundChannel = channels.find(s => s.key === channel);
112   if (foundChannel) {
113     foundChannel.channel.postMessage(data);
114   }
115 };