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
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 { 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';
26 channel: BroadcastChannel;
30 type AuthTypes = 'login' | 'logout';
31 export type AuthMessage = {
36 type SettingsType = 'general';
37 export type SettingsMessage = {
39 enableNotifications: boolean;
43 const channels: Broadcaster[] = [];
44 let store: ApplicationStore | null = null;
46 export const saveChannel = (channel: BroadcastChannel, channelName: string) => {
47 channels.push({ channel: channel, key: channelName });
50 const createSettingsBroadcastChannel = () => {
52 const name = 'odlux_settings';
53 const bc: BroadcastChannel = new BroadcastChannel(name);
54 channels.push({ channel: bc, key: name });
56 bc.onmessage = (eventMessage: MessageEvent<SettingsMessage>) => {
57 console.log(eventMessage);
59 if (eventMessage.data.key === 'general') {
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));
71 const createAuthBroadcastChannel = () => {
72 const name = 'odlux_auth';
73 const bc: BroadcastChannel = new BroadcastChannel(name);
74 channels.push({ channel: bc, key: name });
76 bc.onmessage = (eventMessage: MessageEvent<AuthMessage>) => {
77 console.log(eventMessage);
79 if (eventMessage.data.key === 'login') {
80 if (!store?.state.framework.authenticationState.user) {
81 const initialToken = localStorage.getItem('userToken');
83 store?.dispatch(loginUserAction(User.fromString(initialToken)));
84 store?.dispatch(new ReplaceAction('/'));
87 } else if (eventMessage.data.key === 'logout') {
89 if (store?.state.framework.authenticationState.user) {
90 store?.dispatch(logoutUser());
91 store?.dispatch(new ReplaceAction('/login'));
97 export const startBroadcastChannel = (applicationStore: ApplicationStore) => {
98 store = applicationStore;
100 // might decide to use one general broadcast channel with more keys in the future
101 createAuthBroadcastChannel();
102 createSettingsBroadcastChannel();
105 export const getBroadcastChannel = (channelName: string) => {
106 const foundChannel = channels.find(s => s.key === channelName);
107 return foundChannel?.channel;
110 export const sendMessage = (data: any, channel: string) => {
111 const foundChannel = channels.find(s => s.key === channel);
113 foundChannel.channel.postMessage(data);