Add data-provider
[ccsdk/features.git] / sdnr / wt / odlux / apps / connectApp / src / handlers / mountedNetworkElementsHandler.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 import { IActionHandler } from '../../../../framework/src/flux/action';
19
20 import {
21   AddOrUpdateMountedNetworkElement,
22   AllMountedNetworkElementsLoadedAction,
23   LoadAllMountedNetworkElementsAction,
24   RemoveMountedNetworkElement,
25   UpdateConnectionStateMountedNetworkElement,
26   UpdateRequiredMountedNetworkElement
27 } from '../actions/mountedNetworkElementsActions';
28
29 import { MountedNetworkElementType } from '../models/mountedNetworkElements';
30
31 export interface IMountedNetworkElementsState {
32   elements: MountedNetworkElementType[];
33   busy: boolean;
34 }
35
36 const mountedNetworkElementsStateInit: IMountedNetworkElementsState = {
37   elements: [],
38   busy: false
39 };
40
41 export const mountedNetworkElementsActionHandler: IActionHandler<IMountedNetworkElementsState> = (state = mountedNetworkElementsStateInit, action) => {
42   if (action instanceof LoadAllMountedNetworkElementsAction) {
43
44     state = {
45       ...state,
46       busy: true
47     };
48
49   } else if (action instanceof AllMountedNetworkElementsLoadedAction) {
50     if (!action.error && action.mountedNetworkElements) {
51       state = {
52         ...state,
53         elements: action.mountedNetworkElements,
54         busy: false
55       };
56     } else {
57       state = {
58         ...state,
59         busy: false
60       };
61     }
62   } else if (action instanceof AddOrUpdateMountedNetworkElement) {
63     if (!action.mountedNetworkElement) return state; // should handle error here
64     const index = state.elements.findIndex(el => el.mountId === (action.mountedNetworkElement && action.mountedNetworkElement.mountId));
65     if (index > -1) {
66       state = {
67         ...state,
68         elements: [
69           ...state.elements.slice(0, index),
70           action.mountedNetworkElement,
71           ...state.elements.slice(index + 1)
72         ]
73       }
74     } else {
75       state = {
76         ...state,
77         elements: [...state.elements, action.mountedNetworkElement],
78       }
79     };
80   } else if (action instanceof RemoveMountedNetworkElement) {
81     state = {
82       ...state,
83       elements: state.elements.filter(e => e.mountId !== action.mountId),
84     };
85   } else if (action instanceof UpdateConnectionStateMountedNetworkElement) {
86     const index = state.elements.findIndex(el => el.mountId === action.mountId);
87     if (index > -1) {
88       state = {
89         ...state,
90         elements: [
91           ...state.elements.slice(0, index),
92           { ...state.elements[index], connectionStatus: action.mountId },
93           ...state.elements.slice(index + 1)
94         ]
95       }
96     }
97   } else if (action instanceof UpdateRequiredMountedNetworkElement) {
98     const index = state.elements.findIndex(el => el.mountId === action.mountId);
99     if (index > -1 && (state.elements[index].required !== action.required)) {
100       state = {
101         ...state,
102         elements: [
103           ...state.elements.slice(0, index),
104           { ...state.elements[index], required: action.required },
105           ...state.elements.slice(index + 1)
106         ]
107       }
108     }
109   };
110   return state;
111 };