Add networkMap
[ccsdk/features.git] / sdnr / wt / odlux / apps / networkMapApp / src / handlers / mapReducer.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2020 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 { IActionHandler } from '../../../../framework/src/flux/action';
20 import { Feature } from "../model/Feature";
21 import { HighlightLinkAction, HighlightSiteAction, ZoomToSearchResultAction, AddAlarmAction, SetCoordinatesAction, SetStatistics, SetIconSwitchAction, RemoveHighlightingAction } from '../actions/mapActions';
22
23 export type location = {lat: number, lon:number}
24
25 export type mapState = {
26     selectedLink: Feature | null,
27     selectedSite: Feature | null,
28     zoomToElement: location | null,
29     alarmlement: Feature|null,
30     lat: number,
31     lon: number, 
32     zoom: number,
33     statistics:{links: string, sites: string},
34     allowIconSwitch: boolean
35 }
36
37 const initialState: mapState ={
38     selectedLink: null,
39     selectedSite: null,
40     zoomToElement: null,
41     alarmlement: null,
42     lat: 52,
43     lon: 13,
44     zoom: 10,
45     statistics:{links:"Not counted yet.", sites: "Not counted yet."},
46     allowIconSwitch: true
47 }
48
49 export const MapReducer: IActionHandler<mapState> = (state=initialState, action: any) => {
50     
51     if(action instanceof HighlightLinkAction){
52       
53         state = Object.assign({}, state, {selectedSite: null, selectedLink:{type: "Feature", geometry:{type:"LineString", coordinates:[[action.link.locationA.lon,action.link.locationA.lat ],[action.link.locationB.lon,action.link.locationB.lat ]]}}})
54
55
56     }
57     else if(action instanceof HighlightSiteAction){
58        
59     state = Object.assign({}, state, {selectedLink: null, selectedSite:{type: "Feature", properties: {id: action.site.name, type:action.site.type}, geometry:{type:"Point", coordinates:[action.site.geoLocation.lon,action.site.geoLocation.lat ]}}})
60
61     }else if (action instanceof ZoomToSearchResultAction){
62         state = Object.assign({}, state, {zoomToElement:{lat: action.lat, lon: action.lon}});
63     }else if (action instanceof AddAlarmAction){
64         state = Object.assign({}, state, {alarmlement:action.element});
65
66     }else if(action instanceof SetCoordinatesAction){
67         state = Object.assign({}, state, {lat:action.lat, lon: action.lon, zoom:action.zoom});
68         
69     }else if(action instanceof SetStatistics){
70         state = Object.assign({}, state, {statistics:{sites: action.siteCount, links: action.linkCount}});
71
72     }else if (action instanceof SetIconSwitchAction){
73         state = Object.assign({}, state, {allowIconSwitch: action.enable});
74
75     }else if(action instanceof RemoveHighlightingAction){
76         state = Object.assign({}, state, {selectedLink: null, selectedSite:null})
77
78     }
79
80     return state;
81 }