Adding option for configurable header
[aai/sparky-fe.git] / src / app / MainScreenWrapperActionHelper.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 import {
22   POST,
23   POST_HEADER,
24   BASE_URL,
25   ERROR_RETRIEVING_DATA
26 } from 'app/networking/NetworkConstants.js';
27 import {
28   MESSAGE_LEVEL_DANGER
29 } from 'utils/GlobalConstants.js';
30 import {fetchRequestObj} from 'app/networking/NetworkCalls.js';
31
32 import {
33   getSetGlobalMessageEvent,
34   getClearGlobalMessageEvent
35 } from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.js';
36 import {aaiActionTypes} from 'app/MainScreenWrapperConstants.js';
37
38 function createWindowSizeChangeEvent() {
39   return {
40     type: aaiActionTypes.AAI_WINDOW_RESIZE
41   };
42 }
43
44 function createShowMenuEvent(show) {
45   return {
46     type: aaiActionTypes.AAI_SHOW_MENU,
47     data: {
48       showMenu: show
49     }
50   };
51 }
52
53 export function windowResize() {
54   return dispatch => {
55     dispatch(createWindowSizeChangeEvent());
56   };
57 }
58
59 export function showMainMenu(show) {
60   return dispatch => {
61     dispatch(createShowMenuEvent(show));
62   };
63 }
64
65 function createOverlayDataFoundEvent(overlayData, paramName, curData, responseEventKey) {
66   return {
67     type: responseEventKey,
68     data: {
69       paramName: paramName,
70       overlayData: overlayData,
71       curData: curData
72     }
73   };
74 }
75
76 function overlayViewData(dataFetchRequest, paramName, curData, responseEventKey) {
77   return dispatch => {
78     dataFetchRequest().then(
79       (response) => {
80         return response.json();
81       }
82     ).then(
83       (responseJson) => {
84         dispatch(createOverlayDataFoundEvent(responseJson, paramName, curData, responseEventKey));
85       }).catch(
86       () => {
87         dispatch(getSetGlobalMessageEvent(ERROR_RETRIEVING_DATA, MESSAGE_LEVEL_DANGER));
88         dispatch(createOverlayDataFoundEvent({}, paramName, curData, responseEventKey));
89       });
90   };
91 }
92
93 export function overlayNetworkCallback(urlApi, postBody, paramName, curData, responseEventKey) {
94   let dataFetchRequest =
95     () => fetchRequestObj(BASE_URL + urlApi, POST,
96       POST_HEADER, JSON.stringify(postBody));
97
98
99   return dispatch => {
100     dispatch(overlayViewData(dataFetchRequest, paramName, curData, responseEventKey));
101   };
102 }
103
104 function createViewDataFoundEvent(viewData, paramName, curViewData) {
105   var obj = {};
106   obj['data'] = {};
107   obj['data']['paramName'] = paramName;
108   obj['data']['viewData'] = viewData;
109   obj['data']['curViewData'] = curViewData;
110   obj['type'] = aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_RESPONSE_RECEIVED;
111
112   return obj;
113 }
114
115 function extensibleViewData(dataFetchRequest, paramName, curViewData) {
116   return dispatch => {
117     return dataFetchRequest().then(
118       (response) => {
119         return response.json();
120       }
121     ).then(
122       (responseJson) => {
123         dispatch(createViewDataFoundEvent(responseJson, paramName, curViewData));
124       }).catch(
125       () => {
126         dispatch(getSetGlobalMessageEvent(ERROR_RETRIEVING_DATA, MESSAGE_LEVEL_DANGER));
127         dispatch(createViewDataFoundEvent({}, paramName, curViewData));
128         //To-do: If need to send a flag later on to the view, add a function to have the flag
129       });
130   };
131 }
132
133 export function extensibleViewNetworkCallback(urlApi, postBody, paramName, curViewData) {
134
135   let dataFetchRequest =
136     () => fetchRequestObj(BASE_URL + urlApi, POST,
137       POST_HEADER, postBody);
138
139   return dispatch => {
140     dispatch(extensibleViewData(dataFetchRequest, paramName, curViewData));
141   };
142 }
143
144 export function extensibleViewMessageCallback(msgText, msgSeverity) {
145   if (msgText.length > 0) {
146     return dispatch => {
147       dispatch(
148         getSetGlobalMessageEvent(msgText, msgSeverity));
149     };
150   } else {
151     return dispatch => {
152       dispatch(getClearGlobalMessageEvent());
153     };
154   }
155 }
156
157 export function clearExtensibleViewData() {
158   return {
159     type: aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_CLEAR_DATA,
160     data: {}
161   };
162 }
163
164 export function setSecondaryTitle(title) {
165   return {
166     type: aaiActionTypes.SET_SECONDARY_TITLE,
167     data: title
168   };
169 }