Adding Violation History to DIO
[aai/sparky-fe.git] / src / app / MainScreenWrapperActionHelper.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 import {
24   POST,
25   POST_HEADER,
26   BASE_URL,
27   ERROR_RETRIEVING_DATA
28 } from 'app/networking/NetworkConstants.js';
29 import {
30   MESSAGE_LEVEL_DANGER
31 } from 'utils/GlobalConstants.js';
32 import {fetchRequestObj} from 'app/networking/NetworkCalls.js';
33
34 import {
35   getSetGlobalMessageEvent,
36   getClearGlobalMessageEvent
37 } from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.js';
38 import {aaiActionTypes} from 'app/MainScreenWrapperConstants.js';
39
40 function createWindowSizeChangeEvent() {
41   return {
42     type: aaiActionTypes.AAI_WINDOW_RESIZE
43   };
44 }
45
46 function createShowMenuEvent(show) {
47   return {
48     type: aaiActionTypes.AAI_SHOW_MENU,
49     data: {
50       showMenu: show
51     }
52   };
53 }
54
55 export function windowResize() {
56   return dispatch => {
57     dispatch(createWindowSizeChangeEvent());
58   };
59 }
60
61 export function showMainMenu(show) {
62   return dispatch => {
63     dispatch(createShowMenuEvent(show));
64   };
65 }
66
67 function createOverlayDataFoundEvent(overlayData, paramName, curData, responseEventKey) {
68   return {
69     type: responseEventKey,
70     data: {
71       paramName: paramName,
72       overlayData: overlayData,
73       curData: curData
74     }
75   };
76 }
77
78 function overlayViewData(dataFetchRequest, paramName, curData, responseEventKey) {
79   return dispatch => {
80     dataFetchRequest().then(
81       (response) => {
82         return response.json();
83       }
84     ).then(
85       (responseJson) => {
86         dispatch(createOverlayDataFoundEvent(responseJson, paramName, curData, responseEventKey));
87       }).catch(
88       () => {
89         dispatch(getSetGlobalMessageEvent(ERROR_RETRIEVING_DATA, MESSAGE_LEVEL_DANGER));
90         dispatch(createOverlayDataFoundEvent({}, paramName, curData, responseEventKey));
91       });
92   };
93 }
94
95 export function overlayNetworkCallback(urlApi, postBody, paramName, curData, responseEventKey) {
96   let dataFetchRequest =
97     () => fetchRequestObj(BASE_URL + urlApi, POST,
98       POST_HEADER, JSON.stringify(postBody));
99
100
101   return dispatch => {
102     dispatch(overlayViewData(dataFetchRequest, paramName, curData, responseEventKey));
103   };
104 }
105
106 function createViewDataFoundEvent(viewData, paramName, curViewData) {
107   var obj = {};
108   obj['data'] = {};
109   obj['data']['paramName'] = paramName;
110   obj['data']['viewData'] = viewData;
111   obj['data']['curViewData'] = curViewData;
112   obj['type'] = aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_RESPONSE_RECEIVED;
113
114   return obj;
115 }
116
117 function extensibleViewData(dataFetchRequest, paramName, curViewData) {
118   return dispatch => {
119     return dataFetchRequest().then(
120       (response) => {
121         return response.json();
122       }
123     ).then(
124       (responseJson) => {
125         dispatch(createViewDataFoundEvent(responseJson, paramName, curViewData));
126       }).catch(
127       () => {
128         dispatch(getSetGlobalMessageEvent(ERROR_RETRIEVING_DATA, MESSAGE_LEVEL_DANGER));
129         dispatch(createViewDataFoundEvent({}, paramName, curViewData));
130         //To-do: If need to send a flag later on to the view, add a function to have the flag
131       });
132   };
133 }
134
135 export function extensibleViewNetworkCallback(urlApi, postBody, paramName, curViewData) {
136
137   let dataFetchRequest =
138     () => fetchRequestObj(BASE_URL + urlApi, POST,
139       POST_HEADER, postBody);
140
141
142   return dispatch => {
143     dispatch(extensibleViewData(dataFetchRequest, paramName, curViewData));
144   };
145 }
146
147 export function extensibleViewMessageCallback(msgText, msgSeverity) {
148   if (msgText.length > 0) {
149     return dispatch => {
150       dispatch(
151         getSetGlobalMessageEvent(msgText, msgSeverity));
152     };
153   } else {
154     return dispatch => {
155       dispatch(getClearGlobalMessageEvent());
156     };
157   }
158 }
159
160 export function clearExtensibleViewData() {
161   return {
162     type: aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_CLEAR_DATA,
163     data: {}
164   };
165 }
166
167 export function setSecondaryTitle(title) {
168   return {
169     type: aaiActionTypes.SET_SECONDARY_TITLE,
170     data: title
171   };
172 }