6278f29e8e802826c56b469052ce43c15a391d50
[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 {aaiActionTypes} from './MainScreenWrapperConstants.js';
24 import {
25   POST,
26   POST_HEADER,
27   BASE_URL,
28   ERROR_RETRIEVING_DATA
29 } from 'app/networking/NetworkConstants.js';
30 import {
31   MESSAGE_LEVEL_DANGER
32 } from 'utils/GlobalConstants.js';
33 import {fetchRequestObj} from 'app/networking/NetworkCalls.js';
34
35 import {
36   getSetGlobalMessageEvent,
37   getClearGlobalMessageEvent
38 } from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.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 createViewDataFoundEvent(viewData, paramName, curViewData) {
68   var obj = {};
69   obj['data'] = {};
70   obj['data']['paramName'] = paramName;
71   obj['data']['viewData'] = viewData;
72   obj['data']['curViewData'] = curViewData;
73   obj['type'] = aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_RESPONSE_RECEIVED;
74
75   return obj;
76 }
77
78 function extensibleViewData(dataFetchRequest, paramName, curViewData) {
79   return dispatch => {
80     return dataFetchRequest().then(
81       (response) => {
82         return response.json();
83       }
84     ).then(
85       (responseJson) => {
86         dispatch(createViewDataFoundEvent(responseJson, paramName, curViewData));
87       }).catch(
88       () => {
89         dispatch(getSetGlobalMessageEvent(ERROR_RETRIEVING_DATA, MESSAGE_LEVEL_DANGER));
90         dispatch(createViewDataFoundEvent({}, paramName, curViewData));
91         //To-do: If need to send a flag later on to the view, add a function to have the flag
92       });
93   };
94 }
95
96 export function extensibleViewNetworkCallback(urlApi, postBody, paramName, curViewData) {
97
98   let dataFetchRequest =
99     () => fetchRequestObj(BASE_URL + urlApi, POST,
100       POST_HEADER, postBody);
101
102
103   return dispatch => {
104     dispatch(extensibleViewData(dataFetchRequest, paramName, curViewData));
105   };
106 }
107
108 export function extensibleViewMessageCallback(msgText, msgSeverity) {
109   if (msgText.length > 0) {
110     return dispatch => {
111       dispatch(
112         getSetGlobalMessageEvent(msgText, msgSeverity));
113     };
114   } else {
115     return dispatch => {
116       dispatch(getClearGlobalMessageEvent());
117     };
118   }
119 }
120
121 export function clearExtensibleViewData() {
122   return {
123     type: aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_CLEAR_DATA,
124     data: {}
125   };
126 }
127
128 export function setSecondaryTitle(title) {
129   return {
130     type: aaiActionTypes.SET_SECONDARY_TITLE,
131     data: title
132   };
133 }