Merge "YANG Model update for A1 Adapter"
[ccsdk/features.git] / sdnr / wt / 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 import { SetTitleAction } from '../actions/titleActions';
20
21 import { AddSnackbarNotification, RemoveSnackbarNotification } from '../actions/snackbarActions';
22 import { AddErrorInfoAction, RemoveErrorInfoAction, ClearErrorInfoAction } from '../actions/errorActions';
23 import { MenuAction, MenuClosedByUser } from '../actions/menuAction'
24 import { IconType } from '../models/iconDefinition';
25
26 import { ErrorInfo } from '../models/errorInfo';
27 import { SnackbarItem } from '../models/snackbarItem';
28 import { SetWebsocketAction } from '../actions/websocketAction';
29
30 export interface IApplicationState {
31   title: string;
32   appId?: string;
33   icon?: IconType;
34   isMenuOpen: boolean;
35   isMenuClosedByUser: boolean;
36   errors: ErrorInfo[];
37   snackBars: SnackbarItem[];
38   isWebsocketAvailable: boolean | undefined;
39 }
40
41 const applicationStateInit: IApplicationState = { title: "Loading ...", errors: [], snackBars: [], isMenuOpen: true, isMenuClosedByUser: false, isWebsocketAvailable: undefined };
42
43 export const applicationStateHandler: IActionHandler<IApplicationState> = (state = applicationStateInit, action) => {
44   if (action instanceof SetTitleAction) {
45     state = {
46       ...state,
47       title: action.title,
48       icon: action.icon,
49       appId: action.appId
50     };
51   } else if (action instanceof AddErrorInfoAction) {
52     state = {
53       ...state,
54       errors: [
55         ...state.errors,
56         action.errorInfo
57       ]
58     };
59   } else if (action instanceof RemoveErrorInfoAction) {
60     const index = state.errors.indexOf(action.errorInfo);
61     if (index > -1) {
62       state = {
63         ...state,
64         errors: [
65           ...state.errors.slice(0, index),
66           ...state.errors.slice(index + 1)
67         ]
68       };
69     }
70   } else if (action instanceof ClearErrorInfoAction) {
71     if (state.errors && state.errors.length) {
72       state = {
73         ...state,
74         errors: []
75       };
76     }
77   } else if (action instanceof AddSnackbarNotification) {
78     state = {
79       ...state,
80       snackBars: [
81         ...state.snackBars,
82         action.notification
83       ]
84     };
85   } else if (action instanceof RemoveSnackbarNotification) {
86     state = {
87       ...state,
88       snackBars: state.snackBars.filter(s => s.key !== action.key)
89     };
90   } else if (action instanceof MenuAction) {
91     state = {
92       ...state,
93       isMenuOpen: action.isOpen
94     }
95   } else if (action instanceof MenuClosedByUser) {
96     state = {
97       ...state,
98       isMenuClosedByUser: action.isClosed
99     }
100   }
101   else if (action instanceof SetWebsocketAction) {
102     state = {
103       ...state,
104       isWebsocketAvailable: action.isConnected
105     }
106   }
107   return state;
108 };