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