Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / framework / src / handlers / applicationStateHandler.ts
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 import { IActionHandler } from '../flux/action';
19
20 import { SetTitleAction } from '../actions/titleActions';
21 import { SetExternalLoginProviderAction } from '../actions/loginProvider';
22 import { AddSnackbarNotification, RemoveSnackbarNotification } from '../actions/snackbarActions';
23 import { AddErrorInfoAction, RemoveErrorInfoAction, ClearErrorInfoAction } from '../actions/errorActions';
24 import { MenuAction, MenuClosedByUser } from '../actions/menuAction'
25 import { SetWebsocketAction } from '../actions/websocketAction';
26
27 import { IconType } from '../models/iconDefinition';
28 import { ErrorInfo } from '../models/errorInfo';
29 import { SnackbarItem } from '../models/snackbarItem';
30 import { ExternalLoginProvider } from '../models/externalLoginProvider';
31 import { ApplicationConfig } from '../models/applicationConfig';
32 import { IConnectAppStoreState } from '../../../apps/connectApp/src/handlers/connectAppRootHandler';
33 import { IFaultAppStoreState } from '../../../apps/faultApp/src/handlers/faultAppRootHandler';
34 import { GeneralSettings, Settings } from '../models/settings';
35 import { LoadSettingsAction, SetGeneralSettingsAction, SetTableSettings, SettingsDoneLoadingAction } from '../actions/settingsAction';
36 import { startWebsocketSession, suspendWebsocketSession } from '../services/notificationService';
37
38
39 declare module '../store/applicationStore' {
40   interface IApplicationStoreState {
41     connect: IConnectAppStoreState;
42     fault: IFaultAppStoreState
43   }
44 }
45 export interface IApplicationState {
46   title: string;
47   appId?: string;
48   icon?: IconType;
49   isMenuOpen: boolean;
50   isMenuClosedByUser: boolean;
51   errors: ErrorInfo[];
52   snackBars: SnackbarItem[];
53   isWebsocketAvailable: boolean | null;
54   externalLoginProviders: ExternalLoginProvider[] | null;
55   authentication: "basic"|"oauth",  // basic 
56   enablePolicy: boolean,             // false 
57   transportpceUrl : string,
58   settings: Settings & { isInitialLoadDone: boolean }
59 }
60
61 const applicationStateInit: IApplicationState = {
62   title: "Loading ...",
63   errors: [],
64   snackBars: [],
65   isMenuOpen: true,
66   isMenuClosedByUser: false,
67   isWebsocketAvailable: null,
68   externalLoginProviders: null,
69   authentication: "basic",
70   enablePolicy: false,
71   transportpceUrl: "",
72   settings:{ 
73     general: { areNotificationsEnabled: null },
74     tables: {},
75     isInitialLoadDone: false
76   }
77 };
78
79 export const configureApplication = (config: ApplicationConfig) => {
80   applicationStateInit.authentication = config.authentication === "oauth" ? "oauth" : "basic";
81   applicationStateInit.enablePolicy = config.enablePolicy ? true : false;
82   applicationStateInit.transportpceUrl=config.transportpceUrl == undefined ? "" : config.transportpceUrl;
83 }
84
85 export const applicationStateHandler: IActionHandler<IApplicationState> = (state = applicationStateInit, action) => {
86   if (action instanceof SetTitleAction) {
87     state = {
88       ...state,
89       title: action.title,
90       icon: action.icon,
91       appId: action.appId,
92     };
93   } else if (action instanceof AddErrorInfoAction) {
94     state = {
95       ...state,
96       errors: [
97         ...state.errors,
98         action.errorInfo,
99       ]
100     };
101   } else if (action instanceof RemoveErrorInfoAction) {
102     const index = state.errors.indexOf(action.errorInfo);
103     if (index > -1) {
104       state = {
105         ...state,
106         errors: [
107           ...state.errors.slice(0, index),
108           ...state.errors.slice(index + 1),
109         ]
110       };
111     }
112   } else if (action instanceof ClearErrorInfoAction) {
113     if (state.errors && state.errors.length) {
114       state = {
115         ...state,
116         errors: [],
117       };
118     }
119   } else if (action instanceof AddSnackbarNotification) {
120     state = {
121       ...state,
122       snackBars: [
123         ...state.snackBars,
124         action.notification,
125       ]
126     };
127   } else if (action instanceof RemoveSnackbarNotification) {
128     state = {
129       ...state,
130       snackBars: state.snackBars.filter(s => s.key !== action.key),
131     };
132   } else if (action instanceof MenuAction) {
133     state = {
134       ...state,
135       isMenuOpen: action.isOpen,
136     }
137   } else if (action instanceof MenuClosedByUser) {
138     state = {
139       ...state,
140       isMenuClosedByUser: action.isClosed,
141     }
142   }
143   else if (action instanceof SetWebsocketAction) {
144     state = {
145       ...state,
146       isWebsocketAvailable: action.isConnected,
147     }
148   } else if (action instanceof SetExternalLoginProviderAction){
149     state = {
150       ...state,
151       externalLoginProviders: action.externalLoginProvders,
152     }
153   }else if(action instanceof SetGeneralSettingsAction){
154
155     state = {
156       ...state,
157       settings:{tables: state.settings.tables, isInitialLoadDone:state.settings.isInitialLoadDone, general:{areNotificationsEnabled: action.areNoticationsActive}}
158     }
159   }
160   else if(action instanceof SetTableSettings){
161
162     let tableUpdate = state.settings.tables;
163     tableUpdate[action.tableName] = {columns: action.updatedColumns};
164
165     state = {...state, settings:{general: state.settings.general, isInitialLoadDone:state.settings.isInitialLoadDone, tables: tableUpdate}}
166     
167   }else if(action instanceof LoadSettingsAction){
168     
169     state = {...state, settings:action.settings}
170   }
171   else if(action instanceof SettingsDoneLoadingAction){
172     state= {...state, settings: {...state.settings, isInitialLoadDone: true}}
173   }
174
175   return state;
176 };