d3feab9854a789fde7a7eacd87a6e7947ba67989
[aai/sparky-fe.git] / src / app / inventory / InventoryActions.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 {
27   GET,
28   POST_HEADER,
29   ERROR_RETRIEVING_DATA,
30   SAME_ORIGIN,
31   BACKEND_IP_ADDRESS
32 } from 'app/networking/NetworkConstants.js';
33
34 import {
35   INVENTORY_GEO_VISUALIZATION_SEARCH_URL,
36   GEO_VISUALIZATION_QUERY_STRING_PARAMETERS,
37   INVENTORY_COUNT_BY_TYPE_SEARCH_URL,
38   INVENTORY_COUNT_BY_DATE_SEARCH_URL,
39   InventoryActionTypes
40 } from 'app/inventory/InventoryConstants.js';
41
42 import {MESSAGE_LEVEL_DANGER} from 'utils/GlobalConstants.js';
43
44 function getSuccessfulTopographicVisualizationQueryEvent(responseJson) {
45   return {
46     type: InventoryActionTypes.TOPOGRAPHIC_QUERY_SUCCESS,
47     data: {
48       plotPoints: responseJson.plotPoints
49     }
50   };
51 }
52
53 function getFailedTopographicVisualizationQueryEvent() {
54   return {
55     type: InventoryActionTypes.TOPOGRAPHIC_QUERY_FAILED,
56     data: {
57       message: ERROR_RETRIEVING_DATA,
58       severity: MESSAGE_LEVEL_DANGER
59     }
60   };
61 }
62
63 function getSuccessfulCountByTypeQueryEvent(responseJson) {
64   return {
65     type: InventoryActionTypes.COUNT_BY_ENTITY_SUCCESS,
66     data: {
67       countByType: responseJson.result
68     }
69   };
70 }
71
72 function getFailedCountByTypeQueryEvent() {
73   return {
74     type: InventoryActionTypes.COUNT_BY_ENTITY_FAILED,
75     data: {
76       message: ERROR_RETRIEVING_DATA,
77       severity: MESSAGE_LEVEL_DANGER
78     }
79   };
80 }
81
82 function getSuccessfulCountsByDateQueryEvent(responseJson) {
83   return {
84     type: InventoryActionTypes.COUNT_BY_DATE_SUCCESS,
85     data: {
86       countByDate: responseJson.result
87     }
88   };
89 }
90
91 function getFailedCountByDateQueryEvent() {
92   return {
93     type: InventoryActionTypes.COUNT_BY_DATE_FAILED,
94     data: {
95       message: ERROR_RETRIEVING_DATA,
96       severity: MESSAGE_LEVEL_DANGER
97     }
98   };
99 }
100
101 function getDynamicTopographicQueryURL(entityType) {
102   return INVENTORY_GEO_VISUALIZATION_SEARCH_URL.replace(BACKEND_IP_ADDRESS,
103       BACKEND_IP_ADDRESS) +
104     GEO_VISUALIZATION_QUERY_STRING_PARAMETERS +
105     entityType;
106 }
107
108 function getCountByTypeQueryURL() {
109   return INVENTORY_COUNT_BY_TYPE_SEARCH_URL.replace(BACKEND_IP_ADDRESS,
110     BACKEND_IP_ADDRESS);
111 }
112
113 function getCountByDateQueryURL() {
114   return INVENTORY_COUNT_BY_DATE_SEARCH_URL.replace(BACKEND_IP_ADDRESS,
115     BACKEND_IP_ADDRESS);
116 }
117
118 export function onLoadTotalCountByDate() {
119   return function (dispatch) {
120     fetch(getCountByDateQueryURL(), {
121       credentials: SAME_ORIGIN,
122       method: GET,
123       headers: POST_HEADER
124     }).then(
125       (response) => response.json()
126     ).then(
127       (responseJson) => dispatch(
128         getSuccessfulCountsByDateQueryEvent(responseJson))
129     ).catch(
130       () => dispatch(getFailedCountByDateQueryEvent())
131     );
132   };
133 }
134
135 export function onCountByTypeLoad() {
136   return function (dispatch) {
137     fetch(getCountByTypeQueryURL(), {
138       credentials: SAME_ORIGIN,
139       method: GET,
140       headers: POST_HEADER
141     }).then(
142       (response) => response.json()
143     ).then(
144       (responseJson) => dispatch(
145         getSuccessfulCountByTypeQueryEvent(responseJson))
146     ).catch(
147       () => dispatch(getFailedCountByTypeQueryEvent())
148     );
149   };
150 }
151
152 export function onTopographicMapMounted(requestObject) {
153   return function (dispatch) {
154     fetch(getDynamicTopographicQueryURL(requestObject.entityType), {
155       credentials: SAME_ORIGIN,
156       method: GET,
157       headers: POST_HEADER
158     }).then(
159       (response) => response.json()
160     ).then(
161       (responseJson) => dispatch(
162         getSuccessfulTopographicVisualizationQueryEvent(responseJson))
163     ).catch(
164       () => {
165         dispatch(getFailedTopographicVisualizationQueryEvent());
166       }
167     );
168   };
169 }
170