[AAI-92 Amsterdam] Update license
[aai/sparky-fe.git] / src / app / globalAutoCompleteSearchBar / GlobalAutoCompleteSearchBarActions.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 {getTSUIElasticSearchQueryString} from 'app/networking/NetworkUtil.js';
24 import networkCall from 'app/networking/NetworkCalls.js';
25 import {
26   POST,
27   POST_HEADER,
28   ERROR_RETRIEVING_DATA
29 } from 'app/networking/NetworkConstants.js';
30
31 import {
32   globalAutoCompleteSearchBarActionTypes,
33   ERROR_INVALID_SEARCH_TERMS,
34   GLOBAL_SEARCH_URL
35 } from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarConstants.js';
36 import {
37   getSetGlobalMessageEvent,
38   getClearGlobalMessageEvent
39 } from 'app/GlobalInlineMessageBar/GlobalInlineMessageBarActions.js';
40
41
42 function createSuggestionFoundEvent({suggestions}) {
43   return {
44     type: globalAutoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
45     data: {suggestions}
46   };
47 }
48
49 function createSuggestionNotFoundEvent() {
50   return {
51     type: globalAutoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
52   };
53 }
54
55 function createSuggestionChangedEvent(value) {
56   return {
57     type: globalAutoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
58     data: value
59   };
60 }
61
62 function getInvalidQueryEvent(value) {
63   return {
64     type: globalAutoCompleteSearchBarActionTypes.NETWORK_ERROR,
65     data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
66   };
67 }
68
69 function getSearchBarWarningMessageEvent(message) {
70   return {
71     type: globalAutoCompleteSearchBarActionTypes.SEARCH_WARNING_EVENT,
72     data: {errorMsg: message}
73   };
74 }
75 export function getInvalidSearchInputEvent(value) {
76   return getSearchBarWarningMessageEvent(
77     ERROR_INVALID_SEARCH_TERMS + ': ' + value);
78 }
79
80
81 function fetchView(selectedSuggestion) {
82   return {
83     type: globalAutoCompleteSearchBarActionTypes.SUGGESTION_CLICKED,
84     data: {selectedSuggestion: selectedSuggestion}
85   };
86 }
87
88
89 export function populateView(
90   searchRequestObject, keyWord, selectedNodeFetchRequest) {
91   if (selectedNodeFetchRequest === undefined) {
92     let postBody = JSON.stringify(searchRequestObject);
93     selectedNodeFetchRequest =
94       () => networkCall.fetchRequest('', POST,
95         POST_HEADER, postBody);
96   }
97
98   return dispatch => {
99     dispatch(fetchView(searchRequestObject));
100   };
101 }
102
103
104 export function fetchRequestedValues(fetchRequestCallback, keyWord) {
105   return dispatch => {
106     return fetchRequestCallback().then(
107       (responseJson) => responseJson.suggestions
108     ).then(
109       (filteredResults)=> {
110         if (filteredResults.length > 0) {
111           dispatch(createSuggestionFoundEvent({suggestions: filteredResults}));
112         } else {
113           dispatch(createSuggestionNotFoundEvent());
114         }
115       }
116     ).catch(
117       () => {
118         dispatch(getInvalidQueryEvent(keyWord));
119       }
120     );
121   };
122 }
123
124 export function queryRequestedValues(keyWord, requestedFetchRequest) {
125   if (requestedFetchRequest === undefined) {
126     let postBody = JSON.stringify(getTSUIElasticSearchQueryString(keyWord));
127     requestedFetchRequest =
128       () => networkCall.fetchRequest(GLOBAL_SEARCH_URL, POST, POST_HEADER,
129         postBody);
130   }
131   return dispatch => {
132     dispatch(fetchRequestedValues(requestedFetchRequest, keyWord), keyWord);
133   };
134 }
135
136 export function clearSuggestionsTextField() {
137   return dispatch => {
138     dispatch(
139       {type: globalAutoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD});
140   };
141 }
142
143 export function onSuggestionsChange(event, value) {
144   return dispatch => {
145     dispatch(createSuggestionChangedEvent(value));
146     //Only fetch values if the enter key is used.
147     if (event.keyCode === 13) {
148       let postBody = JSON.stringify(getTSUIElasticSearchQueryString(value));
149       dispatch(fetchRequestedValues(
150         () => networkCall.fetchRequest(GLOBAL_SEARCH_URL, POST, POST_HEADER,
151           postBody), value));
152     }
153   };
154 }
155
156 export function onSuggestionsClearRequested() {
157   return dispatch => {
158     dispatch({type: globalAutoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS});
159   };
160 }
161
162 export function setNotificationText(msgText, msgSeverity) {
163   if (msgText.length > 0) {
164     return dispatch => {
165       dispatch(
166         getSetGlobalMessageEvent(msgText, msgSeverity));
167     };
168   } else {
169     return dispatch => {
170       dispatch(getClearGlobalMessageEvent());
171     };
172   }
173 }